I am trying to backup my home directory using rsync, and I want to exclude some of the directories that contain junk. I want to specifically exclude /home/ben/.ccache and /home/ben/build. Unfortunately the documentation for rsync was information overload and didn't answer my question. This is what I tried:

rsync -arv --exclude "/home/ben/.ccache:/home/ben/build" /home/ben /media/ben/thumbdrive/

What is the right way to do this?

  • 6
    Just wanted to say that the flag a implies r already :) -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
    – Populus
    Apr 17 '15 at 13:32

OK I feel really dumb. Before I even posted this question my friend showed me how to do it and it is really simple. To exclude multiple directories you just use multiple --exclude=path switches. So my command above properly written is as follows:

rsync -arv --exclude=.ccache --exclude=build /home/ben /media/ben/thumbdrive/

Note: Use relative paths with exclude. The paths are relative to the source directory, here /home/ben.

  • 9
    An absolute path did not work for me (the directory specified was silently not excluded), whereas a path relative to the source directory worked correctly. Maybe I'm missing something, but I would be cautious about @FreeSoftwareServers' assertion that absolute paths always work. Aug 20 '16 at 19:43
  • 11
    FYI: The option -a includes -r so -ar is redundant.
    – David
    Aug 8 '17 at 14:01
  • 9
    Contrary to what FreeSoftwareServers believes, the exclude path is relative to the source path, NOT /. So that comment is categorically false and will lead to failures if you follow it. The best strategy is to leave off the leading / in your excludes to avoid this type of mental error. If source is say, /home/me and if you exclude='/mydir/' this excludes the directory mydir in /home/me, that is, it excludes /home/me/mydir NOT /mydir/ at /. It's unfortunate I can't either downvote or edit that comment since it's technically wrong and will mislead.
    – Lizardx
    Sep 6 '17 at 0:09
  • 6
    FYI for later travellers, it looks like the comment in question by @FreeSoftwareServer has been removed. tl;dr: use relative paths Sep 6 '17 at 16:25
  • 3
    If you're dumb then so am I and 179 other upvoters! Sep 22 '18 at 4:48

When having multiple directories and/or files to exclude, make a text file and use the --exclude-from switch. Make a file called exclude_me.txt and in it list your exclusions. Example (/home/ben/exclude_me.txt):


Then your rsync would look something like this:

rsync -arv --exclude-from='/home/ben/exclude_me.txt' /home/ben /media/ben/thumbdrive/

This is some information on filter rules that may help:

  • /dir/ means exclude the root folder /dir
  • /dir/* means get the root folder /dir but not the contents
  • dir/ means exclude any folder anywhere where the name contains dir/

  • Examples excluded: /dir/, /usr/share/directory/, /var/spool/dir/ /var/spool/lpd/cf means skip files that start with cf within any folder within /var/spool/lpd

Also see the filter rules section on the man page.

  • 10
    It's important to note that root folder means the root of the transfer not the root directory.
    – jamesdlin
    Sep 10 '18 at 20:12
  • This is a brilliant answer! Aug 29 '19 at 9:04
  • 3
    Finally someone explained the difference between /dir/ and dir/ in rsync's exclude option +1
    – 4levels
    Oct 14 '19 at 15:54

You can also exclude multiple paths within curly braces:

rsync -arv --exclude={.ccache,build} /home/ben /media/ben/thumbdrive/
  • 51
    this works nicely, just not under sh, only under bash, I found out "the hard way"
    – xor
    Oct 1 '15 at 9:56
  • 16
    No space after the comma. Learnt that the hard way too 😭
    – styl3r
    May 25 '18 at 5:21
  • 1
    And don't use '' like --exclude='.gvfs'
    – Martin T.
    Nov 21 '18 at 9:03
  • 2
    The reason this works is because of "Brace Expansion", in case anyone wanted to look at the details.
    – shredalert
    Jun 5 '19 at 12:50
  • 2
    It also works to have brace expansion in the middle of a path, like --exclude=/folder/{one,two}/*
    – mbomb007
    Sep 10 '19 at 21:03

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.