How to Exclude Files and Directories in Linux Rsync
- Exclude All Files With a Specified Extension in Linux
-
Use the
-exclude
Option to Exclude a Specific Directory in Linux - Exclude Multiple Directories in Linux
The rsync
is a powerful command-line tool for synchronizing files and directories between two sites using a remote shell.
With the rsync
command, you can copy data and files between systems and make additional backups.
Also, you can exclude one or more files or folders from a copy of the data depending on the names and locations.
Exclude All Files With a Specified Extension in Linux
A file with a specified extension can also be excluded. Run the following command to exclude
all files with the .txt
extension, for example:
$ rsync -avz --exclude=*.txt source/ destination
Output:
sending incremental file list
thanosdir2/thanosdir3/
sent 203 bytes received 20 bytes 446.00 bytes/sec
total size is 0 speedup is 0.00
Use the -exclude
Option to Exclude a Specific Directory in Linux
The sync or copy operation can also be disallowed from a specified directory. The directory name is specified using the -exclude
option.
We’ll exclude the directory thanosdir
from the list in the example below.
$ rsync -avrz --exclude=thanosdir3 source/ destination
Output:
sending incremental file list
created directory destination
./
thanos.txt
thanos1.txt
thanos2.txt
thanosdir/
thanosdir/thanosdir1/
thanosdir2/
thanosdir4/
sent 391 bytes received 130 bytes 1,042.00 bytes/sec
total size is 0 speedup is 0.00
Exclude Multiple Directories in Linux
You can also filter out any directories that fit a particular pattern. For example, any directories under source that begin with the letter t
should be excluded:
$ rsync -avrz --exclude=t* source/ destination
Output:
sending incremental file list
sent 56 bytes received 12 bytes 136.00 bytes/sec
total size is 0 speedup is 0.00
All the directories with the letter t
are excluded, as seen in the output.