LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-10-2015, 06:43 PM   #1
Simon_zhu
LQ Newbie
 
Registered: Jun 2015
Posts: 8

Rep: Reputation: Disabled
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.
 
Old 06-10-2015, 07:46 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Rather than assume, see what data are actually being returned, then proceed to debug. Delete the pipe and the xargs and see what's presented.
 
Old 06-10-2015, 08:06 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Try:
Code:
find ~/Downloads/ -name "*.pdf" -print0 | xargs -I '{}' -0 cp '{}' ~/randomstuff
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
 
2 members found this post helpful.
Old 06-10-2015, 08:19 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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
 
Old 06-10-2015, 08:31 PM   #5
Simon_zhu
LQ Newbie
 
Registered: Jun 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
Try:
Code:
find ~/Downloads/ -name "*.pdf" -print0 | xargs -I '{}' -0 cp '{}' ~/randomstuff
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.
 
Old 06-10-2015, 09:10 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
The "find" returned the full pathname - you can't use that with a (source) directory in the "cp".
 
Old 06-11-2015, 12:15 AM   #7
Simon_zhu
LQ Newbie
 
Registered: Jun 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
The "find" returned the full pathname - you can't use that with a (source) directory in the "cp".
Thanks for the response. However, if that is the case how come when I enter the command:

cp 'Downloads/OReilly - Unix Power Tools.pdf' randomstuff/

or

cp Downloads/*.pdf randomstuff/

they both work properly? thanks
 
Old 06-11-2015, 12:17 AM   #8
Simon_zhu
LQ Newbie
 
Registered: Jun 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
ah nvm, i think i know what you mean, full path name as in /root/Downloads/*.pdf right?
 
Old 06-11-2015, 09:19 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by Simon_zhu View Post
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".)
 
1 members found this post helpful.
Old 06-11-2015, 04:56 PM   #10
Simon_zhu
LQ Newbie
 
Registered: Jun 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks for the reply, that clears everything up.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash and operation on multiple files using xargs and copy nass Slackware 3 09-22-2009 07:45 AM
the ./ command returns error msg in 6.06 Thane Ubuntu 1 09-26-2006 10:30 AM
du on specific files from find command (use xargs?) fireman949 Linux - Software 2 07-10-2005 01:42 AM
copying files and give new unique names to each file by using xargs command gnim66 Programming 6 06-22-2005 08:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration