Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I copy recursively like cp -rf *, but excluding hidden directories (directories starting with .) and their contents?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

You could just copy everything with

cp -rf 

and then delete hidden directories at the destination with

find -type d -name '.*' -and -not -name '.' -print0 | xargs -0 rm -rf

Alternatively, if you have some advanced tar (e.g. GNU tar), you could try to use tar to exclude some patterns. But I am afraid that is not possible to only exclude hidden directories, but include hidden files.

For example something like this:

tar --exclude=PATTERN -f - -c * | tar -C destination -f - -x

Btw, GNU tar has a zoo of exclude style options. My favourite is

--exclude-vcs
share|improve this answer

Good options for copying a directory tree except for some files are:

  • rsync: this is basically cp plus a ton of exclusion possibilities.

    rsync -a --exclude='.*' /source/ /destination
    
  • pax: it has some exclusion capabilities, and it's in POSIX so should be available everywhere (except that some Linux distributions don't include it in their default installation for some reason).

    cd /source && mkdir -p /destination && \
    pax -rw -pp -s '!.*/\..*!!'  . /destination
    
share|improve this answer

alternatively to cp you could use rsync with an --exclude=PATTERN.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.