Marcel Krüger

How to Rsync-Exclude

Featuring /dir/ /dir dir/ und dir*

22. Feb. 2020

Here are some examples of how to exclude stuff with rsync. I hope this post helps you to exclude what exactly you want and nothing more and nothing less without going mental. A few rsync calls are shown below, which are copy from the source directory src into a target directory dst.

The source directory looks like this:

src:
├── dir1
│   └── dir11
│       ├── file1
│       └── file2
├── dir2
│   ├── dir1
│   └── dir21
├── file1
└── file2

The directory views in this post were created with the command line tool tree

Ordner ausschließen

The command rsync -a --exclude '/dir1' src/ dst creates the following in the target directory:

dst:
├── dir2
│   ├── dir1
│   └── dir21
├── file1
└── file2

dir1 in the working directory was omitted. / dir2 / dir1 is still there. Ergo: The / at the beginning of /dir1 leads to that of the current directory is assumed.

rsync always starts from the current working directory. So --exclude '/dir1' does not cause folders or files named dir1 from subdirectories are ignored.

Furthermore, it does not mean that the root directory / of the system is searched.

Appending a / to the source directory src means that only the content of the source directory is copied, not the directory itself.

So it makes a difference whether you call rsync -a src dst orrsync -a src/ dst.

Here is the target directory if / is omitted. Executing rsync -a --exclude 'dir1' src/ dst results in:

dst:
├── dir2
│   └── dir21
├── file1
└── file2

Note that dir1 is missing in both, the working and subdirectories.

Include folder, exclude content

Folders can be synchronized using --exclude '/dir1/*'. Their content will be excluded. Executing rsync -a --exclude 'dir1/*' src/ dst leads to:

dst:
├── dir1
├── dir2
│   ├── dir1
│   └── dir21
├── file1
└── file2

Exclude files or folders

Like folders, files are of course excluded from the patterns specified with --exclude. Example: --exclude 'file*' excludes files and folders starting with file. Executing rsync -a --exclude 'file*' src/ dst results in:

dst:
├── dir1
│   └── dir11
└── dir2
    ├── dir1
    └── dir21

--exclude 'file*/' would only exclude folders beginning with the string file.

Exclude multiple patterns

Of course, --exclude can also be applied several times: rsync -a --exclude '/dir1' --exclude '/dir2' src/ dst

Exclude a lot

If you have a lot to exclude, you can use the parameter --exclude-from 'exclude-list.txt'. In this example you can use the file exclude-list.txt holding the exclude patterns you desire. Each exclude pattern is to be specified in a separate line. Comments can be started with #.

Example of an exclude-list.txt:

/dir1
/dir2/*
file1

A good exclude list for linux home directories can be found here.