20+ rsync command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

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 /src to /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

Sample Output:

20+ rsync command examples in Linux [Cheat Sheet]

 

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

Sample Output:

20+ rsync command examples in Linux [Cheat Sheet]

 

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

Sample Output:

root@golinux:~# rsync -zvh /home/deepak/sample/hello.c ubuntu@192.168.43.232:/home/ubuntu/Record
ubuntu@192.168.43.232'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 ubuntu@192.168.43.232
ubuntu@192.168.43.232'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

Sample Output:

rsync command to copy file from remote to local

 

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

Sample Output:

root@golinux:~# rsync -zvhe ssh ubuntu@192.168.43.232:/home/ubuntu/document /snap/new
ubuntu@192.168.43.232'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

Sample Output:

rsync command to set max size limit

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 --bwlimit option.

# rsync -zvh --bwlimit=5 /source/directoy/file user@remote_host:/destination/directory

Sample Output:

root@golinux:~# rsync -zvh --bwlimit=5 /home/deepak/Folder/article ubuntu@192.168.43.232:/home/ubuntu
ubuntu@192.168.43.232'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

Sample Output:

command to view rsync process

 

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

Sample Output:

rsync command to delete sources

 

10. Exclude files while copying using rsync command

The --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

Sample Output:

rsync command to exclude file

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 -i or --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

OR

# rsync -zavh --itemize-changes  /source/directory/ user@remote_host:/destination/directory

Sample Output:

rsync command to view the difference between source and destination

  • 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/

Sample Output:

rsync commad to list contents of directories

 

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 -o or --owner option as shown below:

# rsync -zvho /source/directory/file destination/directory

Sample Output:

rsync command to preserve owner

 

14. rsync command to preserve group after file transfer

You can also preserve the group ownership of a source file to copy file using -g or --group option.

# rsync -zvhg /source/directory/file /destination/directory

Sample Output:

rsync command to preserve group

 

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

Sample Output:

rsync command to delete files at destination

 

16. Perform dry run of rsync command

-n or --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

OR

# rsync -zavh --dry-run user@remote_host:/source/directory/ /destination/directory

Sample Output:

rsync dry run command output

 

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

OR

# rsync -zavh --ignore-non-existing /source/directory/ user@remote_host:/destination/directory

Sample Output:

rsync command to skip syncing existing file

 

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

Sample Output:

rsync command to ignore existing file

 

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 -l option.

# rsync -zavh -l /source/directory/file user@remote_host:/destination/directory

Sample Output:

root@golinux:~# rsync -zvhl /snap/new/symdoc1 ubuntu@192.168.43.232:/home/ubuntu
ubuntu@192.168.43.232'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

-r or --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

OR

# rsync -zavh --recursive /source/directory/ user@remote_host:/destination/directory

Sample Output:

rsync command to copy directory recursively

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

Sample Output:

root@golinux:~# rsync -zvh --update /snap/new/document ubuntu@192.168.43.232:/home/ubuntu
ubuntu@192.168.43.232's password: 
sending incremental file list

sent 55 bytes  received 12 bytes  19.14 bytes/sec
total size is 76  speedup is 1.13

 

Conclusion

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.

 

What's Next

15+ scp command examples in Linux [Cheat Sheet]

 

Further Reading

man page for rsync command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment