Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cp -r without hidden files

Tags:

linux

bash

cp

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?

like image 963
Rahul Avatar asked Jul 19 '12 08:07

Rahul


People also ask

Does cp copy hidden folders?

at end of the source path is a specific cp syntax that allowes to copy all files and folders, including hidden ones.

Does cp move hidden files?

We can copy hidden files and folders in Ubuntu distribution through cp command-line utility. In addition to, other cp command-line utility options would also be discussed. It is worth mentioning here that, cp command only copies files (not folders) by default.

Does the cp command remove the original file?

cp does not delete files. it does in that it overwrites dirA/dir1 with dirB/dir1, rather than just copying the files in that folder.

How do I cp all files?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.


2 Answers

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 
like image 104
Eugene Yarmash Avatar answered Oct 26 '22 00:10

Eugene Yarmash


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.

like image 45
Tuxdude Avatar answered Oct 25 '22 22:10

Tuxdude