How to copy with cp to include hidden files and hidden directories and their contents?
Lets say you created the new folder (or are going to create one) and want to copy the files to it after the folder is created
mkdir /home/<new_user>
cp -r /etc/skel/. /home/<new_user>
This will copy all files/folder recursively from /etc/skel
in to the already existing folder created on the first line.
Don't specify the files:
cp -r /etc/skel /home/user
(Note that /home/user
must not exist already, or else it will create /home/user/skel
.)
The correct means of doing this is to use the -T (--no-target-directory)
option, and recursively copy the folders (without trailing slashes, asterisks, etc.), i.e.:
cp -rT /etc/skel /home/user
This will copy the contents of /etc/skel
to /home/user
(including hidden files), creating the folder /home/user
if it does not exist; however the -T
option prevents the contents of /etc/skel
from being copied to a new folder /home/user/skel
should the folder /home/user
exist.