How to exclude files and directories from rsync sync?

Published on Aug. 22, 2023, 12:18 p.m.

To exclude files and directories from an rsync sync, you can use the --exclude option followed by the path of the file or directory you want to exclude. You can use this option multiple times to exclude multiple files or directories.

Here is an example command to exclude a file and a directory during an rsync sync:

rsync -avz --exclude 'file.txt' --exclude 'dir/' /path/to/source/ /path/to/destination/

Explanation of the options used:

  • -a: archive mode, which preserves permissions, ownership, timestamps, and recursive copying
  • -v: verbose output
  • -z: compress file data during transfer to reduce bandwidth usage
  • --exclude: specify a file or directory to exclude from the sync

Note that you can also use patterns with wildcards to exclude multiple files or directories that match a certain pattern. For example, to exclude all .log files in the source directory, you can use:

rsync -avz --exclude '*.log' /path/to/source/ /path/to/destination/

Tags: