1

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

2
  • 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
    Oct 9 '15 at 10:39
  • yup thx the Q&A is quite helpful
    – yeahman
    Oct 25 '15 at 8:07
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.)

2
  • 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
    Oct 15 '15 at 19:57
  • Ah yes, the --include='*/' includes any cache directories before they're excluded (which as a result doesn't happen).
    – wurtel
    Oct 16 '15 at 7:47
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.