`rsync` is a free and open-source utility that synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. It is especially renowned for its rich set of options that allow users to fully control the synchronization process. Certain times, you may want to exclude one or many directories from rsync task.
To exclude a directory or certain files during the rsync process, the `—exclude` option is used. This option prevents files, directories, and patterns from being included in your rsync operation.
Here’s a basic syntax:
rsync -av —exclude=‘dir1’ source/ destination/This command would sync all files from the source directory to the destination directory, excluding files in ‘dir1’.
You can also exclude multiple directories:
rsync -av —exclude={‘dir1’,‘dir2’,‘dir3’,‘dir4’} source/ destination/This would exclude ‘dir1’, ‘dir2’, ‘dir3’, and ‘dir4’ from the rsync process.
You can further use wildcard characters in your exclusions:
rsync -av —exclude=‘dir\*’ source/ destination/The above command will exclude all directories starting with ‘dir’.
For excluding files with a certain extension:
rsync -av —exclude=’\*.txt’ source/ destination/This will exclude all .txt files from being transferred.
The `—exclude` option is simple and flexible, allowing you to construct a command that suits your specific needs.
It’s also worth mentioning the `—exclude-from` option of `rsync`. Instead of specifying the excluded directories and/or files on the command line, they can be stored in a file:
rsync -av —exclude-from=‘exclude.txt’ source/ destination/The `exclude.txt` file mentioned would contain a list of directories or files to exclude, each on a new line.
As a final note, always test your rsync commands with the `-n` or `—dry-run` option before running them. This option will show you what would occur without actually performing the action.
Sources that contain more information on using `rsync` and the `—exclude` option include the `rsync` manual page (`man rsync` in the terminal), as well as numerous online guides and tutorials, such as those on the Tecmint and DigitalOcean websites.
References:
- `man rsync` command, Linux man pages.
- K, Sarath. “Rsync Exclude Files and Directories.” Tecmint, April 27, 2020. https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
- Ewing, Etel Sverdlov, Justin. “How To Use Rsync to Sync Local and Remote Directories.” DigitalOcean, May 8, 2019. https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories-on-a-vps