265

I'm looking for the difference between cp -r and cp -a. What does "recursive" mean in terms of copying files from a folder?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Randy
  • 2,759

3 Answers3

299

Recursive means that cp copies the contents of directories, and if a directory has subdirectories they are copied (recursively) too. Without -R, the cp command skips directories. -r is identical with -R on Linux, it differs in some edge cases on some other unix variants.

By default, cp creates a new file which has the same content as the old file, and the same permissions but restricted by the umask; the copy is dated from the time of the copy, and belongs to the user doing the copy. With the -p option, the copy has the same modification time, the same access time, and the same permissions as the original. It also has the same owner and group as the original, if the user doing the copy has the permission to create such files.

The -a option means -R and -p, plus a few other preservation options. It attempts to make a copy that's as close to the original as possible: same directory tree, same file types, same contents, same metadata (times, permissions, extended attributes, etc.).

  • 66
    Basically, unless you want something special, you never need -r because -a (for archive) is always the safest and probably what you expected to happen. – ams Aug 08 '12 at 09:20
  • 3
    @ams Yes, that's a good summary. The only common reason to use -r is because you're on some unix variant other than Linux that doesn't have -a, and generally you'd use cp -rp. Or rsync -a. – Gilles 'SO- stop being evil' Aug 08 '12 at 09:51
  • 1
    sometimes permissions will allow -r but not -a – DrCord Dec 19 '15 at 15:40
  • Not -r, not -R doesn't work in Ubuntu 18 – Arkady Jun 17 '19 at 15:06
  • Good summary, but according to my man page, -a (short for --archive) means -dR --preserve=all. Which is almost the same as what you said. – Isaac Rabinovitch Nov 13 '21 at 17:39
  • 1
    macOS man says "Preserves structure and attributes of files but not directory structure" – Oleksii Nezhyborets Feb 05 '22 at 09:32
  • @OleksiiNezhyborets I wonder what this is supposed to mean. It also says that -a is an alias for -RPp and -R does preserve directory structure`. – Gilles 'SO- stop being evil' Feb 05 '22 at 10:37
  • @OleksiiNezhyborets, @Gilles'SO-stopbeingevil', I came across this but not directory structure note on the man page for cp on MacOS today. Did either of you happen to figure out what that means that it does not preserve the directory structure? – plafratt Jan 30 '24 at 15:11
  • 1
    @plafratt Beats me. They don't mean the natural language meaning of “preserv[ing] directory structure”, i.e. copying the nested arrangement of files inside directories that are themselves inside directories etc. Maybe they mean that the order of files in a directory is not preserved? You could view that as preserving the structure of individual directories (as opposed of preserving the structure of the files arranged in directories). Or maybe someone just used the wrong word and this wasn't caught during review. – Gilles 'SO- stop being evil' Jan 30 '24 at 21:09
28

The -r or -R option for "recursive" means that it will copy all of the files including the files inside of subfolders.

The -a option as listed is the same as -dR which means it will preserve links as well as copy the contents of subdirectories. What it means by preserving links is that it will not follow links as it is recursively copying.

DrCord
  • 103
Joe
  • 1,306
2
-r=-R
-a=-dR --preserve=all
-d=--no-dereference --preserve=links

Then:

-r=-R
-a=-R --no-dereference --preserve=links --preserve=all
Mario Palumbo
  • 233
  • 1
  • 14