17 Helpful Rsync SSH Command Examples For Linux 2024

Rsync is also known as remote sync is a command-line tool used to copy and sync files and directories remotely as well as locally in Linux/Unix systems. With this rcopy and synchronize data:

  • Remotely and locally across directories, disks, and networks.
  • Perform data backups.
  • Mirror between two Linux machines.

We have created a list with various Rsync command examples, including using Rsync backup examples and other tips to help you make the most out of this fabulous tool.

Let’s get in!

Why use Rsync Commands in the first place?

Thought you would ask, so here is why:

  1. Copies and syncs file efficiently to or from a remote system.
  2. It supports copying links, devices, owners, groups, and permissions.
  3. It is much faster than Secure Copy (scp) as it uses a remote-update protocol to transfer between two sets of files. For the first time, it copies the entire content of a file or directory from the source towards a destination but the next time, it only copies the changed blocks and bytes to the destination.
  4. It consumes less bandwidth as it uses compression and decompression method while sending and receiving data on both ends.

Common Rsync Arguments

  1. -r: copies data recursively.
  2. -v: This is for verbose output.
  3. -a: archive mode which allows copying files recursively. Moreover, it also preserves symbolic links, file permissions, user, and group.
  4. -z: Compress file data.
  5. -h: human-readable, it output numbers in a format which is read by humans.

17 Different Rsync Commands To Use (Man, Backup, SSH & Directories)

Rsync Syntax

sync options source destination

Installing Rsync (Remote Sync) on Linux

Install this by using the following command

yum install rsync (For RedHat OS)
apt-get install rsync (For Debian Distros)

Copy and syncing files or a directory (Local to Remote Site)

Rsync Directory

This shall sync directory from local machine to remote machine, like there is a folder in local computer “rpmpkgs” which carries some RPM packages and you want local directories content sent towards the remote server, use the following command:

[root@dekisoft] rsync -avz rpmpkgs/ [email protected]:/home/
sending incremental file list

READ ALSO: Top Xfce Dark Themes for Manjaro and Mint.

Copy or Sync a remote directory to the local desktop

This shall help to sync the remote directory to the local directory. In the example below, a directory /home/shaheer/rpmpkgs, a remote server is brought copies in local systems in /tmp/myrpms.

[[email protected]] rsync -avzh [email protected]:/home/shaheer/rpmpkgs /tmp/myrpms
receiving incremental file list
created directory /tmp/myrpms

This shall sync a single file on a local machine from one location to another. In the example, a file name backup.tar needs to be copied or synced towards /tmp/backups/ folder.

[[email protected]] rsync -zvh yourfiles.tar /tmp/yourfiles/
created directory /tmp/yourfiles
yourfiles.tar

The following command shall transfer or sync all files from one directory to a different directory in the same machine. In the examples, /root/rpmpkgs carries some rpm package files and you want that directory to be copied inside /tmp/backups/ folder.

[root@dekisoft] rsync -avzh /root/rpmpkgs /tmp/yourdekifiles/
sending incremental file list
rpmpkgs/

Display Rsync Transfer Progress

We use –progress option to show progress while transferring data from one machine to another. It also displays files and the time remaining to complete the transfer.

[root@dekisoft] rsync -avzhe ssh --progress /home/rpmpkgs

Rsync over SSH

With this, we can use SSH (Secure shell) for the transfer of data, Using this protocol while transferring data we can be assured that data is being transferred in a secured connection with encryption so that no one can read data while it is transferred over wire on the internet.

Also when we use this, we need to provide user/root password to achieve a certain task, so using this option shall send logins in an encrypted manner so that passcode shall remain safe.

READ ALSO: Reset the root password of Kali Linux to default.

Backup files from a cloud server to a locally hosted server via SSH

Rsync Backup Examples

To specify protocol with this you need to give –e option with protocol name you wish to use. In the example, we shall use ssh with –e  option and perform data transfer.

[root@dekisoft]rsync -avzhe ssh [email protected]:/root/install.log /tmp/
[email protected]'s password:

Copy file from local server to a local remote server with SSH

Rsync SSH Example

[root@dekisoft] rsync -avzhe ssh backup.tar [email protected]:/dekibackup/
[email protected]'s password:
sending incremental file list

Rsync –delete

If any file or directory does not exist at the source but exits at the destination then you may want to delete that existing file or directory at a target while syncing.

-delete option can be used to delete a file that is not in the source directory.

Source and target are in sync, creating new files test.txt at the target.

[root@dekisoft] touch dekisoftryncpost.txt

[root@dekisoft] rsync -avz --delete [email protected]:/var/lib/rpm/ 

Target carries a new file known as test.txt when synched with the source with this option, it removed file dekisoftrysncpost.txt.

READ ALSO: Ubuntu and Manjaro Comparison – Updates, Packages, and Speed.

Set the max size of files to be first transferred

Users can specify the file size to be transferred or synced. This can be done with –max-size option. In the example, the maximum size is 200k so this command shall transfer only those files equal to or smaller than this.

[root@dekisoft] rsync -avzhe ssh --max-size='400k' /var/lib/rpm/ [email protected]:/root/tmprpm
[email protected]'s password:
sending incremental file list
created directory /root/tmprpm
./

Do a dry run with Sync

If you are a new user and using this and do not know what your command is going to do, this can mess up things in your destination folder and then doing undo can be quite a tedious job.

Using this option shall not make any changes but only do a dry run of the command and show the output of the command, if the output shows the same result you want to do then you can remove –dry-run option from the command and run on the terminal.

root@dekisoft$ rsync --dry-run --remove-source-files -zvh mybackup.tar /tmp/mybackup/
mybackup.tar

READ ALSO: ADB and Fastboot Terminal Commands with examples.

Conclusion: Which Rsync command amazes you?

A remote sync is a great tool that has made many system administrators’ lives easier for creating backups and moving data between servers. This is why I have shared my expertise on the practical Rsync command examples that you can utilize for backups, SSH, moving and synchronizing directories. For further information, you can refer to the official Man page.

Previous article12 Best CMD Commands Used for Hacking in Windows 11 (2024)
Next article12 Best Hacking Tools For Windows 11 2024 (Free Download)
Lee White
Lee is currently a full-time writer at DekiSoft that is eager to discover new and exciting advancements in Technology, Software, Linux and Cyber Security. Lee has spent the past 18 years working as an Systems Engineer providing support for various operating systems and networks. When not at his desk or writing, you will find him tinkering with retro tech.

1 Comment

  1. The –itemize-changes option is important. Even more so than -v.
    Rsync’s default for -e has been ssh for around 20 years.
    Don’t use -z on local copies. Rsync is dumb enough to do what you tell it to even when that is compressing and decompressing files to save bandwidth when no bandwidth is involved.
    2 of your examples (–delete and –progress) have only 1 path. If you run rsync with only 1 path it simple lists files. This is to allow a user who only has rsync access to do remote file listings.
    -t should probably be in your list of common options. Rsync should almost never be run without either -a or -r + -t.
    –link-dest is the option that makes rsync really interesting for backup purposes. Though I would agree that that is a complex enough topic to be a separate article.

Leave A Reply

Please enter your comment!
Please enter your name here