*
The moose likes Linux / UNIX and the fly likes cp command in linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "cp command in linux" Watch "cp command in linux" New topic
Author

cp command in linux

Rahul Dhaware
Greenhorn

Joined: Aug 13, 2008
Posts: 22
batchId2="cp '24 - 28 Nov_Task_5194_1.txt' /home/backup/"
I want to use now $batchId to execute cp command.
when i used cp on files which contains space in thire name , i enclosed file name with ''.

but when i used $batchId2 in shell script it does not wrk.

Can anyone please help me?
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Can you give us more to go on... like the segment of shell script that isn't working and how you're setting the variable originally?


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Rahul Dhaware
Greenhorn

Joined: Aug 13, 2008
Posts: 22
Following is my shell script.


#!/bin/sh

fileNames="'abc 345 temp.pdf' 'pqs_234.pdf'"

destinationPath="/home/temp/"

cd /home/myfolder/

cp $fileNames $destinationPath


but if i execute cp command at console like
cp 'abc 345 temp.pdf' 'pqs_234.pdf' /home/temp/

then it wrk.

so why it's not wrk when i execute it from shell script?
Please help me.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Have you tried executing this on the shell? Like this:If you have, you'll see that Bash (I assume that's the shell you're using) expands the spaces into separate filenames, treating the quotes as part of the words. So the cp command is actually seeing these files:
  • 'abc
  • 345
  • temp.pdf'
  • 'pqs_234.pdf'
  • which isn't what you want.

    There are a couple of changes you can make. The first is to use the xargs command, like this:That should work.

    Alternatively, you can change Bash's token identification delimiter. So rather than using quote marks and Bash interpreting the spaces as delimiters, you can separate filenames with any other delimiter character not likely to appear in each name. Common examples are a newline or a colon, or any other character you know won't be found in the filenames. I'll go with the latter:Note you will need to put IFS back to the original value after running the command as that change will likely break other parts of your script.

    Post back if that works or doesn't work for you...
    [ December 10, 2008: Message edited by: Charles Lyons ]
    Rahul Dhaware
    Greenhorn

    Joined: Aug 13, 2008
    Posts: 22
    yes Charles, it works.

    Thank you very much.

    I have solved my critical problem.
    Thanks once again
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: cp command in linux