How to copy files from one location to another using xargs??

Hello Experts,

I need to copy files from one location to another using xargs.

Tried something like this (In Ubuntu & Solaris ).

mkdir -p 1234;
find /home/emd/Desktop/n007/M007/ -type f -name "A2014*" | xargs -0 cp -r {} /home/emd/Desktop/1234

But every time i run this, a weird error pops up.

"File name too long"

Files to be copied have long names

A20140422.1115-1130.theoriginaldata.memory.self.Mb.bin.gz

but they cant be altered either and i have tried most of the things in the internet.
#Helpneeded

If you use the -0 option to xargs , you need to -print0 from find . And, the {} in xargs works only if you specified the replace_string with the -I option.

1 Like
find /home/emd/Desktop/n007/M007/ -type f -name "A2014*" -print0 | xargs -0 cp -t /home/emd/Desktop/1234
2 Likes

Why are you fixated on using xargs?

This isn't Twitter, so no need to for the hashtag.

Regards,
Alister

Apologies for the hashtag.

---------- Post updated at 01:07 AM ---------- Previous update was at 01:04 AM ----------

Hi SriniShoo,

Thanks a lot. Will try the above and let you know.

Regards,
Saidul

Note that unless you happen to be sitting in /home/emd/Desktop when you run this script, mkdir will create 1234 in whatever directory you're sitting in; not necessarily /home/emd/Desktop .

It has already been mentioned that if you use -0 on xargs , you need to use -print0 on find ; but neither of these are available on Solaris systems (unless you have the GNU utilities installed). But, unless there are whitespace characters (<space>, <tab>, and <newline>) or quotes (<single-quote> or <double-quote>) in your filenames, you don't need them (and your sample names don't appear to need them).

Since you are using -type f in find , cp won't be given any directories as operands (other than the target directory given as the last operand), the -r option is a no-op.

But, with the xargs -I or -i option, one of which is needed if you don't want xargs to add the pathnames read from standard input to the end of the constructed cp command line; xargs will only pass one pathname from standard input as an operand to cp at a time. So, xargs is a waste of time. A simpler script that should work on both Ubuntu and Solaris systems is:

dest="/home/emd/Desktop/1234"
mkdir -p "$dest"
find /home/emd/Desktop/n007/M007/ -type f -name "A2014*" -exec cp {} "$dest" \;
2 Likes

Hi Don Cragun,

The above worked.
Thanks a lot for the detailed explanation.

BR,
Saidul