rsync Exclude Patterns: Skip Files and Directories

By 

Updated on

7 min read

rsync exclude patterns skipping files and directories during transfer

rsync is a fast and versatile command-line utility that synchronizes files and directories between two locations. It supports mirroring data, creating incremental backups with --link-dest , and copying files between local and remote systems.

When copying data, you may want to exclude certain files or directories based on their name or location. This guide explains how to use rsync’s --exclude and --exclude-from options to skip files and directories during transfer.

The examples below use the -a (archive) option, which syncs directories recursively and preserves symbolic links, modification times, group ownership, and permissions. You should have a basic understanding of how rsync works before proceeding.

When excluding files or directories, use paths relative to the source directory. There are two ways to specify exclusions:

  • Command line: pass the --exclude option directly.
  • File: list exclusions in a file and pass it with --exclude-from.

Exclude a Specific File

To exclude a specific file, pass the relative path to the file to the --exclude option.

In the following example, the file src_directory/file.txt will not be transferred:

Terminal
rsync -a --exclude 'file.txt' src_directory/ dst_directory/

Exclude a Specific Directory

Excluding a specific directory (folder) works the same as excluding a file. Pass the relative path to the directory to the --exclude option:

Terminal
rsync -a --exclude 'dir1' src_directory/ dst_directory/

If you want to exclude the directory contents but not the directory itself, use dir1/* instead of dir1:

Terminal
rsync -a --exclude 'dir1/*' src_directory/ dst_directory/

Exclude Multiple Files or Directories

To exclude multiple files or directories, specify multiple --exclude options:

Terminal
rsync -a --exclude 'file1.txt' --exclude 'dir1/*' --exclude 'dir2' src_directory/ dst_directory/

You can also use shell brace expansion as a compact way to write multiple --exclude options:

Terminal
rsync -a --exclude={'file1.txt','dir1/*','dir2'} src_directory/ dst_directory/

Note that brace expansion is a shell feature: the shell expands the braces before rsync is invoked, so rsync still receives separate --exclude options.

If the number of files or directories to exclude is large, place them in a text file and pass it to the --exclude-from option. The following command produces the same result as the one above:

Terminal
rsync -a --exclude-from='exclude-file.txt' src_directory/ dst_directory/

The contents of exclude-file.txt would be:

exclude-file.txttxt
file1.txt
dir1/*
dir2

Exclude Files Based on a Pattern

rsync supports glob patterns in exclude rules, which allows you to exclude files by type or name structure.

For example, to exclude all .jpg files, run:

Terminal
rsync -a --exclude '*.jpg' src_directory/ dst_directory/

To do the inverse, transfer only files matching a pattern while excluding everything else, use a combination of --include and --exclude options. When using multiple include and exclude options, the first matching rule applies.

For example, to transfer only .jpg files:

Terminal
rsync -a -m --include='*.jpg' --include='*/' --exclude='*' src_directory/ dst_directory/

The options work as follows:

  • --include='*.jpg' - includes all .jpg files.
  • --include='*/' - includes all subdirectories. Without this, rsync only copies .jpg files in the top-level directory.
  • --exclude='*' - excludes everything else.
  • -m - removes empty directories from the transfer.

Another approach is to pipe the output of the find command to rsync:

Terminal
find src_directory/ -type f -name "*.jpg" -printf '%P\n' | rsync -a --files-from=- src_directory/ dst_directory/
  • -printf '%P\n' - prints each file’s path relative to src_directory/, one per line.
  • --files-from=- - tells rsync to read the list of files from standard input.

Preview the Transfer with –dry-run

Before running an rsync command with exclude rules, it is good practice to preview what will be transferred and what will be skipped. Use the --dry-run (or -n) flag together with --verbose to simulate the transfer without making any changes:

Terminal
rsync -a --dry-run --verbose --exclude '*.jpg' src_directory/ dst_directory/
output
sending incremental file list
file1.txt
dir1/notes.txt
sent 89 bytes  received 20 bytes  218.00 bytes/sec
total size is 1.23K  speedup is 11.27 (DRY RUN)

The (DRY RUN) label at the end of the output confirms that no files were actually transferred. Review the file list to verify your exclude patterns are working as expected before committing to a full transfer.

Exclude Version Control Files with –cvs-exclude

The --cvs-exclude option (or -C) automatically excludes a standard CVS-style set of temporary and backup files. This includes common build artifacts, editor swap files, and version control directories such as .git/, .svn/, .hg/, and .bzr/.

To use it, add the flag to your rsync command:

Terminal
rsync -a --cvs-exclude src_directory/ dst_directory/

Add explicit rules for project-specific paths that are not part of the built-in list, such as dependency directories, virtual environments, or build output:

Terminal
rsync -a -C --exclude 'node_modules/' --exclude '.venv/' --exclude 'dist/' src_directory/ dst_directory/

This is a convenient shorthand when synchronizing project directories where you do not want to transfer temporary files, VCS metadata, and common generated files.

rsync Filter Rules

For more advanced use cases, rsync provides --filter rules that go beyond simple --exclude patterns. Filter rules support include, exclude, protect, and risk modifiers, and can be read from files using merge rules.

For example, the following command excludes all .log files using a filter rule:

Terminal
rsync -a --filter='exclude *.log' src_directory/ dst_directory/

Filter rules follow the same first-match logic as --exclude and --include. For a full reference, see the rsync documentation .

Quick Reference

For a printable quick reference, see the Rsync cheatsheet .

TaskCommand
Exclude a single filersync -a --exclude 'file.txt' src/ dst/
Exclude a directoryrsync -a --exclude 'dir1' src/ dst/
Exclude directory contentsrsync -a --exclude 'dir1/*' src/ dst/
Exclude multiple itemsrsync -a --exclude 'a' --exclude 'b' src/ dst/
Exclude from a filersync -a --exclude-from='list.txt' src/ dst/
Exclude by patternrsync -a --exclude '*.jpg' src/ dst/
Preview without transferringrsync -a --dry-run --verbose --exclude '*.jpg' src/ dst/
Exclude VCS files automaticallyrsync -a --cvs-exclude src/ dst/

Troubleshooting

Exclude pattern has no effect
Exclude paths are relative to the source directory, not your current working directory. If you pass an absolute path such as /home/user/src/file.txt, rsync will not match it. Use just the filename or a path relative to the source, for example subdir/file.txt.

Directory exclusion does not match as expected
Check whether your pattern is anchored and whether it should match only directories. dir1 matches files or directories named dir1 anywhere in the tree. dir1/ matches directories named dir1. /dir1/ matches only a top-level dir1 under the source root.

Excluded files are deleted from the destination when using –delete
Files excluded on the source side are also protected from deletion on the destination by default. However, if you use --delete-excluded, rsync will remove files from the destination that match your exclude rules. Omit --delete-excluded to keep excluded files on the destination intact.

FAQ

How do I exclude hidden files and directories?
Use --exclude '.*' to exclude all files and directories whose names begin with a dot. To exclude only hidden directories, use --exclude '.*/'.

Why does my exclude rule not match files in subdirectories?
Unanchored patterns (without a leading /) match against any part of the path. If your pattern is not matching, try adding a leading / to anchor it to the source root. For example, --exclude '/logs' excludes only a logs directory at the top level of the source.

Will rsync delete excluded files from the destination when I use –delete?
No, not by default. Files that match an exclude rule are protected from deletion on the destination. Only add --delete-excluded if you explicitly want to remove those files from the destination.

What is the difference between –exclude and –filter?
--exclude and --include are shorthand forms of filter rules. The --filter option is more powerful and supports additional modifiers such as protect (P), risk (R), and merge-file rules. For most common use cases, --exclude is sufficient.

Does rsync have an ignore file like .gitignore?
rsync does not read a .rsyncignore file automatically, but you can get the same behavior. List your patterns in a file and pass it with --exclude-from, or use a per-directory merge filter such as --filter='dir-merge .rsync-filter' to apply the rules found in each directory rsync visits.

Conclusion

rsync’s --exclude and --exclude-from options give you precise control over what is transferred during a sync. Use --dry-run with --verbose to verify your exclude rules before running a full transfer, and use --cvs-exclude together with explicit VCS rules when needed.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page