Professional Documents
Culture Documents
6 Rsync Examples To Exclude Multiple Files and Directories Using Exclude-From
6 Rsync Examples To Exclude Multiple Files and Directories Using Exclude-From
You know this already, as we presented you with practical examples on rsync earlier.
In a typical backup situation, you might want to exclude one or more files (or directories)
from the backup. You might also want to exclude a specific file type from rsync.
This article explains how to ignore multiple files and/or directories during rsync with
examples.
First, create a sample directory structure as shown below (with some empty files) that can be
used for testing purpose.
$ cd ~
$ mkdir -p source/dir1/dir2
$ mkdir -p source/dir3
$ touch source/file1.txt
$ touch source/file2.txt
$ touch source/dir1/dir2/file3.txt
$ touch source/dir3/file4.txt
The above command will create a source directory (under your home directory) with the
following structure.
source
- file1.txt
- file2.txt
- dir1
- dir2
- file3.txt
- dir3
- file4.txt
If you don’t want to sync the dir1 (including all it’s subdirectories) from the source to the
destination folder, use the rsync –exclude option as shown below.
$ rm -rf destination
Verify to make sure dir1 is not copied from source directory to destination directory.
$ find destination
destination
destination/file2.txt
destination/file1.txt
destination/dir3
destination/dir3/file4.txt
The following example will exclude any directory (or subdirectories) under source/ that
matches the pattern “dir*”
$ rm -rf destination
Verify the destination directory to make sure it didn’t copy any directories that has the
keyword “dir” in it.
$ find destination
destination
destination/file2.txt
destination/file1.txt
To exclude a specific file, use the relative path of the file in the exclude option as shown
below.
$ rm -rf destination
Verify the destination directory to make sure it didn’t copy the specific file ( dir1/dir2/file3.txt
in this example).
$ find destination
destination
destination/file2.txt
destination/file1.txt
destination/dir1
destination/dir1/dir2
destination/dir3
destination/dir3/file4.txt
In the following example, the exclude option seems to have a full path (i.e /dir1/dir2/file3.txt).
But, from rsync point of view, exclude path is always relative, and it will be treated as
dir1/dir2/file3.txt. In the example below, rsync will look for dir1 under source directory (and
not under / root directory).
So, the above command is exactly same as the following. Just to avoid confusion (and to
make it easy to read), don’t give / in front of the exclude path.
To exclude a specific file type that has a specific extension, use the appropriate pattern. For
example, to exclude all the files that contains .txt as extension, do the following.
Verify the destination directory to make sure it didn’t copy the *.txt files.
$ find destination
destination
destination/dir1
destination/dir1/dir2
destination/dir3
Note: The above is very helpful, when you want to backup your home directory, but exclude
all those huge image and video files that has a specific file extension.
When you want to exclude multiple files and directories, you can always specify multiple
rsync exclude options in the command line as shown below.
I can’t keep adding them in the command line using multiple –exclude, which is hard to read,
and hard to re-use the rsync command for later.
So, the better way is to use rsync –exclude-from option as shown below, where you can list all
the files (and directories) you want to exclude in a file.
First, create a text file with a list of all the files and directories you don’t want to backup. This
is the list of files and directories you want to exclude from the rsync.
$ vim exclude-list.txt
file1.txt
dir3/file4.txt
Next, execute the rsync using –exclude-from option with the exclude-list.txt as shown below.
$ rm -rf destination
Verify the desitination directory to make sure the files and directories listed in the exclude-
list.txt file is not backed-up.
$ find destination
destination
destination/file2.txt
destination/dir1
destination/dir1/dir2
destination/dir1/dir2/file3.txt
destination/dir3