31

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?

Dane O'Connor
  • 1,199
  • 2
  • 14
  • 20

2 Answers2

46

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

Dane O'Connor
  • 1,199
  • 2
  • 14
  • 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

VL-80
  • 228
  • 4
  • 16