Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to exclude files in rsync?

Writer Mia Lopez

I'm using ubuntu server 12.04, now I want to backup some files using rsync, here is a try:

rsync -aAX $HOME/Documents/* $HOME/Backups/TEST --exclude={$HOME/Documents/another/*,$HOME/Documents/temp/*} 

As you can see, I want to backup all files in the folder $HOME/Documents to folder $HOME/Backups/TEST, but exclude files in folder another and temp. But I failed, rsync still copied the files in both excluded folders:

ls $HOME/Backups/TEST/another
test

test is a file in the folder another, and it is also copied though I exclude the file in rsync, why? How to let those files be actually excluded?

2 Answers

You may find it easier to add .rsync-filter files in your source directories, and use the -F option.

From the man page:

-F : The -F option is a shorthand [...] for this rule:

 --filter='dir-merge /.rsync-filter'

This tells rsync to look for per-directory .rsync-filter files that have been sprinkled through the hierarchy and use their rules to filter the files in the transfer.

For example: in $HOME/Documents/.rsync-filter

# you can add comments in filter files
- /another/
- /temp/

Instead of -, you can also write the whole word exclude.

Now you can

rsync -aAX -F $HOME/Documents/ $HOME/Backups/TEST/

There are several issues with your rsync command (also see manpage for rsync for detailed explanation of filter rules).

  • we need an --exclude command each for any given pattern.
  • paths given need to be relative to the source path (no absolute paths).
  • options need to be given before we state source and destination.

For your example the following syntax will work:

rsync -avAX --exclude=another/ --exclude=temp/ ~/Documents/ ~/Backups/TEST

Note that if an exlude pattern ends with / it refers to a directory. If you omit it both, files and directories with that name will be excluded. Replace it with a wildcard * to exclude all files or directories with that string in their names.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy