Main Content

Copy multiple files - confusing error message from cp

Archive - Originally posted on "The Horse's Mouth" - 2006-12-30 09:59:53 - Graham Ellis

Thought you might like me to share this one ...

Copying a whole series of files into a directory (Linux, Unix, OS X) using cp, you give a whole series of source files followed by the name of the target directory. Add the -r option is you want to specify subdirectories to be copied recursively in your input.

So this should work, eh?

grahamellis$ cp -r whc.css images grgen.php ~/back_20061229/htaccess
usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src target
cp [-R [-H | -L | -P]] [-f | -i | -n] [-pv] src1 ... srcN directory
grahamellis$


What an odd result. Is the -r option wrong? No - that's not the problem. In my opinion, there's a bug in cp in that the usage line is incomplete, but that's not the real problem. The real problem was that I specified an existing file name as the target for my copy, and cp won't copy a whole series of files into one.

If I correct my command (by specifying a target directory name), it works perfectly:
grahamellis$ cp -r whc.css images grgen.php ~/back_20061229
grahamellis$


Footnote - if you want to preserve file ownerships as you copy them, use tar instead of cp. You need administrator access (i.e. be logged in as root) to do this.