183

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?

  • 4
    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
236

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/
  • 7
    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. – Soren Bjornstad Aug 20 '16 at 19:43
  • 5
    FYI: The option -a includes -r so -ar is redundant. – David Aug 8 '17 at 14:01
  • 4
    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
  • 3
    FYI for later travellers, it looks like the comment in question by @FreeSoftwareServer has been removed. tl;dr: use relative paths – Freedom_Ben Sep 6 '17 at 16:25
  • 2
    If you're dumb then so am I and 179 other upvoters! – Sridhar Sarnobat Sep 22 '18 at 4:48
107

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.

  • 4
    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! – Alexander Zhukov Aug 29 at 9:04
85

You can also exclude multiple paths within curly braces:

rsync -arv --exclude={.ccache,build} /home/ben /media/ben/thumbdrive/
  • 40
    this works nicely, just not under sh, only under bash, I found out "the hard way" – xor Oct 1 '15 at 9:56
  • 7
    No space after the comma. Learnt that the hard way too 😭 – styl3r May 25 '18 at 5:21
  • And don't use '' like --exclude='.gvfs' – Martin T. Nov 21 '18 at 9:03
  • FWIW, I think curly braces work with ksh. For other shells, you could probably use something like: IFS=$'\n'; rsync -arv $(printf '.ccache\nbuild\n' | sed 's/^/--exclude=/g') /home/ben /media/ben/thumbdrive/. Or, you could just use multiple --exclude arguments, which you'd have to type manually. – TSJNachos117 Jan 24 at 0:27
  • The reason this works is because of "Brace Expansion", in case anyone wanted to look at the details. – shredalert Jun 5 at 12:50

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.