it-swarm.cn

cp -r和cp -R之间的区别(复制命令)

cp -r用于递归复制文件,而cp -R用于递归复制目录。但是我已经检查过,并且两者似乎都复制了文件和目录,都是一样的。那么,实际上有什么区别?

63
its_me

-R是posix定义明确的-r不可移植!

在Linux上,在GNU和BusyBox实现中,cp-r-R是等效的。

另一方面,您可以在 [〜#〜] posix [〜#〜]cp-r行为是实现定义的

    * 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.
76
andcoz

小写_-r_是一个较旧的选项,在4.1BSD中引入,它将简单地将所有非目录复制为文件。也就是说,如果遇到设备或FIFO,它将打开它,读取内容,并在包含该内容的目标位置创建文件。

大写的_-R_是标准化的选项(在4.4BSD中引入了BSD,尽管较早的版本将其作为_-r_的同义词),当遇到设备,FIFO或其他特殊文件时,它会生成一个目的地中的等效特殊文件。

许多实现仍然保持这种区别,但是某些实现(包括Linux中典型的GNU版本))仅提供_-R_语义,并以_-r_作为同义词。

19
Random832

区别在于,一个使用小写字母“ R”,另一个使用大写字母“ R”。除此之外,没有区别。如果您使用--recursive长选项。

18
Ignacio Vazquez-Abrams

在OS X和旧版本的FreeBSD中,-r类似于coreutils中的-R -L --copy-contents,或者它遵循符号链接并读取特殊文件和FIFO的内容。

mkdir a;touch b;ln -s $PWD/b a;cp -r a c将符号链接替换为OS X中的目标文件,mkdir a;mkfifo a/b;cp -r a c被阻止读取FIFO,并且mkdir a;ln -s /dev/zero a;cp -r a b开始用零填充b/zero

在OS X和旧版本的FreeBSD的cp手册页中:

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.

在新版本的FreeBSD中,-r等效于-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

如果以递归方式复制,请复制任何特殊文件(例如FIFO和设备文件)的内容,就像它们是常规文件一样。这意味着尝试读取每个源文件中的数据并将其写入目标。使用此选项通常是一个错误,因为它通常会对特殊文件(如FIFO)和通常在/dev目录中找到的文件产生不良影响。在大多数情况下,cp -R --copy-contents将无限期挂起,以尝试从FIFO和/dev/console之类的特殊文件中读取,并且如果您使用它复制/dev/zero,它将填满目标磁盘。除非以递归方式进行复制,否则此选项无效,并且不影响符号链接的复制。

3
Lri