15

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?

Greg Mattes
  • 324
  • 1
  • 4
  • 11

4 Answers4

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

Gareth
  • 8,573
  • 13
  • 44
  • 44
  • 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 00: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 01:58
  • As I noted above, -a is not strictly portable. – Matthew Flaschen May 13 '09 at 04: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 05: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
6

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.

Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • 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 00:34
  • -R has long since been part of the standard (http://www.opengroup.org/onlinepubs/009695399/utilities/cp.html) though not -a. – Matthew Flaschen May 13 '09 at 01:38
  • @Matthew Flaschen: Is your comment intended for @gyaresu's answer? – Greg Mattes May 13 '09 at 01: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 04: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 06:23
6

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

trent
  • 3,114
  • 19
  • 17
  • 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 – Amandasaurus Jul 15 '09 at 09:26
0

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.

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253