Copy folder structure, but not the files

resource

See the folder structure first

First install the tree utility into gitbash according to this tutorialla

tree -dfi --noreport rootDir

Next, let’s walk through the options we passed to the tree command and understand what they mean:

  • -d: We ask the tree command to print directories only

  • -f: The tree command will print the full path for us

  • -i: Usually, tree outputs in a tree format. In other words, lines are well indented in the output. However, this option will prevent tree from printing indentation lines

  • –noreport: This flag suppresses the summary report at the end of the output, such as “x directories, y files

Using rsync

Using rsync
rsync -av -f"+ */" -f"- *" "/path/to/the/source/rootDir" "/tmp/test"

The options we passed to the rsync command look a bit obscure. Next, let’s go through them quickly.

The -a option asks rsync to do an “archive” copying. In other words, the copying will be done recursively and with file permission preserved. Next, the -v option means verbose. With this option, we can see a detailed log of what the rsync command has done.

Next, the tricky part -f”+ */” -f”- *” comes.

Each -f option defines a filter, and a filter rule follows a filter. When rsync works, it’s going to apply the filters on files it wants to copy.

A plus “+” indicates “include”, while a minus “-” means “exclude”. In our command, we include all directories “+ */” but exclude all files “- *”. Thus, the rsync command will only copy directories under the source directory and skip all files.

Last updated