Avatar
Linux, plaintext & minimalism
September 28, 2019

Copy files with rsync for a better overview

Copying larger files and/or larger amounts of files with the shell command cp can sometimes be a bit lacking. It doesn’t give you any feedback whatsoever, which is almost never an issue for your average small file, but when you copy perhaps several gigabytes of files you want to know if it’s going to take 10 minutes or 10 hours.

A way around this is to use rsync, the tool most people probably associate with making backups, but with the right flags it’s actually a really good verbose tool for simply copying files. Here’s an example when I copied a folder for a virtual machine:

$ rsync -ah --progress VirtualBox/vhost-cloud-new /media/HDD0/
sending incremental file list
vhost-cloud-new/
vhost-cloud-new/vhost-cloud-new.vbox
          3.29K 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=7/9)
vhost-cloud-new/vhost-cloud-new.vbox-prev
          3.29K 100%    3.14MB/s    0:00:00 (xfr#2, to-chk=6/9)
vhost-cloud-new/vhost-cloud.vdi
          3.43G  19%  111.59MB/s    0:02:00

As you can see rsync will show you the size of the file, the current progress, the current speed and the estimated time left for the file it’s currently copying:

3.43G  19%  111.59MB/s    0:02:00

Something to know about rsync and folders

When you copy folders with rsync you need to make sure that you end the folder name with a forward slash (folder/) when you want to copy the content of the folder, and don’t end it with a forward slash (folder) when you want to copy the folder itself.

Example

This command will copy the folder to the destination:

$ rsync -ah --progress VirtualBox/vhost-cloud-new /media/HDD0/

This command will copy the content of the folder to the destination:

$ rsync -ah --progress VirtualBox/vhost-cloud-new/ /media/HDD0/

Explanation of the flags

-a, –archive Archive mode; equals -rlptgoD (no -H,-A,-X).
-r, –recursive Recurse into directories.
-l, –links Copy symlinks as symlinks.
-p, –perms Preserve permissions.
-t, –times Preserve modification times.
-g, –group Preserve group.
-o, –owner Preserve owner (super-user only).
-D same as --devices --specials;
--devices Preserve device files (super-user only).
--specials Preserve special files.

Makings things easier with an alias

It’s not that convenient to type rsync -ah --progress <source> <destination> every time, so I highly suggest you creating an alias for it. I made one that I named cpr.

Just add the snippet alias cpr='rsync -ah --progress' to either your ~/.bashrc or whatever shell you’re using and then reload the configuration with the command source ~/.bashrc.