The Rsync command allows us to transfer and sync files over an SSH connection or a local machine across different directories and machines. If you ever need to backup your directory or sync the data, the Rsync is one of the best tools for backing up the system. Using the Rsync is a lot more efficient than just copying files because Rsync only copies what is different.
For instance, if you’re trying to backup 1000 photos, the Rsync will only choose those specific files that are either new or different since the last backup. And also, if you’re copying data and lose the network connection and re-run the Rsync command, it would know where it left off and wouldn’t have to start over.
As a Linux sysadmin, you must know some major and practical examples of Rsync commands in Linux to make your experience smoother.
Features of Rsync in Linux
Here, I am adding a few basic features of the Rsync. You can go through the below-provided points for a detailed overview of the remote sync command that will help you later to understand the examples of Rsync commands in Linux.
- The Rsync command can copy files and directories and subdirectories.
- As the Rsync uses the delta-transfer algorithm, the tool skips files already in the destination address.
- The Rsync ignores the file that is exactly the same as the client and host systems.
- You can use Rsync for both on SSH or Local machine.
- It can perform the tar commands along with copying files.
- In most cases, the Rsync doesn’t need the sudo privilege.
- You can use the pipeline commands along with the Rsync to transfer files faster and keep the latency lower.
- For copying files over the web, the Rsync uses the TCP protocol.
- The Rsync can reduce the amount of required bandwidth.
- You can not copy files between two or more remote hosts via the Rsync on Linux.
Remote Sync Syntax in Linux
Here, I’m adding a few most important syntaxes and the synopsis of the Rsync. Going through the syntaxes and flags is important for better understanding the examples of Rsync commands in Linux.
The given command formats are for transferring files on the local machine.
rsync [OPTION...] SRC... [DEST]
Rsync uses the given command format for transferring files via a remote shell.
Pull: rsync [OPTION...] [[email protected]]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [[email protected]]HOST:DEST
Rsync uses the given command format for transferring files when it accesses the host as a daemon.
Pull: rsync [OPTION...] [[email protected]]HOST::SRC... [DEST] rsync [OPTION...] rsync://[[email protected]]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [[email protected]]HOST::DEST rsync [OPTION...] SRC... rsync://[[email protected]]HOST[:PORT]/DEST
Install Rsync on Linux
Before we jump into the examples of Rsync commands in Linux, we must check if we have the tool installed on the system or not. By default, the Rsync comes pre-installed with all major Linux distributions. Hence, if you find the tool is not installed on your system, you can easily get the Rsync tool on your machine. Here, I am enlisting the commands for the major Linux distributions to install Rsync.
Get the Rsync on Fedora and Red Hat Linux
sudo yum install rsync -y sudo dnf install rsync -y
Install Rsync on Debian/Ubuntu Linux
sudo apt install rsync -y
You can get the Rsync tool from the Snap commands on Arch Linux systems. Please execute the below-mentioned commands respectively to get the Rsync tool on Arch Linux. First, clone the repository on Linux.
git clone https://aur.archlinux.org/snapd.git cd snapd makepkg -si
Now, execute the system control commands to use the sockets. Then run the Snap command to install the tool.
sudo systemctl enable --now snapd.socket sudo ln -s /var/lib/snapd/snap /snap sudo snap install rsync-leftyfb
Finally, when the installation ends up, you may now check the help page of Rsync on the terminal shell.
$ rsync --help
Rsync in Linux
The Rsync is the short form of the Remote synchronization command. This Rsync tool is available for both Mac and Linux. In both cases, the syntaxes and command styles are pretty much the same. Now, when we say remote sync, that does not always necessarily mean that you will need to sync or copy files over an SSH or the internet.
You can also execute Rsync on the same machine for syncing two different directories or subdirectories. Very effective use of the Rsync is seen in updating package repositories and alternative or mirror servers.
In this post, we will see a few examples of the most used and powerful Rsync commands for Linux.
Rsync Examples in Linux
Here are some basic examples of the Rsync command and format in Linux. The below provided one is the command format, and the second is for using the Rsync over SSH.
In Linux, the Rsync will establish a link over the SSH connection when running it over a host-client environment. When the remote user or the client accepts the connection, the client Rsync automatically wakes up the Rsync on the host system.
General Command Format of Rsync
$ rsync [options] source [destination]
Rsync Command format for the SSH connections
$ rsync local-file [email protected]:remote-file
1. Remote Sync Command To Copy All Files On Directory
The below-mentioned example of the Rsync command on Linux will copy all the files from the Documents to the /tmp/documents directory.
$ rsync -av Documents/* /tmp/documents
In this command, we have used three flags; -a and -v, and the *. Here is the explanation of what they do in the command.
- -a: The -a flag executes the command in the archive/library manner.
- -v: The -v blag is used for running the Rsync as verbose.
- – *: The * symbol ensures that all the files are selected and copied.
Now, if we add some new files to the original directory and run the Rsync again, it will copy the new files and notify the verbal output in the shell.
$ rsync -av Documents/* /tmp/documents
The below-mentioned command will allow us to run a test to know which files are gonna be synced to avoid errors.
$ rsync -aunv /home/ubuntupit/Documents/* /home/ubuntupit/Pictures/
After running a test check, we can now run the actual command by removing the -n flag on the command.
$ rsync -auv /home/ubuntupit/Documents/* /home/ubuntupit/Pictures/
2. Rsync Command Example Over SSH
Till now, we have seen some examples of Rsync on how to execute it in a local machine. Here, we will see how to run it over the servers. All the syntaxes are the same; you just need to put the web-server address and the user name.
$ rsync -av --ignore-existing Documents/* [email protected]:~/all/
3. Rsync Examples in Linux: Dry Run Rsync
In programming, the term dry run is defined as executing a command just to see a preview or false execution to check what might happen if we run the command. Here, we can apply the dry run command for Rsync as well.
The Rsync command examples below will allow us to check what files will be synced on the host system from the remote Linux server. Later, we can execute the command without the dry run.
$ rsync -av --dry-run --update Documents/* [email protected]:~/all/ $ rsync -av --update Documents/* [email protected]:~/all/
4. Sync Files Between the Local Machine and Remote Server
The above command shows how to send files either from local machine to local machine or from server to another host. The below example of Rsync shows how you can use the tool to send files from a local machine to a remote Linux machine.
rsync [options] [files] [remote-username]@[ip-address]:/[destination]
5. Transfer Files From the Remote Server To the Local Machine
Just the reverse of the above command, the below-mentioned example of the Rsync command will show how you can send files from the remote Linux server to your local machine.
rsync -rv [email protected]:/home/ubuntupit/Backup /home/ubuntupit/Desktop/Original
6. Display Progress with the Rsync
This one is very important for those who need to transfer large files via the Rsync tool. For instance, the below-mentioned example of the Rsync command shows how you can monitor and observe the transfer progress in the CLI on Linux.
sudo rsync -rv --progress Original/* /etc/ubuntupit
You can run the Rsync command with the ‘–include’ flag to add a specific file to the syncing process.
sudo rsync -vr --include 'I*' Original/ /etc/Backup/
7. Use Rsync with ‘–exclude’ to Ignore Files
With Rsync, you can also use an exclude command to exclude any file from your command. The below-mentioned example of the Rsync command in Linux shows us how to exclude a file that we don’t want to sync. Here I have excluded the file To_check_rsync_two (another copy).txt to be synced through this Rsync command on my Linux System.
$ sudo rsync -vr --exclude 'To_check_rsync_two (another copy).txt' /home/ubuntupit/Desktop/ /home/ubuntupit/Documents/
In the same way, you can also include a file if you want to include forcefully that you think that rsync will not add by default.
sudo rsync -vr --exclude '*' --include 'I*' Original/ /etc/Backup/
To remove or delete a file to be synchronized through the Rsync command, you can use the -delete flag in the command on the terminal shell.
rsync -vr --delete /etc/Backup/ [email protected]:/home/ubuntupit/Backup
8. Set the Max Size Limit
If you are using a limited amount of bandwidth data on a computer and you need to pass multiple files, but you don’t know when you need to stop the transferring to save your bandwidth, you can use the below-mentioned example of the Rsync command to limit the maximum size file transferring.
rsync -vr --max-size='200k' /etc/Backup/ [email protected]:/home/ubuntupit/Backup
9. Delete Source Files Automatically After a Transfer
If your host PC has a limited disk space and you want to delete the file simultaneously after getting the file transferred into your system, you can use the delete flag with the Rsync command on the Linux terminal shell.
$ rsync -vr --remove-source-files Original/* [email protected]:/home/ubuntupit/backups/
10. Sync the Whole files with Remote Sync
So far, we have seen the Rsync command only sync those files that have not been edited or modified. But if you want to sync all your modified and unmodified files, you can use the Rsync command from below.
rsync -vrW Original/* [email protected]:/home/ubuntupit/Backup
11. Stop Syncing Modified Files in Destination
After selecting a whole system, if you want not to sync a specific file or folder, you can assign that path to the Rsync command so that the Rsync tool skips the path for syncing.
rsync -vu Original/* [email protected]:/home/ubuntupit/Backup
12. Add -i Flag to Monitor the Difference
To view the difference between the original file and the file after the synchronization, please use an i flag with the command.
rsync -avzi Original/ Backup/
13. Use Remote Sync to Copy Directory Structure only
This command is very interesting for those interested in copying or transferring the file; you can now use the rsync command to see the directory structure through the rsync command.
rsync -av -f"+ */" -f"- *" /home/ubuntupit/Desktop/Original/ /home/ubuntupit/Documents/
14. Add Date Stamp to Directory Name
To include a time stamp on the file while transferring it via the remote sync tool, you can use the below stamp format at the command for the destination location.
Destination directory: $(date +\\%Y-\\%m-\\%d) sudo rsync -rv /home/ubuntupit/Documents/* /etc/$(date +\\%Y-\\%m-\\%d)
15. Copying Multiple Files Remotely
So far, we have seen how to copy and transfer a single file from a remote server to a local machine or vice versa from a host pc to the local machine. Now we will see how we can add multiple files and transfer them through one Rsync command. To add multiple files, we must specify all the file paths in the command.
rsync -vr /home/ubuntupit/Documents/To_check_rsync.txt /home/ubuntupit/Desktop/To_check_rsync_two.txt /home/ubuntupit/ rsync -vr /home/ubuntupit/Documents/To_check_rsync.txt /home/ubuntupit/Desktop/To_check_rsync_two.txt [email protected]:/home/ubuntupit/Backup
As the Rsync command is written in the C programming language and performs as a single-threaded application so this tool can easily communicate with Kernel and other applications. In the entire post, we have seen the Rsync tool and examples of the Rsync Command in Linux for effectively transferring files over machines and servers.
You can even sync apache servers using the Rsync command with proper configuration and knowledge. In some cases, the Rsync command can be associated with the checksum command that can perform whether the file was modified or not. If you’re more interested in Rsync, you can also try the GUI-based Rsync tool.
Please share it with your friends and the Linux community if you find this post useful and informative. We also encourage you to write down your opinions regarding this post in the comment section.