How to Exclude Files and Directories with rsync?

In Linux, the “rsync” is a powerful utility tool for synchronizing files and directories from one place to another. In certain situations, you must make a backup of all important data, excluding some useless/least important files and directories. Excluding files/directories from bulk data is a tedious task. However, it can be simplified by using the rsync command.

This post will demonstrate the method to exclude files and directories with rsync.

How to Exclude Files With rsync Command?

The “rsync” command is utilized with the “exclude” keyword specifying the name of the file as “f_name”. Also, it requires the address of the source as well as the destination directory:

Syntax:

$ rsync -a --exclude 'f_name' src_dir/ des_dir/

The description of the syntax is provided below:

  • f_name: It specifies the name of the file.
  • src_dir: It refers to the source directory.
  • des_dir: It refers to the destination directory.
  • exclude: The utility is used to exclude the particular file after matching the file name.

Example

Firstly, the “ls” command is used to display all content stored in the specific directory named “Dir1” as below:

$ ls Dir1

The “rsync” command is utilized to exclude a particular file named “file.txt” and specify the source and destination directories as below:

$ rsync -a --exclude 'file.txt' Dir1/ Dir2/

Now, you can verify through the “ls” command that the destination directory does not have the excluded file “file.txt”:

$ ls Dir2

How to Exclude Directories With rsync Command?

To exclude a directory before transferring data from one place to another, the “rsync” command is utilized with the “exclude” keyword. Also, it is required to specify the name of the directory as “dir_name” with the source and destination address:

Syntax:

$ rsync -a --exclude 'dir_name' src_dir/ des_dir/

Example

Let’s first list own the content of the directory named ”Dir1” (the source directory going to be synchronized):

$ ls Dir1

To exclude a specific directory, the –exclude keyword is utilized with the “rsync” command. Additionally, the name of the particular directory is required which you want to exclude before transferring:

$ rsync -a --exclude 'Dir_Info' Dir1/ Dir2/

The “ls” command is utilized to display the content present in the specific directory “Dir2”:

$ ls Dir2

You can verify that the destination directory does not contain the “Dir_info” directory, which has been excluded.

How to Exclude Multiple Files and Directories With rsync Command?

In Linux, you can exclude multiple files and directories before transferring all data from one directory.  In the following command, “file.txt” and “Dir_Info” are specified with the “exclude” keyword:

$ rsync -a --exclude  'file.txt' --exclude 'Dir_Info' Dir1/ Dir2/

To display the content in “Dir2”, the “ls” command is utilized as below:

$ ls Dir2

Verify through the “ls” command in the destination directory that one file and one directory have been successfully excluded.

How to Exclude Files of Specific Format With rsync Command?

Using the “rsync” command, users can specify the file format that will be excluded from the transferring data. Here are two files having the file type “.txt” in the “Dir1” directory as below:

$ ls Dir1

Exclude .txt File Format

Execute the below script that excludes all files having the “.txt” file format from moving from one directory to another directory:

$ rsync -a --exclude '*.txt' Dir1/ Dir2/

The “ls” command is utilized to display the content in the particular directory “Dir2”:

$ ls Dir2

You can authenticate that there are no files having the “.txt” file format in the destination directory.

How to Exclude a File or Directory of Specific Size With rsync Command?

You can exclude the specified file or directory having larger or smaller sizes. To do so, the “ls” command is used with “-l” to visualize the length of all files and directories in the specified directory:

$ ls Dir1

The “rsync” command is utilized with the “max-size” argument to assign a particular size of the file for exclusion:

$ rsync -av -max-size-10m Dir1/ Dir2/

To display the content that is present in the “Dir2” directory, execute the below script:

$ ls Dir2

You can verify via “ls” command that the “UnityHub” file is not transferred to the destination directory due to exceeding “10 MB”.

Conclusion

The “rsync” command works with the “exclude” keyword to exclude the files and directories from moving from one directory to another directory. It is quite useful to synchronize all data for backup purposes. Additionally, users can exclude the specified file format and file size when transferring data to another directory. This guide has covered all aspects of the “rsync” command to exclude files and directories in Linux systems.