The rsync command on a Linux system can be used to synchronize the contents of two directories. By default, rsync will transfer all files and directories over to the specified destination. If there's a subdirectory you wish to exclude from the transfer, rsync gives us two options for doing so.

In this tutorial, we'll show two methods for excluding one or multiple directories from an rsync transfer. Follow along with the example commands below on your own system to configure a directory for exclusion.

In this tutorial you will learn:
  • How to exclude a directory in rsync command
Excluding a directory from rsync transfer
Excluding a directory from rsync transfer
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software rsync
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Exclude directory in rsync

Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.

The first option we'll cover is using the --exclude option in the rsync command. Here's how it would be used to exclude the directory linuxconfig in the example command below.

$ rsync -av --exclude linuxconfig /path/to/src/ /path/to/dest/

In the command above, we are synchronizing the contents of /path/to/src with that of /path/to/dest and omitting the linuxconfig directory. Note, however, that the path to linuxconfig is relative to that of our source path, which in this case is /path/to/src. Therefore, rsync assumes that directory linuxconfig is inside of /path/to/src.

But what if the linuxconfig directory is actually a bit deeper in our source directory? Let's say that the absolute path to the directory we wish to exclude is actually /path/to/src/some/subdir/linuxconfig. In that case, our rsync command would look like this:

$ rsync -av --exclude some/subdir/linuxconfig /path/to/src/ /path/to/dest/

Now, how about multiple directories? We can use as many --exclude options as we want. Let's try excluding three directories from our rsync transfer with this command:

$ rsync -av --exclude some/subdir/linuxconfig --exclude some/other/dir --exclude exampledir /path/to/src/ /path/to/dest/

The command above will do its job as expected, but as you can see, the command grows gradually more unwieldy as we append more excluded directories to it. What if we want to exclude 15 directories? That command will be a monstrosity. But that's where rsyn'c other option --exclude-from comes into play.

The --exclude-from rsync option allows us to specify a file that contains a list of directories to be excluded. The file should be plaintext, so a simple .txt file will do. List one directory per line as you're compiling the list of directories that you want to exclude.

$ cat exclude-list.txt

With our file of exclusions created, we can now reference it in our rsyc command:

$ rsync -av --exclude-from='exclude-list.txt' /path/to/src/ /path/to/dest/

Now we are excluding the same three directories as before, but our command is much more concise and we can continue to add more directories to our exclusion file as needed.


In this article, we learned two methods for excluding one or multiple directories from an rsync command in Linux. One option is good for a small number of exclusions and the other is more appropriate when you have a long list of directories that you wish to exclude. But their core function is the same and you should use whichever is better for your backup scenario.

Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:

Comments and Discussions