Thursday, January 5, 2017

Linux - How to copy all files including hidden with cp command

Sometimes we need to copy an entire folder, including all its content from one location to another. For that purposes we can use cp command. But most of users (including me) experience problems with it. If you try to copy files using star wildcard "*", it wont copy hidden files and folders.

For example:
mkdir /tmp/dir1
cp -r /home/username/* /tmp/dir1
This method does not copy hidden files and folders, such as '/home/username/.bashrc'.

Solution


To recursively copy all directory contents including hidden files and directories, you can use the same cp command, but instead of "*" wildcard, you need to use dot symbol "." at the end:
cp -r /home/username/. /tmp/dir2
This will copy all files and directories from /home/username/ to /tmp/dir2. Also, you don't need to create target directory in advance. It will be created automatically for you.

No comments:

Post a Comment