×
rsync exclude - tutorial and howto

Rsync is a very powerful tool to backup, copy and transfer files and directories between two locations and servers, much like the SCP Command.

In some cases, you need to ensure Rsync Excludes some files and directories from a transfer or backup.

By default, rsync syncs everything.

So you can use the rsync –exclude option to exclude your required directories and files from sync.

In this tutorial, we will show you how to exclude files and directories with rsync through different examples.

Create a Directory Structure

For testing purposes, you will need to create a directory structure in your system.

You can create it with the following command:

mkdir -p source/d1/d2
mkdir -p source/d3/d4
mkdir -p source/d5
touch source/f1.txt
touch source/f2.txt
touch source/f3.txt
touch source/d1/d2/f4.txt
touch source/d5/f5.txt
touch source/d3/f6.txt

You can see your directory structure with the following command:

tree source

You should see the following screen:

Exclude a Specific File and Directory

You can exclude a specific file and directory using –exclude option with rsync.

For example, to exclude d3 directory from the source directory to destination directory run the following command:

rsync -avrz --exclude=d3 source/ destination

You should see the following output:

sending incremental file list
created directory destination
./
f1.txt
f2.txt
f3.txt
d1/
d1/d2/
d1/d2/f4.txt
d5/
d5/f5.txt
sent 460 bytes received 168 bytes 1,256.00 bytes/sec
total size is 0 speedup is 0.00

To exclude the file f1.txt and f4.txt from the source directory, run the following command:

rsync -avz --exclude=f1.txt --exclude=d1/d2/f4.txt source/ destination

You should see the following output:

sending incremental file list
created directory destination
./
f2.txt
f3.txt
d1/
d1/d2/
d3/
d3/f6.txt
d3/d4/
d5/
d5/f5.txt
sent 465 bytes received 157 bytes 1,244.00 bytes/sec
total size is 0 speedup is 0.00

Exclude a Specific file Type

You can also exclude a file with a specific extension.

For example, to exclude all the files with .txt extension run the following command:

rsync -avz --exclude=*.txt source/ destination

You should see the following output:

sending incremental file list
created directory destination
./
d1/
d1/d2/
d3/
d3/d4/
d5/
sent 201 bytes received 73 bytes 548.00 bytes/sec
total size is 0 speedup is 0.00

Exclude multiple directories that match a pattern

You can also exclude any directories that match with a specific pattern.

For example, exclude all the directory under source that starts with the letter “d”:

rsync -avrz --exclude=d* source/ destination

You should see the following output:

sending incremental file list
created directory destination
./
f1.txt
f2.txt
f3.txt
sent 234 bytes received 110 bytes 688.00 bytes/sec
total size is 0 speedup is 0.00

Exclude multiple files and directories

You can also exclude multiple files and directories at the same time.

You can achieve it using the option –exclude-from with Rsync.

First, create a file that contains the path of all files and directories that you want to exclude:

nano file-list.txt

Add the following lines:

d1/d2/f4.txt
d3/d4/f6.txt
f1.txt

Save and close the file when you are finished.

Then, run the following command using –exclude-from option with the file-list.txt as shown below:

rsync -avrz --exclude-from=file-list.txt source/ destination

You should see the following output:

sending incremental file list
created directory destination
./
f2.txt
f3.txt
d1/
d1/d2/
d3/
d3/f6.txt
d3/d4/
d5/
d5/f5.txt
sent 465 bytes received 157 bytes 1,244.00 bytes/sec
total size is 0 speedup is 0.00

Exclude Specific Size File

In some cases, you need to exclude files based on their size.

For example, exclude files bigger than 1 MB size using the option –max-size with Rsync:

rsync -avrz --max-size=1m source/ destination

You should see the following output:

sending incremental file list
created directory destination
./
f1.txt
f2.txt
f3.txt
d1/
d1/d2/
d1/d2/f4.txt
d3/
d3/f6.txt
d3/d4/
d5/
d5/f5.txt
sent 581 bytes received 195 bytes 1,552.00 bytes/sec
total size is 0 speedup is 0.00

To exclude files smaller than 20 KB using the option –min-size with Rsync:

rsync -avrz --min-size=20k source/ destination

You should see the following output:

sending incremental file list
created directory destination
./
d1/
d1/d2/
d3/
d3/d4/
d5/
sent 357 bytes received 73 bytes 860.00 bytes/sec
total size is 0 speedup is 0.00

Conclusion

In the above tutorial, we’ve learned what the Rsync Exclude files and directories from copying in Linux.

These options are very useful in the day-to-day backup process in order to save time and save disk space.