Difference between cp -r and cp -R (copy command)

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

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.

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

Method 5

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x