Unix Power Tools
Copying Directory Trees with cp -r

by Jerry Peek

Some versions of cp have a -r (recursive) flag. It copies all the files in a directory tree -- that is, all the files in a directory and its subdirectories.

The first argument(s) to cp -r can be directory(s) - or, if you name any file(s), they'll be copied just the way they would without the -r. The last argument should be a directory. So, you can use cp -r in two ways:

Those two methods are really doing the same thing. They're both copying the tail of the first pathname(s) to the end of the last pathname.

Some gotchas: