Introduction
Welcome π to this blog. In this blog, we will learn about the rsync
command-line utility on Unix/Linux systems. A command-line utility is a program or tool that you run using the CLI(Command Line Interface) instead of the GUI(Graphical User Interface)
rsync
rsync stands for remote sync. It is a fast and extraordinarily file-copying tool. It is capable of file transferring from a local host to any remote host. Rsync is most popular for its delta-transfer algorithm, which allows only to send data by sending only the differences between the source files and the existing files in the destination. Rsync finds files that need to be transferred using a lqquick checkrq algorithm.
Let's get our hands dirty with code
I have two folder directories with the name local and remote. I am treating local as the file system in my local computer and the remote as the server file system to transfer the data. Don't worry you can do the same for the server as well only a small syntax will change nothing else.
ankur:~/Desktop/local$ ls
file1.txt file2.txt file3.txt file4.txt file5.txt
ankur:~/Desktop/remote$ ls -a
. ..
- -r flag (recursive mode) This enables you to recurse into directories.
ankur:~/Desktop$ rsync -r local/ remote
Now
ankur:~/Desktop/remote$ ls
file1.txt file2.txt file3.txt file4.txt file5.txt
Mind the "/" after the local other wise, you output be like remote/local/file{1..5}.txt
- -a flag (archive mode) This enables you to recurse and want to preserve almost everything(symbolic links, special and device files, modification times, groups, owners, and permissions)
ankur:~/Desktop$ rsync -a local/ remote
- -P flag (partial mode) By default, rsync will delete any partially transferred file if the transfer is interrupted. In some circumstances it is more desirable to keep partially transferred files. The -P flag will help us to achieve this.
ankur:~/Desktop$ rsync -aP local/ remote
sending incremental file list
- -h flag (human readdable mode)
ankur:~/Desktop$ rsync -aPh local/ remote
sending incremental file list
- --delete flag
To keep the directories truly in sync file must be deleted from the destination directory as well if they are deleted from the source as rsync by default doesn't do it automatically but you can always overwrite this. Using --delete flag, we can achieve the same.
ankur:~/Desktop$ rsync -aPh --delete local/ remote
sending incremental file list
deleting file1.txt
./
- -v flag increase verbosity in the output of the command.
ankur:~/Desktop$ rsync -aPhv --delete local/ remote
sending incremental file list
./
file1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=4/6)
sent 197 bytes received 38 bytes 470.00 bytes/sec
total size is 0, speedup is 0.00
There are at least hundreds of flags you can use according to your needs. You are always welcome to check out the different commands from the man page.
For connecting server using SSH
rsync -avz local/ username@remote:/remote/destination/
π You nailed it
Youβve now learned how to use the rsync command for syncing your remote server with your local directory. Itβs time to open your terminal and try these commands on your own system. Play around, explore, and see what's happening under the hood of your machine. Got something cool or unexpected? Share it...
References
- https://linux.die.net/man/1/rsync
- https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories
Let's connect
π§ Email: ankursingh91002@gmail.com
π LinkedIn: Ankur Singh
π Twitter: @ankur_136
Top comments (2)
Any suggestions for the next blog on which Linux command line utility?
Do u have devops documents please share