Rsync is a command-line utility that is used to synchronize files and directories between two locations. It is commonly used to copy files from one location to another while preserving file attributes such as permissions, timestamps, and ownership. One of the powerful features of rsync is the ability to copy only the files that are missing or have been modified in the destination location. This can be useful when you want to keep a backup of your files, or when you want to update a website or server with the latest changes.
Copy Missing Files with Rsync
To copy only missing files using rsync, you can use the
`--ignore-existing` option. This tells rsync to skip files that already exist in the destination location and only copy files that are missing. For example, to copy all files from the directory “source” to the directory “destination”, using the
`--ignore-existing` option, you would run the following command:
rsync --ignore-existing -av source/ destination/
-a option tells rsync to preserve file attributes and the
-v option verbose mode which will show you the progress of the transfer and the files that are being copied.
Copy Modified Files with Rsync
It’s also possible to copy only the new or modified files and exclude the older files that already exist in the destination. For this you can use
--update along with
--recursive option to transfer the directory recursively.
rsync -ur source/ destination/
Copy Files Modified in Last N Days
You can also specify that the rsync should only copy files that have been modified in the last N days. For example, to copy only files that have been modified in the last 7 days, you would use the
--max-age option along with the number of days, like this:
rsync --ignore-existing --max-age=7 -av source/ destination/
Copy Files By Size with Rsync
It’s also possible to specify only to copy the files that are larger or smaller than a certain size using
--size-range option, which takes the argument of either
-SIZE. For example, if you want to copy only files that are larger than 100MB, you would use the
--size-range option like this:
rsync --ignore-existing --size-range='+100M' -av source/ destination/
There are also many other options available with rsync that can be used to further fine-tune the file transfer process. For example, you can use the
--exclude option to exclude certain files or directories from the transfer, or the
--dry-run option to see what files would be copied without actually performing the transfer.
In conclusion, the rsync command is a powerful tool for copying files and directories and can be used to copy only the files that are missing or have been modified in the destination location. By using the various options available with rsync, you can further fine-tune the file transfer process to suit your specific needs.