Command line recursive/deep directory copy in Linux?

What is a good, general way to make a recursive/deep directory copy in Linux that works in most cases? I've used simple things like cp -R as well as fairly elaborate cpio incantations. Are there any significant strengths or weaknesses that cause you to prefer one over the other? Which one do you use most often?


Solution 1:

NAME
cp - copy files and directories

-a, --archive
   same as -dpR

-d     same as --no-dereference --preserve=links
-p     same as --preserve=mode,ownership,timestamps
-R, -r, --recursive
    copy directories recursively

So in answer to your question:

cp -a /foo /bar

Copy everything recursively from directory /foo to directory /bar while preserving symbolic links and file/directory 'mode' 'ownership' & 'timestamps'.

Solution 2:

I use a command like "cd $srcdir ; tar -c . | tar -C $destdir -x " most often. But I also use rsync -a $src $dst.

The biggest strength of the tar solution is that it is what I had to use on a system many years ago that didn't have cpio, rsync or a cp that would copy recursively. Tar is pretty much everywhere. It is stuck on my head because I used it a lot, there probably are more elegant ways. It always seems to get the job done correctly, so I have never really looked to hard to find a replacement.