Linux Commands

How to Exclude Directory Rsync?

Rsync (stands for remote sync) is a very powerful command-line utility for local and remote file and directories synchronization. It comes preinstalled on most Linux distributions. It is one of the best utilities for backup and keeping files and directories on multiple locations in sync. The best part about rsync is that it minimizes the amount of data that is copied to the remote location by only copying the data that has been changed. There is another great feature of rsync that we are going to discuss today; it is excluding files or directories from sync. This is extremely useful during backups when you don’t want to copy one or more files or directories.

We will show you how you can exclude a file or directory in rsync using different examples. The examples presented here have been tested on Ubuntu 20.04 LTS, however, these are also valid for other Linux distributions having rsync installed.

Exclude a Specific File

While syncing a directory, you may want to exclude a specific file located inside it. You can do so using the –exclude option followed by the filename enclosed in commas.

The syntax of the command will be:

$ rsync -a --exclude ‘file_name’ source_directory/ destination_directory/

Here, a option is used for recursive syncing.

For instance, to exclude a file named sample.txt from the source when syncing src_dir and dest_dir, the command would be:

$ rsync -a --exclude ‘sample.txt’ src_dir/ dest_dir/

Exclude a Specific Directory

To exclude a specific directory (including its subdirectories) from the source when syncing the source and the destination directories, use the following syntax:

$ rsync -a --exclude ‘directory_name’ src_dir/ dest_dir/

For example, to exclude a directory named sampledir when syncing the src_dir and dest_dir, the command would be:

$ rsync -a --exclude ‘sampledir’ src_dir/ dest_dir/

If you want to exclude the content of the directory but not the directory itself, use directory name followed by /*:

$ rsync -a --exclude 'sampledir/* ' src_dir/ dest_dir/

The above command will only copy the directory to the destination but not its content.

Exclude Multiple Files or Directories

To exclude multiple files or directories during sync process, specify each of them as follows:

$ rsync -a --exclude 'file_name' --exclude 'directory1 --exclude 'directory2' src_dir/ dest_dir/

Instead of specifying –exclude option separately for each file or directory, you can use a single –exclude option by specifying all the files or directories in curly brackets.

$ rsync -a --exclude={'file_name' ,'directory1’,'directory2'} src_dir/ dest_dir/

Another option to exclude multiple files or directories is by listing them in a file and then passing the filename to the –exclude-from option:

$ rsync -a --exclude-from ‘list’ src_dir/ dest_dir/

Here, the “list” contains the file and directories name that we want to exclude. This command syncs src_dir to the dest_dir while excluding the files and directories mentioned in the “list” file.

Exclude Files or Directories that Match a Pattern

With rsync, you can also exclude files or directories that match a specific pattern. For example, while syncing a directory, you may want to exclude all files ending with a .txt extension. The command, in this case, would be:

$ rsync -a --exclude ‘*.txt’ src_dir/ dest_dir/

Exclude a File by Size

With rsync, you can exclude files based on their minimum or maximum size. Here, we will not use –exclude option, but the –max-size=<size in MB> or –min-size==<size in MB> options based on the maximum and minimum size, respectively.

Here is the listing of our src_dir which shows file names along with their sizes in MB.

Let’s say we want to sync all the files to the dest_dir except those with a size greater than 100MB. In this case, the command would be:

$ rsync -av --max-size=100m src_dir/ dest_dir/

Similarly, to exclude files that are smaller than a specific size, let’s say 50 MB, the command would be:

$ rsync -av --min-size=50m src_dir/ dest_dir/

Conclusion 

That is all there is to it! In this post, we have discussed different examples to exclude a file or a directory in rsync. We have shown how to exclude a single file or directory, multiple files and directories, files that match a specific pattern, and the files based on their minimum/maximum sizes.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.