Filter Files With rsync Include and Exclude

Sync only matching files while still traversing directories.

LinuxbackupFilessyncLow risk
Use --include='*/' then --include='*.md' then --exclude='*' to copy only Markdown files.

01Command

Command
$ rsync -av --include='*/' --include='*.md' --exclude='*' docs/ backup/

02What it does

rsync applies filter rules in order, so including directories first keeps traversal while the final exclude drops everything else. This makes targeted backups and content syncs fast and reproducible, and the same rules can be stored in a file for reuse.

03Risk

RiskLow risk
Requirementssudo apt install rsync
!Warning: Rule order is significant: a broad exclude before an include prevents the include from ever matching.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-10-13 · Category: Files & Folders

06Related solutions