DEV Community

Cover image for Linux Insight Blogs: rsync
Ankur Singh
Ankur Singh

Posted on

Linux Insight Blogs: rsync

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
.  ..
Enter fullscreen mode Exit fullscreen mode
  • -r flag (recursive mode) This enables you to recurse into directories.
ankur:~/Desktop$ rsync -r local/ remote
Enter fullscreen mode Exit fullscreen mode

Now

ankur:~/Desktop/remote$ ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -h flag (human readdable mode)
ankur:~/Desktop$ rsync -aPh local/ remote
sending incremental file list
Enter fullscreen mode Exit fullscreen mode
  • --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
./
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ 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

Let's connect
πŸ“§ Email: ankursingh91002@gmail.com
πŸ”— LinkedIn: Ankur Singh
πŸ”— Twitter: @ankur_136

Top comments (2)

Collapse
 
ankur0904 profile image
Ankur Singh

Any suggestions for the next blog on which Linux command line utility?

Collapse
 
roshan_kh_30ad8c03e38cfde profile image
roshan kh

Do u have devops documents please share