Home » Questions » Computers [ Ask a new question ]

How to copy with cp to include hidden files and hidden directories and their contents?

How to copy with cp to include hidden files and hidden directories and their contents?

How can I make cp -r copy absolutely all of the files and directories in a directory

Asked by: Guest | Views: 343
Total answers/comments: 5
bert [Entry]

"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.)"
bert [Entry]

"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."
bert [Entry]

"rsync is good, but another choice:

cp -a src/ dst/

From the main help:

-a, --archive
same as -dR --preserve=all

-d same as --no-dereference --preserve=links

-R, -r, --recursive
copy directories recursively"
"rsync is good, but another choice:

cp -a src/ dst/

From the main help:

-a, --archive
same as -dR --preserve=all

-d same as --no-dereference --preserve=links

-R, -r, --recursive
copy directories recursively"
bert [Entry]

"You could use rsync.

rsync -aP ./from/dir/ /some/other/directory/

You can even copy over ssh

rsync -aP ./from/dir/ username@remotehost:/some/other/directory/

There are various flags you can use:
-a, --archive # archive (-rlptgoD)

-r, --recursive
-l, --links # copy symlinks as links
-p, --perms # preserve permissions
-t, --times # preserve times
-g, --group # preserve group
-o, --owner # preserve owner
-D # --devices --specials

--delete # Delete extra files

You may want to add the -P option to your command.

--partial # By default, rsync will delete any partially transferred file if the transfer is interrupted. In some circumstances it is more desirable to keep partially transferred files. Using the --partial option tells rsync to keep the partial file which should make a subsequent transfer of the rest of the file much faster.

-P # The -P option is equivalent to --partial --progress. Its purpose is to make it much easier to specify these two options for a long transfer that may be interrupted.

Rsync man page"
bert [Entry]

"The simplest way is:
cp -r /etc/skel/{.,}* /home/user

The expression {.,}* includes all files and directories (also starting with a dot).
If you don't want use above expression, then you can use the cp property, which is the ability to specify multiple sources for one target folder:
cp -r /etc/skel/* /home/user"