Rsync exclude directory (folder), files in Linux with examples

rsync
Rsync exclude directory (folder), files in Linux with examples


Rsync exclude directory (folder), files Introduction

Rsync exclude directory (folder), files in Linux article includes how to exclude files and directories which are not required while taking backup with rsync.
Rsync commands is used to take backup, copy files and directories in a remote server.
You may encounter many situations when you wish to exclude directories and files while taking backups with rsync.
This article focusses on step by step to exclude directory and files while using the tool rsync.

You should also check out article for Rsync alternative for Windows system as it requires if the backup task involves between two different OS (Linux and Windows).

Create a directory, sub-directory and files to test

Create a directory

$ mkdir TestDirectory

Change to TestDirectory

$ cd TestDirectory

Under TestDirectory create three sub directories

$ mkdir directory1 directory2 directory3

Create file inside the three sub-directories

$ touch directory1/file1.txt
$ touch directory2/file2.txt
$ touch directory3/file3.txt

Change directory . In the next step we will exclude directory2 and its files while copying.

$ cd ~
Rsync exclude specific directory

Run rsync command using option --exclude to exclude a specific directory.

$ rsync -av --exclude 'directory2' TestDirectory/ path/
sending incremental file list
created directory path
./
directory1/file1.txt
directory3/file3.txt
sent 248 bytes  received 76 bytes  648.00 bytes/sec
total size is 0  speedup is 0.00

Verify that 'directory2' and its files is not copied to the destination

$ find path
path
path/directory1/file1.txt
path/directory3/file3.txt
Rsync exclude multiple directory

Rsync can be used to exclude multiple directory which matches a pattern. Lets see below example which exclude directories which matching pattern 'pos*'

Create two directories "pos1" and "pos2" under "TestDirectory"

$ cd TestDirectory
$ mkdir pos1 pos2

Run rsync to exclude directory 'pos1, ' pos2'

$ rsync -av --exclude 'pos*' TestDirectory/ path/
Rsync exclude directory without using patern

To exclude directory with pattern matching is good. But what if the directories doesn't matches pattern. The curly braces with exclude option comes as life saver in this scenario. See the below example.

$ rsync -arv --exclude={directory1,pos1} TestDirectory/ path/
sending incremental file list
created directory path
./
directory2/file2.txt
directory3/file3.txt
sent 161 bytes received 53 bytes 428.00 bytes/sec
total size is 0 speedup is 0.00
Rsync to exclude specific file

For example if there is any file named 'file3.txt' inside the directory 'TestDirectory' and its sub-directory can be excluded by the following.

$ rsync -av --exclude 'file3.txt' TestDirectory/ path/
sending incremental file list
created directory path
./
directory1/file1.txt
directory2/file2.txt
directory3/
sent 311 bytes  received 84 bytes  790.00 bytes/sec
total size is 0  speedup is 0.00

But there can be more than one file with similar name in its other sub-directory. In that case you can choose relative path to exclude specific file. Lets look at an example.

$ rsync -av --exclude '/directory3/file3.txt' TestDirectory/ path/
Rsync exclude multiple files

If you want to exclude more that one file at a time then see the given example where two exclude option is used.

$ rsync -av --exclude 'file1.txt' --exclude 'file3.txt' TestDirectory/ path/
sending incremental file list
created directory path
./
directory1/
directory2/file2.txt
directory3/
sent 244 bytes received 65 bytes 618.00 bytes/sec
total size is 0 speedup is 0.00
Rsync exclude from option

However, in many cases you may have to exclude 100 - 200 of files. In such cases using exclude for every single file is really a bad idea. So what will you do? Create a file 'exclude.txt', you can give any other name as well if you want. Write the name of each file inside 'exclude.txt' you want to exclude. To be more careful give the relative path of each file. For example.

$ vi exclude.txt

directory1/file1.txt
directory2/file2.txt

save the file, press Escape then :wq

Run rsync command as given below.

$ rsync -av --exclude-from 'file3.txt' TestDirectory/ path/

For more options to use with rsync search the man page

$ man rsync

This is it with the article Rsync exclude directory (folder), files in Linux with examples

See also:
Rsnapshot - Rsync based utility to take backup in Linux and Unix
Rsync passwordless ssh login in Linux and UNIX

Be the first to comment

Leave a Reply

Your email address will not be published.


*


shares