12

This answer reveals that one can copy all files - including hidden ones - from directory src into directory dest like so:

mkdir dest
cp -r src/. dest

There is no explanation in the answer or its comments as to why this actually works, and nobody seems to find documentation on this either.

I tried out a few things. First, the normal case:

$ mkdir src src/src_dir dest && touch src/src_file src/.dotfile dest/dest_file
$ cp -r src dest
$ ls -A dest
dest_file  src

Then, with /. at the end:

$ mkdir src src/src_dir dest && touch src/src_file src/.dotfile dest/dest_file
$ cp -r src/. dest
$ ls -A dest
dest_file  .dotfile  src_dir  src_file

So, this behaves simlarly to *, but also copies hidden files.

$ mkdir src src/src_dir dest && touch src/src_file src/.dotfile dest/dest_file
$ cp -r src/* dest
$ ls -A dest
dest_file  src_dir  src_file

. and .. are proper hard-links as explained here, just like the directory entry itself.

Where does this behaviour come from, and where is it documented?

  • 3
    What do you mean nobody can find documentation? The cp reference clearly explains how cp -R works. . and .. are directories just like any other directories, there is nothing magical or mysterious about them. – AlexP Dec 6 '17 at 14:58
  • 2
    @AlexP I edited the answer to make it clearer. The whole point is that . and .. don't behave like other directories. – iFreilicht Dec 6 '17 at 15:07
  • I have tried to explain why it works at How to copy a folder recursively in an idempotent way using cp – roaima Feb 16 at 9:32
23

The behaviour is a logical result of the documented algorithm for cp -R. See POSIX, step 2f:

The files in the directory source_file shall be copied to the directory dest_file, taking the four steps (1 to 4) listed here with the files as source_files.

. and .. are directories, respectively the current directory, and the parent directory. Neither are special as far as the shell is concerned, so neither are concerned by expansion, and the directory will be copied including hidden files. *, on the other hand, will be expanded to a list of files, and this is where hidden files are filtered out.

src/. is the current directory inside src, which is src itself; src/src_dir/.. is src_dir’s parent directory, which is again src. So from outside src, if src is a directory, specifying src/. or src/src_dir/.. as the source file for cp are equivalent, and copy the contents of src, including hidden files.

The point of specifying src/. is that it will fail if src is not a directory (or symbolic link to a directory), whereas src wouldn’t. It will also copy the contents of src only, without copying src itself; this matches the documentation too:

If target exists and names an existing directory, the name of the corresponding destination path for each file in the file hierarchy shall be the concatenation of target, a single slash character if target did not end in a slash, and the pathname of the file relative to the directory containing source_file.

So cp -R src/. dest copies the contents of src to dest/. (the source file is . in src), whereas cp -R src dest copies the contents of src to dest/src (the source file is src).

Another way to think of this is to compare copying src/src_dir and src/., rather than comparing src/. and src. . behaves just like src_dir in the former case.

  • But it doesn't behave the same way. Specifying src will copy the directory into dest, src/. will copy the contents. I'll try to make that clearer in the question. – iFreilicht Dec 6 '17 at 14:59
  • There, I think that answers your underlying question. – Stephen Kitt Dec 6 '17 at 15:07
  • Not sure what you mean by so neither are concerned by expansion (which avoids issues with the shell expanding hidden files or not). cp -r src/.* dest/ is a problem in shells like bash that don't remove . and .. from their globs (fixed in zsh, fish, pdksh (from the Forsyth shell) and derivatives). – Stéphane Chazelas Dec 6 '17 at 15:22
  • 1
    @Stéphane the OP compares copying src/. and src/* (note, not src/.*); src/* doesn’t include hidden files if globbing ignores them... – Stephen Kitt Dec 6 '17 at 15:23
  • Ah OK. Note that yash -o dotglob -c 'echo *' does include . and ... * can include . and .. as well in ksh depending on how you configure FIGNORE. – Stéphane Chazelas Dec 6 '17 at 15:27
1

When you run cp -R src/foo dest, you'll get dest/foo. So if directory dest/foo does not exist, cp will create it, and then copy the contents of src/foo to dest/foo.

When you run cp -R src/. dest, cp sees that dest/. exists, and then it's just the matter of copying the contents of src/. to dest/..

When you think of it as copying a directory named . from src and merging its contents with the existing directory dest/., it will make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.