Blog Post
How to Exclude Files and Directories Using Rsync

How to Exclude Files and Directories Using Rsync

Rsync, also called Remote Sync, is a popularly used command in Linux for managing files and directories remotely and locally. Through Rsync, we can synchronize, modify, and even transfer files and directories across various servers or locations.

Consequently, Rsync also allows for excluding files and/or directories. To do this, we make use of the –exclude command. Additionally, Rsync is capable of synchronizing data changes to avoid major mismatch issues.

Before beginning to exclude files, note that you need Rsync installed in your system along with certain admin privileges to make changes. The basic syntax for the –exclude command is as follows:

rsync <options> –exclude ‘file/directory name’ source/ destination/ where ‘source/’ refers to your source of data which you want to modify, transfer, exclude, etc and ‘destination/’ refers to your target location for the changed or new data. We will use ‘-a’ command in place of <options> as it syncs data while maintaining the settings and permissions hence making it a necessary command. However, you can add more optional commands to it for additional functionalities such as ‘v’, ‘r’, ‘z’, etc.

Exclude a Particular File

When you are modifying or transferring contents of a folder, use this:

rsync -avz --exclude ‘random.pdf’ source/ destination/

It is also possible to rsync a file to a directory while excluding specific folders in it. For instance:

rsync -avz --exclude={“/link/up”,”/link/side”} link/ usergg/

Exclude a Particular Directory

When it comes to excluding a directory, it works similarly to excluding a file, except we need to replace the filename with the directory. Use:

rsync -avz --exclude ‘direc0’ source/ destination/

For removing the contents of a directory while keeping the directory itself intact, use this line of command instead:

rsync -avz --exclude ‘direc0/*’ source/ destination/

Exclude Multiple Files And/Or Directories

There are multiple ways to exclude multiple files and/or directories using rsync. One, we can simply type the –exclude command repeatedly in the same line of command. For example:

rsync -avz --exclude ‘leg.txt’ --exclude ‘direc0’ source/ destination/

We can also concatenate multiple files and directories into a single –exclude command by using curly braces (‘{}’). Type:

rsync -avz --exclude={‘leg.txt’, ‘direc0’} source/ destination/

If your files and directories happen to be in huge numbers, you can use the –exclude-from command for better efficiency of speed and memory. First, create a file that contains the names of all the files and directories needed to be excluded. Now, simply pass the file to the –exclude-from command. For instance:

rsync -avz --exclude-from=‘masterfile.txt’ source/ destination/

Exclude Files Based on their Type

With rsync, we can have greater control over modifying files. It enables users to exclude files of a specific type as well. Assuming we need to exclude all ‘.txt’ type of files, we can use this line of command for the task:

rsync -avz –exclude ‘*.txt’ source/ destination/ where the ‘*’ indicates all text files. Conversely, to exclude all other types of files except text files, we use the –include command. For example:

rsync -avz --include ‘*.txt’ --include ‘/*’ --exclude ‘*’ source/ destination/

Now this command includes only the text files and excludes the rest.

Exclude Files or Directories Based on Patterns

The commands here are similar to the ones seen above, with the ‘*’ being the primary option for specific customized commands. Users can exclude files and/or directories that start, end, or include particular alphabets, numbers, or symbols.

For example, to exclude files beginning with the letters ‘del’, use this:

rsync -avz --exclude ‘del*’ source/ destination/

Or run this line of command to exclude directories that end with ‘init’:

rsync -avz --exclude ‘*init’ source/ destination/

Exclude Files Based on Size

By using commands such as -max-size and -min-size, filtering out files is much more efficient. If you are required to exclude files exceeding 2MB of file size, run this command:

rsync -avz --max-size=2m source/ destination/

On the other hand, restricting files that are below 1MB, for instance, will require this command:

rsync -avz --min-size=1m source/ destination/

These commands are an apt method of managing storage space, speed, and memory.


The rsync feature provides one of the most efficient tools for managing files and directories by enabling customized and detailed commands to transfer, modify, and store data.

Related posts

Leave a Reply

Required fields are marked *

Copyright © 2022 All rights reserved.