You can use cp -a
to copy directory trees, but rsync
can do the same and give you more flexibility. rsync
supports a syntax for filter rules which specify which files and directories should and should not be copied.
Examples
Copy only the directory structure without copying any files:
$ rsync -a -f"+ */" -f"- *" source/ destination/
The two -f
arguments mean, respectively, "copy all directories" and then "do not copy anything else".
Copy only directories and Python files:
$ rsync -a -f"+ */" -f"+ *.py" -f"- *" source/ destination/
This is really handy for replicating the general directory structure but only copying a subset of the files.
Copy everything but exclude .git
directories:
$ rsync -a -f"- .git/" -f"+ *" source/ destination/
Conclusion
Of course, rsync
also works great for copying files between machines, and it knows better than to transfer files that already exist on the destination. I use something similar to the above to do backups, copying my homedir but excluding stuff like caches that are not even worth copying.