How to Exclude Files And Directory Using Rsync
Rsync (remote sync) is a powerful command-line application for synchronizing local and remote files and directories. It comes preinstalled on most Linux distributions. It is one of the most powerful applications for backup and keeping files and folders on many sites in sync. The most pleasing aspect of sync is that it reduces the amount of data that is duplicated to the distant site by just transferring the data that has been modified. There is another excellent feature of rsync that we are going to examine today; it is omitting files or directories from sync. This is incredibly beneficial during backups when you don’t want to duplicate one or more files or directories.
We will show you how to exclude a file or directory in sync using several examples. The examples shown here have been tried on Ubuntu 20.04 LTS. However, they are equally applicable for other Linux distributions having rsync installed.
Exclude a Specific File
While syncing a directory, you may wish to exclude a particular file placed inside it. You may do so using the –exclude option followed by the filename surrounded in commas.
The syntax of the command will be:
$ rsync -a —exclude ‘file name’ source directory/ destination directory/
Here, an option is utilized for recursive synchronization.
For instance, to exclude a file called sample.txt from the source while synchronizing 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 (with its subdirectories) from the source while synchronizing 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 called sampledir while synchronizing the src dir and dest dir, the command would be:
$ rsync -a —exclude ‘sampledir’ src dir/ dest dir/
If you wish 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 transfer the directory to the destination but not its content.
Exclude Multiple Files or Directories
To exclude several files or directories during a sync operation, define each of them as follows:
$ rsync -a —exclude 'file name' —exclude 'directory1 —exclude 'directory2' src dir/ dest dir/
Instead of supplying –exclude option individually for each file or directory, you may use a single –exclude option by putting all the files or directories in curly brackets.
$ rsync -a —exclude={'file name' ,'directory1’,'directory2'} src dir/ dest dir/
Another approach to exclude several files or directories is by listing them in a file and then supplying the filename to the –exclude-from option:
$ rsync -a —exclude-from ‘list’ src dir/ dest dir/
Here, the “list” comprises the file and folders name that we wish to exclude. This command syncs src dir to the dest dir while omitting the files and directories indicated in the “list” file.
Exclude Files or Directories that Match a Pattern
With rsync, you may also exclude files or folders that match a specified pattern. For example, when synchronizing a directory, you may wish to exclude any files ending with a .txt suffix. The command, in this situation, would be:
$ rsync -a —exclude ‘*.txt’ src dir/ dest dir/
Exclude a File by Size
With rsync, you may exclude files depending on their minimum or maximum size. Here, we will not utilize –exclude option, but the –max-size=<size in MB> or –min-size==<size in MB> options depending on the maximum and minimum size, respectively.
Let’s imagine we want to sync all the files to the dest dir, excluding those with a size of more than 100MB. In this situation, the command would be:
$ rsync -av —max-size=100m src dir/ dest dir/
Similarly, to exclude files that are lower than a specified 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! This article has examined several instances to exclude a file or a directory in sync. We have shown how to exclude a single file or directory, multiple files, and directories, files that fit a specified pattern, depending on their minimum/maximum sizes.