34

I have the following tree

# upper letters = directory
# lower letters = files
A
|-- 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:

C
|-- e

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

2 Answers 2

54

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

1
  • 2
    Shortcut : -m
    – Opsse
    Feb 22, 2022 at 17:20
1

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

OR

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .