2

Is there any difference between:

cp -R /a/* /b 

and

cp -R /a/. /b

The original idea was to copy anything from folder /a into folder /b.

prosti
  • 1,038

2 Answers2

2

The only difference is that the first command,

cp -R /a/. /b

would copy hidden files and directories from /a to /b, while the second command,

cp -R /a/* /b

would not do so.

The reason for the second command not copying hidden files is that the * expands to all the non-hidden names in /a (unless the shell option dotglob is set in bash, or the equivalent option in whatever shell is being used, if available).


The original question used -r in the second command instead of -R:

The flag -r is kept in some implementations of cp (GNU cp for example) for backwards compatibility. It is a non-standard flag for the cp command and on implementation that have it, it is similar to -R.

In GNU and AIX cp, -r and -R are the same. In some historical implementations of cp, it handles special files such as FIFOs and sockets differently. Solaris' implementation of cp -r/-R is only different for FIFOs (-R recreates them, -r reads from them). None of the free BSDs have -r in their cp implementations.

Kusalananda
  • 333,661
  • sorry I meant to say -R for both. – prosti Jul 04 '18 at 18:30
  • Look at the standard http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html and the historic UNIX man pages to learn that -r and -R are definitely not equivalent. – schily Jul 04 '18 at 18:43
  • @schily Fair enough. I was looking at the only implementation I knew that supports -r (GNU) in which it is equivalent. I'll update. – Kusalananda Jul 04 '18 at 18:49
  • 1
    Well, GNU is an abbreviation for GNU is not UNIX ;-) – schily Jul 04 '18 at 18:54
-1
cp -R

copies recursive and copies the original filetype. This is the only recursive method mentioned in the POSIX standard.

cp -r

is the historic option for recursive copy. It opens all files and reads the content, then creates plain files with the read content. This is the historic UNIX recursive option. It may not be available in modern implementations.

See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html for the POSIX man page.

schily
  • 19,173