I'm looking for the difference between cp -r
and cp -a
. What does "recursive" mean in terms of copying files from a folder?
-
76some people are looking for a better explanation to the man pages because it is not always clear what the description meant. I have had to dig many times and test/experiment to figure out what is really going on with a particular command. grep's man page can confuse me any day.– JoeAug 7, 2012 at 22:41
-
2I can understand your frustration, but imo, most times best way to learn is to test and experiment.– tripledesAug 8, 2012 at 1:09
-
50OR learn by asking questions here, getting the answer in a way you understand, and then have a better sense of understanding what and how to test and experiment. Please keep in mind that works for you very often doesn't work for me, etc. etc.– Will LanniMar 27, 2015 at 0:41
-
2@tripledes, my computational biology professor said that is a great way to somewhat understand the specific cases you've tested without actually understanding the underlying rules. As a scientist, I can tell you that experiments only answer the questions you know to ask.– JoshApr 6, 2020 at 15:24
3 Answers
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.).
-
54Basically, unless you want something special, you never need
-r
because-a
(fora
rchive) is always the safest and probably what you expected to happen.– amsAug 8, 2012 at 9: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 usecp -rp
. Orrsync -a
. Aug 8, 2012 at 9:51 -
1
-
-
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. Nov 13, 2021 at 17:39
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.
-r=-R
-a=-dR --preserve=all
-d=--no-dereference --preserve=links
Then:
-r=-R
-a=-R --no-dereference --preserve=links --preserve=all