SEARCH

How-To Geek

How to Exclude Files from Rsync

Rsync is one of the most useful utilities for a server administrator, but it syncs everything by default, which can be annoying if your application creates a lot of temporary files. Here’s how to exclude files when using rsync.

Excluding from a List in a File

This is the ideal method for excluding files and folders, since you can always edit the list and tweak things if necessary. Here is the syntax:

rsync --exclude-from=/path/to/exclusion-file /path/to/source /path/to/dest

The tricky thing with rsync is that you need to use a relative path when you are trying to exclude things, because when it tries to match the exclusions it won’t use the first part of the path for the match… it’s weird.

Say, for example, you are trying to backup /data/web/ and send it to another server, so you use a command like rsync -a /data/web/ user@server:/backups/data/web/ to make it happen… but you’d really like to skip syncing the /data/web/cache/ folder. When rsync goes to check your exclusion list for each item that it syncs, it won’t check /data/web/cache/ since your original rsync command is based in the /data/web/ folder. It’ll just check “cache/” against the list. So you’ll need to put “cache” into the list, not the full path. Example:

rsync -a --exclude-from=/data/exclusions /data/web/ /backups/

Now to exclude /data/web/cache and /data/web/temp from rsync using this command, we’d remove the /data/web/ portion of the path and the /data/exclusions file would simply contain this:

cache*
temp*

You’ll note that I added the * into the path, to make sure that it matches anything that starts with “cache” at the beginning. You can use this star pattern for more useful reasons if you wanted — say you want to exclude all .txt files from being synced. You’d start the pattern with the star to make sure that always matches, and add this:

*.txt

That would ensure that those types of files are skipped during a sync. It’s pretty simple beyond that.

Excluding a Single Item

This technique is much less useful, but you can use it on the fly if you need to. If you are setting up a script to use rsync, which  you usually are, you should take the extra minute to exclude from a file list instead to make future maintenance easier.  The syntax is very similar:

rsync --exclude=relative/path/to/exclusion /source /dest

The same relative path should apply here as above.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 01/16/14

Enter Your Email Here to Get Access for Free:

Go check your email!