Open In App

rsync command in Linux with Examples

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

rsync or remote synchronization is a software utility for Unix-Like systems that efficiently sync files and directories between two hosts or machines. One is the source or the local-host from which the files will be synced, the other is the remote-host, on which synchronization will take place. There are basically two ways in which rsync can copy/sync data:

  • Copying/syncing to/from another host over any remote shell like ssh, rsh.
  • Copying/Syncing through rsync daemon using TCP.

Rsync is famous for its delta-transfer algorithm, in which it copies only the differences between the source files present in the local-host and the existing files in the destination or the remote host.

Example:

rsync local-file user@remote-host:remote-file

What Happens Here:

Rsync will first use SSH to connect as the user to remote-host and will ask for user's password. Once connected, it will invoke the remote host’s rsync, and then the two programs will determine what parts of the local-file need to be copied so that the remote file matches the local one. Please note the following behavior of rsync:

  • Files that do not exist on the remote-host are copied.
  • Files that have been updated will be synced, rsync will copy only the changed parts of files to the remote host.
  • files that are exactly the same are not copied to the remote host.

Syntax of `rsync` command in Linux

rsync [options] source [destination]

Option available in `rsync` command in Linux

Options

Description

a, –archive

This is equivalent to using -rlptgoD. Archive mode includes all the necessary options like copying files recursively, preserving almost everything (like symbolic links, file permissions, user & group ownership and timestamps).

-v, –verbose

By default, rsync operates silently. Using a single “-v” option provides information on transferred files and a summary at the end. Adding two “-v” options gives status updates on delta-transmission and skipped files, along with more information at the end. Multiple “-v” options are typically used for debugging rsync.

-h, –human-readable format

Outputs in a human readable format.

-z, –compress

Compress file data during the transfer

Examples

Using `rsync` as a list command

If only the source path is specified, the contents of the source are listed in an output format similar to ls -l.

rsync foo/ 

The above command will list the files and directories present in the directory foo.

Output:

378

Copy/Sync files and directory locally

If neither the source or destination path specifies a remote host, the rsync commands behave as a copy command.

rsync -avh foo/ bar/ 

The above command will copy/sync all the files and directories present in directory foo to directory bar. If the destination directory is not present (here bar), rsync automatically creates one and copies all the data in it.

Output:

379

Rsync using ssh

There are two different ways for rsync to contact a remote system:

  • Using a remote-shell program as the transport(such as ssh(Secure Shell) or rsh(Remote Shell)).
  • Contacting an rsync daemon directly via TCP.

Here we will be discussing rsync over ssh.

rsync -avhze ssh /foo user@remote-host:/tmp/

To specify the type of protocol to be used, -e option is used.

Output:

380

Rsync with particular file permissions

If we want to sync files to the local or remote host with the permissions of the files being changed. The following command must be used.

rsync -avhe ssh --chown=USER:GROUP /foo user@remote-host:/tmp/

The above command will sync all the files present in directory /foo with the files present in directory /tmp in the remote-host with all the files owned by USER with group GROUP.

Output:

381

Note: The user and group must already be created in the remote-host.

Rsync with --ignore-existing-files

We can also skip the already existing files on the destination. This can generally be used when we are performing backups using the –link-dest option, while continuing a backup run that got interrupted.

rsync --ignore-existing -avhe /foo user@remote-host:/tmp/

So, any files that do not exist on the destination will be copied over. Here I have deleted the geeksforgeeks folder from the directory foo, so it should copy only the geeksforgeeks directory.

Note: This does not ignore existing directories, or nothing will get done. Even if there are some changes in a file in the local host, it still would not be synced if it’s present on the remote host.

Output:

382

Show progress during transfer

To show the progress while transferring the data from local-host to remote-host, we can use -–progress option.

rsync -avhe ssh --progress /foo user@remote-host:/tmp/

When the transfer completes for a particular file, rsync outputs a summary line as shown below.

Output:

383

In the above image, if we look at the file /foo/file2, it tells us that

  • The file was 44 bytes long in total.
  • The average rate of transfer for the whole file was 42.97 kilobytes per second over the 0:00:00 seconds that it took to complete.
  • It was the second transfer during the current rsync session.
  • There are 10 more files for the remote-host to check (to see if they are up-to-date or not) remaining out of the 13 total files in the file-list.
  • Update the remote only if there is a newer version is on the local filesystem:
    If we want to copy files over the remote-host that have been updated more recently on the local filesystem. It is done with –update flag. The behavior is now like this:
    • Files that do not exist on the remote-host are copied.
    • Files that exist on both local and remote but have a newer timestamp on the local-host are copied to remote-host. (Conversely, files that have an older timestamp are not copied).

    Here, I made some changes in file1 and file2, but the changes in file2 were done recently. So only file2 will get synced.

    rsync -avhe ssh --progress --update /foo root@remote-host:/tmp/
    

    Output:rsync update

    Automatically delete files from local-host after successful transferring

    Now, suppose we have a web server and a backup server and we created a daily backup and synced it with our backup server and then we don’t want to keep the local copy of the backup in our web server. So, instead of deleting it manually after successful transfer, we can use the --remove-source-files flag to automatically delete the files from the web server.

    rsync -avhe ssh --remove-source-files /foo user@backup-server:/tmp
    

    Output:

    384

    Note: This will delete only the files and not the directories.

    Delete the files that have been deleted on the local-host

    If there are some files that are deleted on the local-host and we want that to be updated on the remote host as well, then we need to use the --delete option.

    rsync -avhe ssh  /foo --delete user@remote-host:/tmp/
    

    Output:

    So, here file1, file2, file3 were deleted on the local-host, and as can be seen, are updated in the remote-host as well.

    Note: `rsync` does not delete the files automatically to sync the directories present on both sides.

    Performing a Dry run with `rsync`

    A Dry run makes `rsync` perform a trial run that doesn’t make any changes and displays almost the same output as a real run would do. It is generally used with the -v, –verbose and/or -i, –itemize-changes options so as to see what a `rsync` command would do before one actually runs it.

    rsync -avhe ssh --dry-run --chown=USER:GROUP /foo user@remote-host:/
    

    Output:

    385

    Conclusion

    In this article we discussed the rsync command in Linux which is a versatile and powerful tool for synchronizing files and directories between hosts or machines. With its delta-transfer algorithm and various options, it offers efficient data synchronization, backup capabilities, and file transfer management. By mastering the rsync command, Linux users can ensure file consistency, optimize data transmission, and streamline their data management workflows.



  • Previous Article
    Next Article

    Similar Reads

    How to Use Rsync to Make a Remote Linux Backup
    Imagine losing all your data by accidentally doing sudo rm -rf /someDirectory, sucks right? But it happens to the best of us. Now, if it was a Windows system, we could have used numerous backup options like OneDrive to secure our data. But what about Linux? Fortunately, we have some very robust tools to backup our Linux system too, and one of them
    4 min read
    Ccat – Colorize Cat Command Output command in Linux with Examples
    ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
    2 min read
    How to Display Command History in Linux | history Command
    The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
    4 min read
    select command in Linux with examples
    select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
    1 min read
    ZIP command in Linux with examples
    ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip {.zip-filename} file with the extension .zip. Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.If you have limited bandwidth between two
    7 min read
    atrm command in Linux with examples
    atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
    1 min read
    comm command in Linux with examples
    comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
    5 min read
    cmp Command in Linux with examples
    cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
    5 min read
    expand Command in LINUX with examples
    Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
    3 min read
    df Command in Linux with examples
    There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
    8 min read
    Article Tags :