Rsync command in Linux examples
rsync is an open-source utility that stands for remote sync. rsync is free to use file transfer tool and synchronization tool that provides fast incremental file transfer. The rsync daemon can now handle a client address with an implied “%scope” suffix. Rysnc is also used as an incremental backup tool in Linux.
What Is The Use Of Rsync?
Rsync is basically used to copy files locally or in between remote hosts. Rsync communicates with a remote host using a remote-shell program such as ssh or rsh or by using its own rsync daemon directly via TCP. Rsync can use any transparent remote shell, including ssh or rsh and it doesn’t require any super usage privileges too. Rsync does not support copying files between two remote hosts.
Along with the files, Rsync also copies links, devices, owners, groups, and permissions of the respective files.
Rsync Command In Linux With Examples [Updated]
Rsync comes pre-installed on most Linux distributions but you can also install the rsync package with the help of the following commands.
1. Install Rsync On Ubuntu/Debian/Mint
Command to install Rsync on Ubuntu, Debian & Mint.
sudo apt-get install rsync
2. Install Rsync On Arch Linux
Command to install Rsync on Arch Linux
pacman -S rsync
3. Install Rsync On Fedora/CentOS/RHEL/Rocky Linux/AlmaLinux
sudo dnf install rsync
4. Install Rsync On openSUSE
sudo zypper install rsync
5. Install Rsync On Gentoo
emerge sys-apps/rsync
Rsync Command To Copy/Sync Files And Directory Locally
Command to copy/sync files locally:
Let’s have a look into the command for copying files and directories locally from one location to another location. using rsync command. In this example, we will take a file example.tar that needs to be copied to another folder. If the destination folder is not available then this command will create one for you.
rsync -zvh example.tar /home/itsubuntu/foldername created directory /home/itsubuntu/foldername example.tar
Command to copy/sync a directory on Local Computer:
The following command will transfer or sync the content of one directory to a different directory in the same machine.
rsync -avzh /root/blogfile_directory /tmp/backups/
Rsync To Copy/Sync Files And Directory To Or From A Remote Server
Command to copy a Directory from Local Server to a Remote Server:
The following command will sync a directory from a local machine to a remote machine.
rsync -avzh /root/blogfiledirectory [email protected]:/root/
Command to Copy/Sync a Remote Directory to a Local Machine
The following command will sync a remote directory to a local directory.
rsync -avzh [email protected] :/root/directoryfromremotemachine /localmachine/mydirectory
How to Transfer Files with Rsync over SSH
SSH is the most popular and secure protocol for data transfer. You can transfer files and directories with rsync over SSH from and to remote servers. While using rsync, you need to specify a protocol with rsync with the “-e” option and the protocol name you want to use.
Command to Copy a File from a Remote Server to a Local Server with SSH
rsync -avzhe ssh [email protected]:/root/example.cfg /localmachine/mydirectory
Command to Copy a File from a Local Server to a Remote Server with SSH
rsync -avzhe ssh example.tar.gz [email protected]:/remoteserverdir/
How to Transfer Files with Rsync By Excluding file
You need to use two options include and exclude while transferring files if you want to include or exclude certain files by specifying parameters.
The basic syntax for the rsync exclude option looks like this:
rsync [OPTIONS] --exclude 'file_or_directory' source/ destination/
Run the following command to exclude a file while copying the directory.
rsync -av --exclude 'testfilename.txt' sourcedir/ destinationdir/
If you want to exclude the specific directory, then run the following command:
rsync -av --exclude 'dirname' sourcedir/ destinationdir/
Exclude a Specific File Type using Rsync
If you want to exclude a certain file type while copying data then use the following command.
rsync -av --exclude '*.mp3' sourcedir/ destinationdir/
.mp3 files will be excluded from the list.