You are reading the article How To Master The Rsync Command In Linux updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How To Master The Rsync Command In Linux
As its name suggests, rsync is a synchronization tool. The first time you use it, it simply copies files and directories from one location to another. Subsequent uses, however, synchronize only the differences between these locations. So, if you add one file to the source directory, rsync will copy only the new file to the destination. This saves a lot of time and bandwidth, especially when copying to a remote location.
Imagine you have 50GB of data you have to synchronize to a remote server. You can schedule rsync to run daily. Instead of having to copy the whole 50GB over and over again, the utility will only send the differences. This might mean it only has to upload a few megabytes of data every day, instead of gigabytes.
In the featured picture above, you can see how the first time 104MB were transferred, while during the second use only 31KB were required.
How to Install rsync on Various Linux DistributionsOn Debian and Debian-based distributions, like Ubuntu, you can install rsync with:
On Fedora, install with:
dnf
install
rsyncAnd on distributions derived from RedHat (e.g., CentOS):
yum install
rsyncTo install rsync on Arch Linux, use the command:
pacman-S
rsyncOn openSUSE, use:
zypperinstall
rsyncIf it isn’t included in your default repositories, consult the openSUSE rsync installation page.
How to Use rsync to Synchronize Local Files or DirectoriesThe general syntax of rsync is: rsync [options] /path/to/source /path/to/destination.
[options] stands for command line options which you can find in the manual:
Press q to quit the manual.
To copy a file:
When you deal with very large files, you can use this command:
rsync-P
/
path/
to/
file
/
path/
to/
destinationThis will show progress and allow you to resume an interrupted transfer. In case you need to resume, use:
rsync-P
--append
path/
to/
file
/
path/
to/
destinationWithout --append, the transfer would start from scratch.
To synchronize directories:
rsync-av
/
path/
to/
source
/
path/
to/
destinationExample command:
rsync-av
/
bin . Effect of Trailing Slash in rsync (Important)In the previous command note that the path to the source directory doesn’t contain a trailing slash / (/bin vs /bin/). Without a trailing slash, rsync copies the directory itself. So rsync -av /bin . results in what is depicted in the following image.
When a trailing slash is used, rsync copies the contents in that directory. In this case every file in /bin would be scattered around in the destination directory. So rsync -av /bin/ . would result in what is depicted in the following image.
It’s very important to remember this difference, as using TAB to autocomplete directory names automatically adds a trailing slash at the end. So keep that in mind to avoid creating a mess in the destination directory.
How to Use rsync to Synchronize with Remote Locations (e.g. Servers)What was described in the previous section holds true when working with remote locations. The only difference is that you have to prepend the login username and address of the remote location. To send your /bin directory to a remote computer, use:
rsync-av
/
bin your_username@
203.0.113.10:/
home/
your_usernamersync connects through OpenSSH. This means you have to set up the OpenSSH server and login credentials on the remote destination beforehand. Note that your_username refers to the username created on the server. This might be different from the username you have on your local machine.
If you own a domain name and point it to your server IP, you can use the domain in the command:
To copy files from the remote destination to your local computer, just reverse locations:
This covers the most important rsync commands you need to know. If you ever need more control over how to synchronize directories, consult the online rsync manual and scroll a few pages down until you find “Options Summary.”
Alexandru Andrei
Fell in love with computers when he was four years old. 27 years later, the passion is still burning, fueling constant learning. Spends most of his time in terminal windows and SSH sessions, managing Linux desktops and servers.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time.
You're reading How To Master The Rsync Command In Linux
Update the detailed information about How To Master The Rsync Command In Linux on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!