Mastering rsync: How to Exclude Files and Directories Efficiently
Learn how to use rsync’s powerful exclude options—including --exclude, --exclude-from, pattern matching, and include rules—to selectively skip files or directories during synchronization, with practical command examples for single items, multiple entries, and regex-based filtering.
Introduction
Rsync is a fast and versatile command-line utility that synchronizes files and directories between two locations via a remote shell.
It can mirror data, create incremental backups, and copy files, and you may need to exclude certain files or directories based on name or location.
Preparation
Brief overview of rsync workflow and usage. The
-aoption recursively syncs directories, preserving links, timestamps, groups, ownership, and permissions.
When excluding, use paths relative to the source directory. Two options exist to specify exclusions:
Use
--excludeon the command line.
Use
--exclude-fromwith a file containing patterns.
Excluding specific files
Pass the relative path to
--exclude. Example:
<code>rsync -a --exclude 'file.txt' src_directory/ dst_directory/</code>Excluding specific directories
Pass the directory’s relative path to
--exclude. Example:
<code>rsync -a --exclude 'dir1' src_directory/ dst_directory/</code>To exclude the contents but keep the directory itself, use
dir1/*instead of
dir1:
<code>rsync -a --exclude 'dir1/*' src_directory/ dst_directory/</code>Excluding multiple files or directories
Specify multiple
--excludeoptions, or combine them with braces:
<code>rsync -a --exclude 'file1.txt' --exclude 'dir1/*' --exclude 'dir2' src_directory/ dst_directory/</code>Or use a single
--excludewith a brace list:
<code>rsync -a --exclude={'file1.txt','dir1/*','dir2'} src_directory/ dst_directory/</code>When many patterns are needed, use
--exclude-fromwith a file listing each pattern, one per line:
<code>rsync -a --exclude-from='exclude-file.txt' src_directory/ dst_directory/</code>Contents of
exclude-file.txt:
<code>file1.txt
dir1/*
dir2</code>Excluding with regular‑expression patterns
Use pattern matching to exclude groups of files, e.g., all
.jpgfiles:
<code>rsync -a --exclude '*.jpg*' src_directory/ dst_directory/</code>To copy only
.jpgfiles while excluding everything else:
<code>rsync -a -m --include='*.jpg' --include='*/' --exclude='*' src_directory/ dst_directory/</code>When using multiple include/exclude rules, the first matching rule applies. The
-mflag removes empty directories.
Alternatively, pipe the output of
findto rsync:
<code>find src_directory/ -name "*.jpg" -printf '%P\0\n' | rsync -a --files-from=- src_directory/ dst_directory/</code>Conclusion
After mastering these options, rsync can replace scp as the standard tool for synchronizing files and directories locally or between servers.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.