Operations 6 min read

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.

Efficient Ops
Efficient Ops
Efficient Ops
Mastering rsync: How to Exclude Files and Directories Efficiently

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

-a

option 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

--exclude

on the command line.

Use

--exclude-from

with 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

--exclude

options, or combine them with braces:

<code>rsync -a --exclude 'file1.txt' --exclude 'dir1/*' --exclude 'dir2' src_directory/ dst_directory/</code>

Or use a single

--exclude

with a brace list:

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

When many patterns are needed, use

--exclude-from

with 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

.jpg

files:

<code>rsync -a --exclude '*.jpg*' src_directory/ dst_directory/</code>

To copy only

.jpg

files 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

-m

flag removes empty directories.

Alternatively, pipe the output of

find

to 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.

command lineBackuprsyncfile synchronizationexclude
Efficient Ops
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.