Exclude multiple files and directories with rsync

Exclude multiple files and directories with rsync

Rsync is very powerful tool to take backups, or sync files and directories between two different locations (or servers).

In a typical backup situation, you might want to exclude one or more files (or directories) from the backup. You might also want to exclude a specific file type from rsync. Following are some examples on how to do this:

1. EXCLUDE A SPECIFIC DIRECTORY

If you don’t want to sync the dir1 (including all it’s subdirectories) from the source to the destination folder, use the rsync –exclude option as shown below.

$ rsync -avz --exclude 'dir1' source/ destination/

2. EXCLUDE MULTIPLE DIRECTORIES THAT MATCHES A PATTERN

The following example will exclude any directory (or subdirectories) under source/ that matches the pattern “dir*”

$ rsync -avz --exclude 'dir*' source/ destination/

3. EXCLUDE A SPECIFIC FILE

To exclude a specific file, use the relative path of the file in the exclude option as shown below.

$ rsync -avz --exclude 'dir1/somefile.txt' source/ destination/

4. EXCLUDE A SPECIFIC FILE TYPE

To exclude a specific file type that has a specific extension, use the appropriate pattern. For example, to exclude all the files that contains .txt as extension, do the following.

$ rsync -avz --exclude '*.txt' source/ destination/

5. EXCLUDE MULTIPLES FILES AND DIRECTORIES AT THE SAME TIME

When you want to exclude multiple files and directories, you can always specify multiple rsync exclude options in the command line as shown below.

$ rsync -avz --exclude file1.txt --exclude dir3/file4.txt source/ destination/

So, the better way is to use rsync –exclude-from option as shown below, where you can list all the files (and directories) you want to exclude in a file.

First, create a text file with a list of all the files and directories you don’t want to backup. This is the list of files and directories you want to exclude from the rsync.

$ vim exclude-list.txt
file1.txt
dir3/file4.txt

Next, execute the rsync using –exclude-from option with the exclude-list.txt as shown below.

$ rsync -avz --exclude-from 'exclude-list.txt' source/ destination/
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
How to maximise SSD performance with Linux
Viewed 138 times since Fri, May 15, 2020
How to mount software RAID1 member using mdadm
Viewed 443 times since Wed, Oct 3, 2018
Linux – How to check the exit status of several piped commands
Viewed 1151 times since Wed, Jul 25, 2018
20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management YUM
Viewed 2221 times since Thu, Oct 25, 2018
A tcpdump Tutorial and Primer with Examples
Viewed 1394 times since Sun, Jun 17, 2018
How setting the TZ environment variable avoids thousands of system calls
Viewed 1093 times since Mon, May 21, 2018
chrt command: Set / Manipulate Real Time Attributes of a Linux Process
Viewed 309 times since Mon, Feb 17, 2020
Using renice and taskset to manage process priority and CPU affinity with Linux OEL 6.4
Viewed 318 times since Mon, Feb 17, 2020
RHEL: GPT/MBR partition tables (using disks larger than 2 TiB)
Viewed 956 times since Sun, May 27, 2018
How to enable Proxy Settings for Yum Command on RHEL / CentOS Servers
Viewed 731 times since Thu, Jul 19, 2018