How to Exclude Files and Directories From rsync

If you have some files or folders that you want rsync to ignore, it’s simple enough to exclude them from a transfer by adding an extra option to the rsync command. In this guide, we show command examples for excluding files and/or directories in an rsync transfer.

The rsync command expects you to specify your exclusions using relative path names (that is, relative to the source or destination directories). It won’t work correctly if you specify an absolute path to exclude.

Example 1. To exclude a file or directory in rsync, use the --exclude option. You can wrap the directories in quotes if they contain spaces or special characters. Otherwise, feel free to omit the single or double quotes. Personally, I include them anyway because it makes the command a little easier to read.

$ rsync -av --exclude 'dir1' /src/ /dst/

Example 2. If you have multiple files or directories that you wish to exclude, just append more --exclude options for each one.

$ rsync -av --exclude 'dir1' --exclude 'logs/access.log' /src/ /dst/

Example 3. You can use wildcards in your exclusions as well, if you want to omit files based on a pattern.

$ rsync -av --exclude 'dir1/*.tmp' /src/ /dst/

Example 4. To avoid putting --exclude over and over again, you can just use it once and separate each exclusion by commas, while wrapping them all in { }.

$ rsync -av --exclude={'dir1/*.tmp','logs/access.log','journal.txt'} /src/ /dst/

Example 5. There’s also the --exclude-from option, which allows us to specify a plaintext file that contains all the files and directories we’d like to exclude.

$ rsync -av --exclude-from 'exclusions.txt' /src/ /dst/

The contents of exclusions.txt would just have one file or directory per line, like so:


Example 6. If writing a lot of exclusions into an rsync Bash script, it’s a lot easier to manage the whole list of exclusions if you set up a variable. Something like this would work well:


my_excludes="--exclude linuxnightly/*.tmp \
--exclude media/ISOs \
--exclude logs/access.log \
--exclude dir1/*.tmp"

rsync -av $my_excludes /src/ /dst/

This should be all the examples you need in order to exclude files or directories by their name or pattern. We can also use a separate text document that has a list of exclusions, or organize a Bash script with the exclusions as a variable, as we’ve seen here.

Leave a Comment

Your email address will not be published. Required fields are marked *