How to Exclude Files and Directories in Rsync

Rsync is a fast copying tool that makes it easy to copy files and directories between two systems. In this guide we’ll learn how to exclude files and directories in Rsync.

Exclude Files and Directories in Rsync

Specific File

To exclude any sort of files you don’t want to be copied or synced you have to use the --exclude flag followed by the file name:

rsync --exclude 'document.pdf' . . .

Multiple Files

Similar to the a singular file, but, in this case we can specify all the file we would like to exclude by grouping them, separated by commas, in curly brackets {}:

rsync --exclude={'document.pdf','important.txt'} . . .
example

rsync --exclude={'demo.txt','demo2.txt'} -apv /home/linuxify/Desktop/ /home/linuxify/

Multiple Files from TXT file list

Alternatively, you write down all the file names you’d like to exclude in a .txt file, then we can use the --exclude-from flag instead of the --exclude flag to specify the .txt file with all the exclusions:

exclude.txt

file1.pdf
documents.pdf
demo1.txt
rsync --exclude-from='exclude.txt' . . .

Specific File Extension or Pattern

You can also exclude files based on their extension or pattern:

Excluding .pdf files

rsync --exclude '*.pdf' . . .

Specific Directory

Similar to the specific file, we need to use the –exclude flag:

rsync --exclude 'directory' . . .

In case you want to copy the directory but not its content:

rsync --exclude 'directory/*' . . .

Multiple Directories

rsync --exclude={'directory1','directory2'}

Multiple Directories from TXT file list

Similar to the file exclusion from the .txt file list. We need to use the –exclude-from flag to specify the .txt file with all the directories we would like to skip or exclude:

exclude.txt

school
friends
family
rsync --exclude-from='exclude.txt' . . .

Summary

In this guide we went through the different scenarios on how you can exclude files and directories in Rsync.

Leave a Comment