rsync (fast, versatile file copying tool)

This is the ultimate file copying utility, much richer in functionality than →cp. rsync is most often used to transfer files over a network, but the tool is equally useful for copying files locally. Invoke it like this, for example:

rsync -r --progress --partial SOURCE... DEST

SOURCE... may be one or more files/directories that you want to copy, and DEST is the destination file or directory. The option --progress will display the estimated transfer time and other useful information. The option --partial will have rsync keep partially copied files if the transfer is interrupted. Both options are very useful for copying large files. You can specify -P as a shorthand for --progress --partial. The option -r (“recursive”) is necessary for copying directories with their contents.

The biggest advantage of rsync over cp is that rsync will not copy SOURCE files that are already present at DEST, while cp will uselessly overwrite files that are already in place. Using rsync can save you a lot of time when copying an updated version of a directory over an older version. As its name suggests, rsync is a file synchronization tool.

There are many useful options to rsync and it is difficult to remember them all. Luckily, there is a shorthand -a (“archive”) for the most popular options, namely -rlptgoD. You can look them up in rsync's man page if you're interested.

If you should ever want to copy files over a network, use rsync like this:

rsync -iavz SOURCE... DEST

The option -i (“itemize changes”) will have rsync list the changes that it applies to DEST. The option -z (“zip”) means that files will be sent in compressed form, which speeds up transfer times and saves bandwidth. Finally, -v is for “verbose output”. If you add the option -n (long form: --dry-run), rsync will do a kind of simulation, showing you what it would do without actually changing anything. The options -i and -n can also be useful for local copying.

One last note. If SOURCE and DEST are directories that already exist, there is a subtle difference between typing SOURCE/ (with a slash) and SOURCE (without a slash):

  • SOURCE/ means that the contents of SOURCE will be copied to DEST. This is like saying cp -r SOURCE/* DEST.
  • SOURCE means that the directory SOURCE will be copied to DEST, resulting in DEST/SOURCE. This is like saying cp -r SOURCE DEST.