Rsync is very powerful tool to take backups, or sync files and directories between two different locations (or servers).
You know this already, as we presented you with practical examples on rsync earlier.
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.
This article explains how to ignore multiple files and/or directories during rsync with examples.
First, create a sample directory structure as shown below (with some empty files) that can be used for testing purpose.
$ cd ~ $ mkdir -p source/dir1/dir2 $ mkdir -p source/dir3 $ touch source/file1.txt $ touch source/file2.txt $ touch source/dir1/dir2/file3.txt $ touch source/dir3/file4.txt
The above command will create a source directory (under your home directory) with the following structure.
source - file1.txt - file2.txt - dir1 - dir2 - file3.txt - dir3 - file4.txt
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.
$ rm -rf destination $ rsync -avz --exclude 'dir1' source/ destination/ building file list ... done created directory dest ./ file1.txt file2.txt dir3/ dir3/file4.txt
Verify to make sure dir1 is not copied from source directory to destination directory.