Jay Taylor's notes

back to listing index

How do you copy hidden files from one directory to another?

[web search]
Original source (www.linuxquestions.org)
Tags: linux howto dotfiles cp www.linuxquestions.org
Clipped on: 2013-08-26

Are all the items you are trying to copy normal files, or are there directories you need to copy as well?

I've just tried out what you describe. I found that cp without the -r flag works fine, but cp -r return the error messages you give. When I looked into the target directory, however, I found that many files that were not in the original directory had been copied. These were files from the parent directory.

The problem is that every directory on a unix-type file system contain two special directories:
  • "." refers to the directory itself
  • ".." refers to the parent directory

Thus, one of the directories you were trying to copy was the directory itself, and then the command will recursively copy everything from the directory above it in the file tree.

A solution is a bit of pattern matching. Try:

cp -r test/.[a-zA-Z0-9]* target

This will copy any file that begins with a dot followed by any lower- or upper-case letter or number, then followed by anything else.