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
Top 4 Reasons for Node Reboot or Node Eviction in Real Application Cluster (RAC) Environment
Viewed 72064 times since Thu, Jun 21, 2018
VMWare tools free
Viewed 8037 times since Mon, Jul 16, 2018
RHEL: udev rules basics
Viewed 7454 times since Sat, Jun 2, 2018
Tunnel SSH Connections Over SSL Using ‘Stunnel’ On Debian 7 / Ubuntu 13.10
Viewed 2410 times since Fri, Sep 28, 2018
Zabijanie wszystkich procesów użytkownika
Viewed 2251 times since Thu, May 24, 2018
How to run command or code in parallel in bash shell under Linux or Unix
Viewed 2623 times since Tue, Aug 6, 2019
3 Ways to Check Linux Kernel Version in Command Line
Viewed 10960 times since Fri, Apr 19, 2019
RHEL: Crash kernel dumps configuration and analysis on RHEL 6
Viewed 4152 times since Sat, Jun 2, 2018
Fake A Hollywood Hacker Screen in Linux Terminal linux FUN
Viewed 4533 times since Thu, Apr 18, 2019
How to do a Filesystem Resize (ext3/ext4) on Redhat running on VMware
Viewed 10619 times since Wed, Jul 25, 2018