cp -r without hidden files

LinuxBashCp

Linux Problem Overview


I have two directories and one is empty.

The first directory has many sub directories with hidden files. When I cp -r content from first directory to the second one, the hidden files gets copied too. Any solutions to escape them?

Linux Solutions


Solution 1 - Linux

You can use rsync instead of cp:

rsync -av --exclude=".*" src dest

This excludes hidden files and directories. If you only want to exclude hidden directories, add a slash to the pattern:

rsync -av --exclude=".*/" src dest

Solution 2 - Linux

You can do

cp -r SRC_DIR/* DEST_DIR

to exclude all .files and .dirs in the SRC_DIR level, but still it would copy any hidden files in the next level of sub-directories.

Solution 3 - Linux

rsync has "-C" option

http://rsync.samba.org/ftp/rsync/rsync.html

Example:

rsync -vazC  dir1 dir2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahulView Question on Stackoverflow
Solution 1 - LinuxEugene YarmashView Answer on Stackoverflow
Solution 2 - LinuxTuxdudeView Answer on Stackoverflow
Solution 3 - LinuxslitvinovView Answer on Stackoverflow