directoryrsynchidden-files

Copy directory tree without files and hidden directories


This is a follow up to Copy folder structure (without files) from one location to another and Rsync how to include directories but not files?, though not a duplicate.

In essence, I want to do the same, i.e. copy a directory tree from src to dst without copying any files (recursively), except that I also want to skip all hidden folders (recursively).

Adapting this answer by adding --exclude '.*/' did not work as expected:

rsync -av --include '*/' --exclude '.*/' --exclude '*' src/ dst/

as it copies also hidden (dot) directories, .hidden in the following example:

building file list ... done
./
.hidden/
a/
a/.hidden/

I would appreciate an explanation why the attempt does not work as well as a solution to the problem.


Solution

  • From the rsync man-page:

    The order of the rules is important because the first rule that matches is the one that takes effect.

    So, move the include after the first exclude::

    rsync -av  --exclude '.*/' --include '*/' --exclude '*' src/ dst/