RSYNC is a powerful tool that lets you synchronize one set of files with another set. It’s generally used to create backups or transfer files over a network, but you can use it to only copy files that already exist on the destination with the help of an “existing” option. This option is useful when you want to update files that are already at the destination without creating new files.
The general syntax to use RSYNC with the “existing” option is as follows:
`rsync -avz —existing src/ dest/`
Here, `src/` is the source directory and `dest/` is the destination directory. The `—existing` option tells RSYNC to only update files that already exist in the destination directory. The `-avz` flags are commonly used options which stands for:
- `a`: archive mode, it ensures to preserve permissions, symbolink links, user & group ownerships and timestamps.
- `v`: verbose mode, it provides detailed information about what the software is doing.
- `z`: compress the file data during the transfer.
Suppose you have a directory `src/` with the files `file1.txt`, `file2.txt`, `file3.txt` and a directory `dest/` with the files `file1.txt`, `file2.txt`. If you use the “existing” option to copy files from `src/` to `dest/`, only `file1.txt` and `file2.txt` will be copied, because `file3.txt` does not exist in the destination directory. Here’s the command:
`rsync -avz —existing src/ dest/`
It is important to note that the pathnames specified by src and dest are relative to the source and destination directories and not absolute paths.
The “existing” option does not work on its own. It tells RSYNC how to behave after it has created the list of files to copy. If you combine it with the “ignore_non_existing” option, you can further finetune its behavior:
`rsync -avz —existing —ignore-non-existing src/ dest/`
This command tells rsync to only consider files that already exist at the destination and ignore files at the source that do not exist at the destination.
In this way, RSYNC’s function flexibility can be leveraged to carry out tasks like updating selected parts of the file system by copying only the files already present at the destination source.
For more information on the usage of RSYNC, one may refer to the manual pages of RSYNC (`man rsync`), which provide an insight into the different flags and options provided by the tool.
Sources:
- CYBERCITI (https://www.cyberciti.biz/faq/linux-unix-bsd-rsync-copy-destination-files/)
- Linux man pages (http://manpages.ubuntu.com/manpages/natty/man1/rsync.1.html)
- Tecmint (https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/)