Table of Contents
Introduction to rsync command
rsync is a fast and versatile tool in Linux for copying files. It allows you to copy and synchronize files and directories locally and remotely. It does not copy files between two remote hosts. rsync is popular for backups and mirroring and as an improved copy command for everyday use. It works on all Linux systems such as Ubuntu, Debian, Kali Linux, CentOS, RHEL, Fedora and others.
How rsync works?
rsync uses the delta-transfer algorithm which copies only the changes from the source to the destination instead of the whole file. That results in reducing the amount of data sent over the network. rsync checks the files that have changed in size or modified time using a lqquick checkrq algorithm. When rsync client is started it will first establish a connection with the server process. The connection may be through pipes or over a network socket.
Using rsync for performing regular backups
rsync is an efficient file-transfer program, and its main purpose is keeping filesystems in sync with each other. When you use it for making backups, it keeps your local files in sync with your backup device. It is fast and efficient because it transfers only the changes in files. Unlike a lot of backup software, which never want you to delete anything, it even mirrors deletions. Because of these features, rsync is the tool of choice for updating and mirroring user home directories, websites, git repositories, and other large complex file trees.
#/bin/bash CURDATE=$(date +%m-%d-%Y) if [ ! -f /usr/bin/rsync ]; then sudo apt install -y rsync fi rsync -avb --delete --backup-dir=/backup/incremental/$CURDATE /src /target
This script, when run, will run an rsync job that will copy the contents from
/target. The beauty of this is that
/target can be an external hard drive or network share. So in a nutshell, you can automate a nightly backup. This backup, since we used the
-b option along with
--backup-dir, will allow you to retrieve previous versions of a file from the
/backup/incremental directory. Feel free to get creative here as far as where to place previous file versions and where to send the backup.
We can now call this script as part of cron job.
Different examples to use rsync command
rsync does not require root privileges and can be run by any user in the system. The basic syntax of rsync command would be:
# rsync [option] [source] [destination]
Some important option:
- -v : verbose output
- -z : compress file data
- -h: human-readable output
- -a: archive files and directories while copying
Now, let's have some look at the practical examples of
rsync command in Linux system.
1. Copy/Sync Files locally using rsync command
You can use the following command to copy a file from one location to another on the same Linux server. If the destination directory does not already exist, rsync will create a directory and copy/sync a file to that directory.
# rsync source/directory/file /destination/directory
2. Copy/Sync Directories locally using rsync command
To copy/sync a directory from one location to another on a local Linux server use
-r option with rsync command
# rsync -r /source/dirctory/ /destination/directory
3. rsync command to copy/sync file from local to remote Linux server
The following command allows you to copy/sync a file from a local to any remote Linux server.
# rsync -zvh /local/directory/file user@remote_host:/remote/directory
root@golinux:~# rsync -zvh /home/deepak/sample/hello.c firstname.lastname@example.org:/home/ubuntu/Record email@example.com's password: hello.c sent 113 bytes received 35 bytes 32.89 bytes/sec total size is 31 speedup is 0.21
Now, let's check for copied file.
root@golinux:~# ssh firstname.lastname@example.org email@example.com's password: ubuntu@golinux:~$ ls /home/ubuntu/Record hello.c
4. rsync command to copy/sync file from remote to local Linux server
If you want to copy/sync a file from a remote Linux server to a local Linux server, you can run the following command.
# rsync -zvh user@remote_host:/source/directory/file /local/directory
5. Using SSH for data transfer with rsync command
You can use SSH protocol with
rsync command to transfer the file. ssh encrypts the data during synchronization and enables the secure connection between the source and destination network.
-e option specifies the remote shell to use.
# rsync -zvhe ssh user@destination_host:/directory/file /source/path/file
root@golinux:~# rsync -zvhe ssh firstname.lastname@example.org:/home/ubuntu/document /snap/new email@example.com's password: document sent 43 bytes received 148 bytes 42.44 bytes/sec total size is 70 speedup is 0.37
6. rsync command to set the max file size on file transfer
--max-size option allows you to specify the maximum size of the file to be copied or synced. The file exceeding the maximum size limit will not be transferred. You can use K for KB, M for MB, and G for GB.
# rsync -zavh --max-size=num K filename /destination/directory
As we can see, the file package.deb exceeds the max size 20K, so it is not copied.
7. rsync command to put the bandwidth limit for file transfer
You can put the bandwidth limit for data transfer using
# rsync -zvh --bwlimit=5 /source/directoy/file user@remote_host:/destination/directory
root@golinux:~# rsync -zvh --bwlimit=5 /home/deepak/Folder/article firstname.lastname@example.org:/home/ubuntu email@example.com's password: article sent 932 bytes received 35 bytes 17.12 bytes/sec total size is 1.55K speedup is 1.60
8. Show the progress detail using rsync command
You can use
--progress option to view the detail of the copy/sync process in the output.
# rsync -zvh --progress /source/directoy/ user@remote_host:/destination/directory
9. Delete the source file after copying with rsync command
Sometimes, you may want to delete the source file after it is copied to another location. To do so, you can use --
remove-source-files option like below.
# rsync -zavh --remove-source-files /source/directory/ user@remote_host:/destination/directory
10. Exclude files while copying using rsync command
--exclude option helps to exclude the specific files from the directory while copying.
# rsync -zavh --exclude 'file to exclude' /source/directory/ user@remote_host:/destination/directory
In the above output, hello.c is excluded while copying all files.
11. rsync command to show the changes after file transfer
You can use
--itemize-changes option to view the list of changes made to files or directories between source and destination Linux server.
# rsync -zavhi /source/directory/ user@remote_host:/destination/directory
# rsync -zavh --itemize-changes /source/directory/ user@remote_host:/destination/directory
- d: change in destination directory
- t: change in modification time
- f: denotes a file
- s: change in file size
12. List the files inside the directory with rsync command
This command will help you to list all the files present in the specified directory.
# rsync user@remote_host:/destination/directory/
13. rsync command to preserve owner after file transfer
Normally when you copy a file using rsync, the copied file owner may change according to the destination. If you want to keep the same owner as a source file, you can use
--owner option as shown below:
# rsync -zvho /source/directory/file destination/directory
14. rsync command to preserve group after file transfer
You can also preserve the group ownership of a source file to copy file using
# rsync -zvhg /source/directory/file /destination/directory
15. Delete file at destination directory using rsync command
While syncing, you can delete a file if it is present at the destination but does not exist at the source.
--delete option removes the files that are not present in the source directory.
# rsync -zavh --delete /source/directory/ user@remote_host:/destination/directory
16. Perform dry run of rsync command
--dry-run option lets you perform a trial run of the
rsync command. It does not make any changes but produces the same output as a real run. This can be helpful if you want to know what
rsync command will do after actually running it.
# rsync -zavh -n user@remote_host:/source/directory/ /destination/directory
# rsync -zavh --dry-run user@remote_host:/source/directory/ /destination/directory
17. rsync command to skip syncing non-existing files
This command is used to skip creating files or directories that do not exist at the destination. It will only update the files or directories already present in the destination directory.
# rsync -zavh --existing /source/directory/ user@remote_host:/destination/directory
# rsync -zavh --ignore-non-existing /source/directory/ user@remote_host:/destination/directory
18. rsync command to skip syncing existing files
--ignore-existing option will help you to skip updating the files that are already present in the destination directory. It does not affect the existing directories.
# rsync -zavh --ignore-existing /source/directory/ user@remote_host:/destination/directory
19. Copy symbolic link file with rsync command
Normally, rsync copies the referent file of a symbolic link file. To copy/sync a symlink to another location, you should use
# rsync -zavh -l /source/directory/file user@remote_host:/destination/directory
root@golinux:~# rsync -zvhl /snap/new/symdoc1 firstname.lastname@example.org:/home/ubuntu email@example.com's password: symdoc1 -> /home/deepak/Folder/doc1 sent 69 bytes received 19 bytes 3.20 bytes/sec total size is 24 speedup is 0.27
20. rsync command to copy directories recursively
--recursive option tells rsync to copy/sync the directory recursively. All of its files and sub-directories will be copied to the target location.
# rsync -zavh -r /source/directory/ user@remote_host:/destination/directory
# rsync -zavh --recursive /source/directory/ user@remote_host:/destination/directory
21. rsync command to skip files if modification times are equal
With this option, rsync skips any files which exist on the destination directory and has the modification time newer than the source file. If the existing destination file has a modified time equal to the source file, it will be updated if their sizes are different.
# rsync -zvh --update /source/directory/file user@remote_host:/destination/directory
root@golinux:~# rsync -zvh --update /snap/new/document firstname.lastname@example.org:/home/ubuntu email@example.com's password: sending incremental file list sent 55 bytes received 12 bytes 19.14 bytes/sec total size is 76 speedup is 1.13
In this article, we have shown you 20+ practical example of
rsync command in Linux systems. rsync is a useful tool which allows you to copy/sync files or directories both locally and remotely. If you still have any confusion, please use the comment section to ask the questions.