Using 'xargs' command to copy files returns error?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using 'xargs' command to copy files returns error?
Hi, I am new to Linux and have been trying to learn some basic commands recently and just practicing with how to use them. I have been trying to copy two .pdf files from a directory called 'Downloads' in my home directory to another directory called 'randomstuff' which is also in my home directory. The names of the files are "Oreilly.Essential.System.Administration.3rd.Edition-www.gocit.vn.pdf" and "OReilly - Unix Power Tools.pdf". Starting from my home directory, I type the command:
find ~/Downloads/ -name "*.pdf" -print0 | xargs -0 cp ~/randomstuff/
I have also tried:
find ~/Downloads/ -name "*.pdf" -print0 | xargs -0 cp ~/Downloads/ ~/randomstuff/
but they both return this error:
cp: target `/root/Downloads/OReilly - Unix Power Tools.pdf' is not a directory
I'm assuming the problem might be with the spaces in the name? but i'm not sure.
0
That's a capital 'eye' not an 'ell' it basicly repleaces '{}' with the output from the pipe from the find command.
Find can be a bit tricky especially with the order of the arguments, tthe 'man' program is your friend here
Code:
man find
man xargs
Last edited by Keith Hedger; 06-10-2015 at 08:07 PM.
Reason: wish i could type
And whilst I realise this is a learning exercise, you may prefer the -exec parameter in "find" easier rather than piping to xargs. Also a little more efficient for large operations.
No less obtuse unfortunately.
Last edited by syg00; 06-10-2015 at 08:22 PM.
Reason: tried to clarify
0
That's a capital 'eye' not an 'ell' it basicly repleaces '{}' with the output from the pipe from the find command.
Find can be a bit tricky especially with the order of the arguments, tthe 'man' program is your friend here
Code:
man find
man xargs
Thanks, this method worked, but I am still unsure why my original one didn't work? Before posting this on the forum I already tried deleting the xarg and the pipe like sygOO suggested and the find worked by itself, which is why I wasn't sure where the problem was.
Hi, I am new to Linux and have been trying to learn some basic commands recently and just practicing with how to use them. I have been trying to copy two .pdf files from a directory called 'Downloads' in my home directory to another directory called 'randomstuff' which is also in my home directory. The names of the files are "Oreilly.Essential.System.Administration.3rd.Edition-www.gocit.vn.pdf" and "OReilly - Unix Power Tools.pdf". Starting from my home directory, I type the command:
find ~/Downloads/ -name "*.pdf" -print0 | xargs -0 cp ~/randomstuff/
I have also tried:
find ~/Downloads/ -name "*.pdf" -print0 | xargs -0 cp ~/Downloads/ ~/randomstuff/
but they both return this error:
cp: target `/root/Downloads/OReilly - Unix Power Tools.pdf' is not a directory
The problem is that the cp command treats the last argument as the destination, whereas xargs will be appending your source files to the end of the command line it generates. For your first example, the cp command will look something like
Code:
cp /root/randomstuff/ ... '/root/Downloads/OReilly - Unix Power Tools.pdf'
and that PDF file is not a valid destination for a multi-file copy.
The cp command has an alternative way to specify the destination and allow the source arguments to follow in the command line:
Code:
find ~/Downloads/ -name "*.pdf" -print0 | xargs -0 cp --target-directory=~/randomstuff/
# Or, the equivalent short form
find ~/Downloads/ -name "*.pdf" -print0 | xargs -0 cp -t ~/randomstuff/
BTW, the method using "xargs -I '{}' ..." is inefficient if there are a large number of files since it will invoke the cp command separately for each file to be copied. (Using "-I" implies "-L 1".)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.