115

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?

2
  • it's doable, but removing them after the copy might be simpler Commented Jul 19, 2012 at 8:50
  • 7
    yeah, but coping the hidden files to other locations is a security hazard in my case. Commented Jul 19, 2012 at 9:26

4 Answers 4

162

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 
Sign up to request clarification or add additional context in comments.

8 Comments

Any overheads when compared with cp?
@Rahul It did not occur to me to benchmark it, but I think you shouldn't worry about that
It works cool! Thanks man! Just for information, rsync would be great when file size is big, but equal to cp in case the smaller files.
rsync is much powerful than cp in all aspects.so you should use rsync as your file transferring tool
Doesn't seem to ignore hidden files in subdirectories (on macOS Big Sur, at least)
|
49

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.

4 Comments

That's exactly the problem.. :)
But it works for GIT-Repos, if you don't wan't to copy the whole GIT-Index in .git/ ...
I +'d this answer because this is the solution I was looking for. I don't want to exclude all invisibles up the tree, just the invisibles at the root level!
Another variant cp SOURCE/[!.]* TARGET.
5

rsync has "-C" option

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

Example:

rsync -vazC dir1 dir2 

2 Comments

You are right. I tried mkdir -p dir1/subdir; touch dir1/subdir/.cvs; rsync -avzC dir1 dir2
rsync -av --exclude=".*" src dest works great. Refer the answer above by eugene. Thanks anyway! :)
3

I came across the same need when I wanted to copy the files contained in a git repo, but excluding the .git folder, while using git bash.

If you don't have access to rsync, you can replicate the behavior of --exclude=".*" by using the find command along with xargs:

find ./src_dir -type f -not -path '*/.*' | xargs cp --parents -t ./dest_dir 

To give more details:

  • find ./src_dir -type f -not -path '*/.*' will find all files in src_dir excluding the ones where the path contain a . at the beginning of a file or folder.
  • xargs cp --parents -t ./dest_dir will copy the files found to dest_dir, recreating the folder hierarchy thanks to the --parents argument.

Note: This will not copy empty folders. And will effectively exclude all hidden files and folders from being copied.

Link to relevant doc:

https://linux.die.net/man/1/cp

https://linux.die.net/man/1/find

1 Comment

cp works. rsync fails. Mounted with jmtpfs. Copying visible files from Debian 13 to Android 13.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.