Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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
up vote 61 down vote accepted

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/
share|improve this answer
8  
The exclude is relative to the "source" directory I believe, so those excludes should really be: --exclude '.ccache' --exclude 'build'. The way they are written above, they would be excluding: "/home/ben/home/ben/.ccache" which is not what is wanted. – Seanchán Dec 1 '13 at 22:38
    
Yes you're right! I fixed the example. Thanks! – Freedom_Ben Dec 2 '13 at 2:52
    
I believe using Absolute Paths will always work and is preferred IMO, this way you don't need to be inside the correct directory when running the command, for example if you wanted to use it in conjunction with Rsync as I plan to. – FreeSoftwareServers Sep 5 '15 at 2:26

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:

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.

share|improve this answer
    
Good tip, thanks! +1 – Freedom_Ben May 19 '14 at 22:01
3  
Thanks, +1, but you missed = makes it fail in --exclude-from. Should be --exclude-from=. – Ilia Rostovtsev Feb 12 '15 at 12:35

You can also exclude multiple paths within curly braces:

rsync -arv --exclude={.ccache,build} /home/ben /media/ben/thumbdrive/
share|improve this answer
6  
this works nicely, just not under sh, only under bash, I found out "the hard way" – xxor Oct 1 '15 at 9:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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