Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use the a Linux terminal to copy many files to a new location. I tired the command:

sudo locate -i *file_I_want* | xargs cp $1 /place_I_want_everything_copied_to/

Which, gives an error. What's the correct way to handle this?

share|improve this question
Do they all have a similar name like file001.jpg file002.jpg file003.jpe? Otherwise cp should do what you want with cp -r /a/folder – jwbensley Feb 3 '12 at 17:04
What is the exact error? – Zoredache Feb 3 '12 at 17:06
@javano Yup the all have similar file names, and they're all in different folders across the system. – InBetween Feb 3 '12 at 17:15
@Zoredache /somedir/.../file.jpg isn't a directory (of course). But if I plug in the file path into the cp command (right where the $1 is) just like I have it up there, then it works. If xargs works like I think it does, I don't see what I'm doing wrong. Everything I've read on xargs makes me think want I have up there is correct. Apparently it's not. – InBetween Feb 3 '12 at 17:17
cp $(locate -i *.jpg) /destination/folder – jwbensley Feb 3 '12 at 17:30
up vote 1 down vote accepted

xargs doesn't work that way. It doesn't use $1. It normally just postpends the argument to the end of the param list. But in this case it won't work, since it's the target, not the destination and a trailing parameter is not what's needed.

Check out the -i flag to xargs, it allows you to have a substitution. The default is to use {}, I guess to be consistent with find

sudo locate -i *file_I_want* | xargs -i cp {} /place_I_want_everything_copied_to/

@javano 's comment is an answer as well, though would break if there are spaces in the filenames (though I believe spaces would break the xargs usage above as well)

share|improve this answer
Thanks a lot for the help! – InBetween Feb 3 '12 at 18:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.