When you use Rsync for transferring or synchronizing files between systems, you may encounter situations where you want to exclude certain files or directories. For context, Rsync is a powerful tool that provides fast, incremental file transfer and synchronization between local and remote systems.
To exclude specific files when using Rsync, there are two general methods:
1. Using ‘—exclude’ option directly on the command line.
2. Utilizing ‘—exclude-from’ option which allows you to list your exclusions in a separate file.
Let’s discuss each approach in detail.
1. USAGE OF ‘—EXCLUDE’ OPTION
This method involves pointing out individual files or directories you wish to exclude directly in your Rsync command. It’s perfect for few and obvious files or directories.
For example, if you want to sync all files from directory ‘/source/’ to ‘/destination/’, excluding ‘file1’, the command will be as follows:
`rsync -av —exclude ‘file1’ /source/ /destination/`
Wildcards can be used as well to exclude types of files. For instance, to exclude all ‘.txt’ files:
`rsync -av —exclude ‘*.txt’ /source/ /destination/`
Multiple —exclude options can be specified. To exclude ‘file1’ and ‘file2’:
`rsync -av —exclude ‘file1’ —exclude ‘file2’ /source/ /destination/`
1. USAGE OF ‘—EXCLUDE-FROM’ OPTION
This method is excellent when you have multiple or complex sets of files or directories to exclude. Instead of listing them all out on the command line, you can list them in a file and reference that file in your command.
For instance, create a file named ‘exclude.txt’ and list the files or directories you want to exclude:
`file1`
`dir1/`
Then, use the ‘—exclude-from’ option in the command:
`rsync -av —exclude-from ‘exclude.txt’ /source/ /destination/`
Like before, wildcards and multiple exclusions are applicable.
Remember that the way Rsync interprets the relative file paths could be different than expected. If issues arise, try changing how you list the file or directory in ‘exclude.txt’ or in ‘—exclude’ (e.g. without leading /, with \*, etc).
This information is sourced from ‘man rsync’, the manual of Rsync, maintained by Wayne Davison, available in many Unix and Unix-like operating systems (source: manpages.ubuntu.com), and the article “How to Use Rsync to Sync Local and Remote Directories” on Tecmint, a respected Linux and open-source blog (source: www.tecmint.com/rsync-local-remote-file-synchronization-commands/).
Note: Always perform a ‘dry run’ with rsync using ‘-n’ or ‘—dry-run’ option to ensure your command behaves as expected before running the actual command.