256

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?

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

3 Answers 3

328

To exclude multiple directories you just use multiple --exclude=path switches. So the command from the question 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.

10
  • 10
    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, 2016 at 19:43
  • 15
    FYI: The option -a includes -r so -ar is redundant.
    – David
    Aug 8, 2017 at 14:01
  • 10
    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, 2017 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, 2017 at 16:25
  • 3
    If you're dumb then so am I and 179 other upvoters! Sep 22, 2018 at 4:48
151

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):

.ccache
build
.java
.gvfs
.xsession-errors

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.

2
  • 13
    It's important to note that root folder means the root of the transfer not the root directory.
    – jamesdlin
    Sep 10, 2018 at 20:12
  • /var/spool/lpd/cf excludes only the filename cf. To exclude all filenames starting with cf, you need to use /var/spool/lpd/cf*. And note: /var/spool/lpd/cf even excludes the dir name cf in this subdir.
    – mgutt
    Mar 8, 2022 at 5:37
133

You can also exclude multiple paths within curly braces:

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

You must log in to answer this question.

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