Answers:
虽然-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.
-r
将复制符号链接所指向的实际文件,而-R
将复制符号链接,这在大多数情况下是需要的。
区别在于,一个使用小写的“ R”,另一个使用大写的“ R”。除此之外,没有区别。如果使用--recursive
long选项,则相同。
cp
实际上,在某些实现上存在区别。参见Random832的答案。@Ignacio,您应该在此答案中包含一个限定词,说明“如果您cp
是现代GNU实现”或类似的东西。
-R
只使用它是谨慎的做法,实际上,刚才提到的系统上的一些手册页也建议这样做。
在OS X和旧版本的FreeBSD -r
中,就像-R -L --copy-contents
在coreutils中一样,或者它遵循符号链接并读取特殊文件和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
零。
cp
在OS X和旧版本的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.
在新版本的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
。除非以递归方式进行复制,否则此选项无效,并且不影响符号链接的复制。
我发现其中之一是-r不会复制隐藏目录,而-R会复制隐藏目录。
我在目标目录中测试了.git目录,得出了以上结论。我当前正在使用centOS。
我可能是错的,但值得讨论。
-r
并且-R
都复制了隐藏目录