I have the following tree

# upper letters = directory
# lower letters = files
|-- B
|-- C
    |-- D
    |-- e <= file
|-- F
    |-- G

I need to copy this tree to another destination, recursively ignoring all the empty directories. So the destination ends up looking like:

|-- e

How would you do this with unix, rsync, etc?


Of course minutes later I discover an easy method. rsync has a --prune-empty-dirs option.


There are various solutions to this problem (Taken from this web page):

This option uses the mkdir command with the find command. This method also requires that you be inside the source folder while executing the command.

bash$ cd /path/to/source && find . -type d -exec mkdir -p /path/to/dest/{} ;

Using find and cpio

bash$ find /path/to/source -type d | cpio -pd /path/to/dest/

Using Rsync

bash$ rsync -a --include '*/' --exclude '*' /path/to/source /path/to/dest


bash$ rysnc -a -f"+ */" -f"- *" /path/to/source /path/to/dest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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