I want to rysnc my folder and subfolder tree structure without their files but with a max depth of 3 (/folder1/folder2/folder3) and exclude specific folders (ex. cache/)

I have tried this command:

$ rsync -azv --include=*/ --exclude=* --exclude=cache/ --exclude=/*/*/*/ source/ destination/

but it is not working... it is copying all folders and subfolders; it does not respect the max depth and it is not excluding cache folder

Do it need to use another parameter to restrict the max depth of the rsyc; what is wrong with my existing parameters?

p.s. last time I asked this question, it was tagged as duplicate of Rsync filter: copying one pattern only

It is not as it does not deal with max depth nor deal with rsync directory tree structure only. thx

  • 1
    It may not be a duplicate...but you need to read that other Q&A more carefully. The most obvious mistake in your command is that you're not single quoting quoting your patterns to avoid shell expansion (e.g. --exclude='/*/*/*/').
    – Wildcard
    Commented Oct 9, 2015 at 10:39
  • yup thx the Q&A is quite helpful
    – yeahman
    Commented Oct 25, 2015 at 8:07

2 Answers 2


You need to exclude the multi-level pattern before including */ as the latter means "include all directories", overriding the later exclude. You also need to add an extra level as */ indicates a directory and /*/*/*/ hence means "exclude three or more levels.

The command becomes:

rsync -azv --exclude='/*/*/*/*/' --include='*/' --exclude='*' --exclude=cache/ source/ destination/

(as Wildcard already commented you must always quote or escape shell metacharacters.)

  • thx for the answer .. it works fine except that I had to put --exclude=cache/ at the beginning for it to work properly.. I'm not really sure why
    – yeahman
    Commented Oct 15, 2015 at 19:57
  • Ah yes, the --include='*/' includes any cache directories before they're excluded (which as a result doesn't happen).
    – wurtel
    Commented Oct 16, 2015 at 7:47

To sync two directory trees while respecting the max depth limit of three subdirectories use:

rsync -avz /source/ /destination --exclude "/**/**/**/*" --exclude=cache

This will also exclude the directory called cache anywhere in the source path.

You must log in to answer this question.

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