Ignore existing files or update only newer files with rsync
Rsync is a useful command line utility for synchronising files and directories across two different file systems. I recently needed to use rsync to only copy over files that did not already exist at the other end, so this post documents how to do this.
Copying from local to remote
Note that all the examples shown in the post are for copying files from the local computer to a remote server/computer.
Default behavior
The following command will recursively copy all files from the local filesystem from /var/www to the remote system at 10.1.1.1. Note the following:
- Any files that do not exist on the remote system are copied over
- Any that have been updated will be copied over, although note that rsync is extremely efficient in that only the changed parts of files are copied and if the file is exactly the same if it is not copied over at all
- Any that have been deleted on the local system are deleted on the remote
rsync
-raz --progress /var/www 10.1.1.1:/var
Ignore existing files
Use the –ignore-existing flag to prevent files from being copied over that already exist on the remote server. By adding this, we eliminate behaviors 2 and 3 in the list above and all that is done is this:
- Any files that do not exist on the remote system are copied over
--ignore-existing -raz --progress /var/www 10.1.1.1:/var