Home » Questions » Computers [ Ask a new question ]

Copy directory contents using 'cp' command

Copy directory contents using 'cp' command

How do you copy all the contents of one directory into another?

Asked by: Guest | Views: 233
Total answers/comments: 3
Guest [Entry]

"Remember that, by default, cp copies the first directory into the second directory if the second exists.

For example cp -r a b will copy the directory a into b. If b does not exist, it will be created with the contents of a.

If you want to copy the content of a into b (for example when copying a whole filesystem into a mount point) use:

cp -r a/. b

as in the previous answer.

Please also note that -a, used in some of the other answers, is the same as -dr --preserve=all and will preserve timestamps, context and extended attributes."
Guest [Entry]

"To copy files that begins with a dot just do cp .* target/

So easiest is just to do the cp command two times.

As Peter Eisentraut sais normal globbing rules do not include .. and . (hm, how to end this sentence? ;)

Just use -r to make it recursive and -i to make cp ask whether you really want to overwrite a file.

cp -ri /backup/olduser/* /newuser/
cp -ri /backup/olduser/.* /newuser/"
Guest [Entry]

"This works for copying all the files and directories except for hidden directories recursively from the current directory to wherever:

cp -rf * ^.*"