Rsync
(Remote Sync) is the command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems.
In this article, we shall examine how rsync can help us sync files or directory content.
Command Usage and Examples
The general format is rsync options source destination
Notes
- For folders, including the
/
rsyncs all contents of the folder to the target. Without the /
it creates and syncs the folder itself and it’s contents
Rsync Local to Local Files/Directories
rsync all files with extenstion *.txt
to /tmp/documents/
rsync -zavh --progress files/*.txt /tmp/documents/
rsync and update contents in files/
to /tmp/documents/
rsync -uzavh --progress files/ /tmp/documents/
rsync and update contents in files/
to /tmp/documents/
. Exclude files larger than 500m
rsync -uzavh --progress --max-size=500m files/ /tmp/documents/
rsync and update contents in files/
to /tmp/documents/
. Exclude files smaller than 1m
rsync -uzavh --progress --max-size=1m files/ /tmp/documents/
Delete files in /tmp/documents/
if source no longer exist in ~/files
rsync --progress --recursive --ignore-existing --delete files/ /tmp/documents/
Delete files in target during rsync not present in the source
rsync --progress --recursive --delete files/ /tmp/documents/ --delete-during
Rsync and exclude based on list. File name exclude-file.txt
files/dir3
files/dir4
files/samplefile.txt
Then
rsync -a --progress --exclude-from='exclude-file.txt' files /tmp/documents/
Rsync and ignore existing files. NOTE: Even with updates, existing files are ignored
rsync -avzh --progress --ignore-existing files/ /tmp/documents/
Rsync only file with updates.
rsync -avzh --progress --update files/ /tmp/documents/
Rsync Local Files/Directories to Remote
Same actions above to remote host
rsync -uzavhe --progress files/ user@192.168.0.11:/tmp/documents/
Rsync Remote Files/Directories to Local system
Same actions above to remote host
rsync -uzavh --progress user@192.168.0.11:/tmp/documents/ ./files/