97

cp -r is meant to copy files recursively, and cp -R for copying directories recursively. But I've checked, and both appear to copy both files and directories, the same thing. So, what's the difference actually?

its_me
  • 13,959

5 Answers5

102

While -R is posix well-defined, -r is not portable!

On Linux, in the GNU and BusyBox implementations of cp, -r and -R are equivalent.

On the other side, as you can read in the POSIX manual page of cp, -r behavior is implementation-defined.

    * If  neither  the  -R  nor  -r  options were specified, cp shall take
      actions based on the type and contents of the file referenced by the
      symbolic link, and not by the symbolic link itself.
* If the -R option was specified:

   * If  none  of  the  options  -H,  -L, nor -P were specified, it is
     unspecified which of -H, -L, or -P will be used as a default.

   * If the -H option was specified, cp shall take  actions based on
     the type and contents of the file referenced by any symbolic link
     specified as a source_file operand.

   * If the -L option was specified, cp shall take  actions based  on
     the type and contents of the file referenced by any symbolic link
     specified as a source_file operand or any symbolic links encoun-
     tered during traversal of a file hierarchy.

   * If  the  -P option was specified, cp shall copy any symbolic link
     specified as a source_file operand and any symbolic links encoun-
     tered  during traversal of a file hierarchy, and shall not follow
     any symbolic links.

* If the -r option was  specified,  the  behavior  is implementation-
  defined.

andcoz
  • 17,130
  • 21
    One of the differences would be that, in OSX for example, -r will copy the real files pointed by symbolic links while -R will copy the symbolic link which is what it is intended most of the times. – nacho4d Apr 15 '13 at 04:35
25

Lowercase -r was an older option, introduced in 4.1BSD, which would simply copy all non-directories as files. That is, if it encountered a device or FIFO, it would open it, read the contents, and create a file at the destination with the contents.

Uppercase -R was a standardized option (introduced to BSD in 4.4BSD, though earlier versions had it as a synonym to -r) which would, on encountering a device, FIFO, or other special file, make an equivalent special file at the destination.

Many implementations do still maintain this distinction, but some (including the GNU version typical to Linux) only provide the -R semantics, with -r as a synonym.

Random832
  • 10,666
20

The difference is that one uses a lowercase "R" and the other uses a capital "R". Beyond that, no difference. Same thing if you use the --recursive long option.

  • 4
    From the man page: -R, -r, --recursive - copy directories recursively – Dave Jennings Aug 14 '11 at 08:29
  • 9
    @DaveJennings: Just because they are equivolent on one platform doesn't mean they are on all. On some implementations of cp there actually is a distinction. See Random832's answer. @Ignacio you should include a qualifier in this answer that says "if your cp is the modern GNU implementation" or something like that. – Caleb Aug 15 '11 at 12:30
  • 2
    @Caleb: The question is tagged "linux". What else would it be? – Ignacio Vazquez-Abrams Aug 15 '11 at 20:06
  • 8
    -R and -r give different behavior on some of the systems I use (OS X, FreeBSD, BusyBox coreutils; not sure which ones at the moment). I decided long ago it was prudent to always use -R only, and in fact some of the man pages on the systems just mentioned also recommend that. – dubiousjim Nov 15 '12 at 14:17
4

In OS X and old versions of FreeBSD -r is like -R -L --copy-contents in coreutils, or it follows symlinks and reads the contents of special files and FIFOs.

mkdir a;touch b;ln -s $PWD/b a;cp -r a c replaces the symlink with the target file in OS X, mkdir a;mkfifo a/b;cp -r a c gets blocked reading the FIFO, and mkdir a;ln -s /dev/zero a;cp -r a b starts filling b/zero with zeroes.

From the cp man page in OS X and old versions of FreeBSD:

Historic versions of the cp utility had a -r option.  This implementation
supports that option; however, its use is strongly discouraged, as it
does not correctly copy special files, symbolic links, or fifo's.

In new versions of FreeBSD -r is equivalent to -RL:

Historic versions of the cp utility had a -r option.  This  implementation
supports that option, however, its  behavior is different from historical
FreeBSD behavior.   Use of this option is strongly discouraged as the
behavior is implementation-dependent.  In FreeBSD,  -r is a synonym for
-RL and works the same unless modified by other flags.  Historical  imple-
mentations  of -r differ as they copy special files as normal files while
recreating  a hierarchy.

http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html:

--copy-contents

If copying recursively, copy the contents of any special files (e.g., FIFOs and device files) as if they were regular files. This means trying to read the data in each source file and writing it to the destination. It is usually a mistake to use this option, as it normally has undesirable effects on special files like FIFOs and the ones typically found in the /dev directory. In most cases, cp -R --copy-contents will hang indefinitely trying to read from FIFOs and special files like /dev/console, and it will fill up your destination disk if you use it to copy /dev/zero. This option has no effect unless copying recursively, and it does not affect the copying of symbolic links.

Lri
  • 5,223
-1

one of the difference I found that -r do not copy hidden directories while -R copies hidden directories.

I tested it .git directory in target directory and came to above conclusion. I am using currently centOS.

I may be wrong but it's open to discussion.

Zeel SHah
  • 101