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?

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'.

| improve this answer | |
  • This is certainly a very straight-forward and elegant solution. Have you ever found any drawbacks to this approach? Distributed file systems? Extremely large copies? Have you found other solutions like cpio, tar, or rsync to be more efficient in some cases? – Greg Mattes May 13 '09 at 0:58
  • 1
    Unless there's a physical error I've never had a local 'cp' fail from memory (I've only done TB sized transfers) to any place mounted localy (regardless of NFS or Samba mounting). Checkout 'USAGE' under the rsync man page for examples you'll find instructive. Favourite of mine: $ rsync --partial --progress --rsh=ssh --archive --verbose --compress foo/ user@hosname:~/bar (rsync -avzP) – Gareth May 13 '09 at 1:58
  • As I noted above, -a is not strictly portable. – Matthew Flaschen May 13 '09 at 4:52
  • Well I know that busybox, GNU & BSD cp are ok. Other than legacy Unix boxes I've never seen what won't that work on? I don't really see the point otherwise although I completely understand Zoredache's sentiment of "It always seems to get the job done correctly, so I have never really looked to hard to find a replacement." – Gareth May 13 '09 at 5:56
  • @gyaresu Please consider splitting your second comment out into another answer so that we can vote on it. – Greg Mattes May 13 '09 at 13:40

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.

| improve this answer | |
  • I like "the ubiquity of tar" argument a lot. I am proficient in vi for similar reasons even though I prefer other editing environments. – Greg Mattes May 13 '09 at 0:34
  • -R has long since been part of the standard (opengroup.org/onlinepubs/009695399/utilities/cp.html) though not -a. – Matthew Flaschen May 13 '09 at 1:38
  • @Matthew Flaschen: Is your comment intended for @gyaresu's answer? – Greg Mattes May 13 '09 at 1:46
  • It's also relevant to this one, as Zoredache said he encountered a system that "didn't have [...] a cp that would copy recursively". Basically, -R -> portable, -a -> not portable. – Matthew Flaschen May 13 '09 at 4:51
  • 1
    This was back in ~95 and the system was running a late 80's version of Unix. I don't think -R was an option for cp, but I am not certain. I learned that tidbit reading the usenet. – Zoredache May 13 '09 at 6:23

Take a look a rsync ... I like it because you copy less data when keeping two directories up to date ... it can also work remotly. In its simplest form rsync -a /src /dest

| improve this answer | |
  • This is a really good point. Essentially rsync has the ability to compute a "directory diff" and transfer only what is needed to synchronize the directories. So you can repeatedly call something like rsync -a src/ dest to "continually copy" directories recursively (the trailing / on src is needed for proper synchronization) whereas cp -a src/ dest doesn't work like that. The cp command will make a new src directory in dest after the first cp. You'd need something like cp -au src/* dest for subsequent copies. – Greg Mattes May 13 '09 at 13:39
  • The other reason I like rsync is you can pass it the -P flag, which shows a progress bar. It gives you some idea of how long something will take – Rory Jul 15 '09 at 9:26

rsync is a great tool. It is the swiss army knife of transfering data. It's such a simple and powerful tool. Once you start using it, you get hooked.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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