It appears in the code below, rsync ignores --exclude. How would I correct that, and extend it to multiple directories?

$ rsync --help | grep Usage
Usage: rsync [OPTION]... SRC [SRC]... DEST

$ man rsync | grep -- '--exclude'
   --exclude=PATTERN        exclude files matching PATTERN

$ sudo rsync -avz --exclude='/home/erwann/.cache/' '/home/erwann' '/run/media/erwann/bkup'
sending incremental file list
  • 1
    Exclude pattern matching matches from the start directory. Correct would be sudo rsync -avz --exclude='erwann/.cache/' '/home/erwann' '/run/media/erwann/bkup' to extend it to multiple dirs, either work with pattern-matchin in the exclude or take a look at --exclude-from
    – stoney
    Apr 24 at 5:34
  • @stoney case closed.
    – Erwann
    Apr 24 at 5:35
  • @stoney Note that your example would also hide /home/erwann/.cache/erwann/.cache, if that directory existed.
    – Kusalananda
    Apr 24 at 5:37
  • @Kusalananda i guess that is what he wanted. or did you mean /home/erwann/whatever/erwann/.cache?
    – stoney
    Apr 24 at 5:40
  • @stoney Yes, that's what I meant. Any erwann/.cache directory beneath the source path.
    – Kusalananda
    Apr 24 at 5:44

1 Answer 1


Patterns consider the bit of the pathname that isn't part of the source path. This means that to match the directory .cache directly under the source path /home/erwann, you would use /erwann/.cache/ as the pattern (since the /home directory is what rsync uses as "transfer-root"):

rsync -av --exclude=/erwann/.cache/ /home/erwann /run/media/erwann/bkup

By anchoring the exclusion pattern at the top of the path with / at the start, we avoid matching it in lower parts of the file hierarchy.

If you add / at the end of the source path, /home/erwann becomes the "transfer-root", and the pattern can therefore be shortened into /.cache/. However, we now also have to add erwann to the destination path to get the same effect as before:

rsync -av --exclude=/.cache/ /home/erwann/ /run/media/erwann/bkup/erwann

From the rsync manual:

If you think of the transfer as a subtree of names that are being sent from sender to receiver, the transfer-root is where the tree starts to be duplicated in the destination directory. This root governs where patterns that start with a / match.

  • 1
    @Erwann Make sure the individual patterns in the brace expansion are quoted. Make sure that there is no whitespace after the commas in the brace expansion.
    – Kusalananda
    May 8 at 13:37

You must log in to answer this question.

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