100 Useful Command-Line Utilities

by Oliver; 2014

29. rsync

rsync can remotely sync files to or from a computer to which you have ssh access. The basic syntax is:
rsync source destination
For example, copy files from a remote machine:
$ rsync username@host:/some/path/on/remote/machine /some/path/on/my/machine/
Copy files to a remote machine:
$ rsync /some/path/on/my/machine username@host:/some/path/on/remote/machine/
You can also use it to sync two directories on your local machine:
$ rsync directory1 directory2
The great thing about rsync is that, if it fails or you stop it in the middle, you can re-start it and it will pick up where it left off. It does not blindly copy directories (or files) but rather syncronizes them—which is to say, makes them the same. If, for example, you try to copy a directory that you already have, rsync will detect this and transfer no files.

I like to use the flags:
$ rsync -azv --progress username@myhost.university.edu:/my/source /my/destination/
The -a flag is for archive, which "ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer"; the -z flag compresses files during the transfer; -v is for verbose; and --progress shows you your progress. I've enshrined this in an alias:
alias yy="rsync -azv --progress"
Preview rsync's behavior without actually transferring any files:
$ rsync --dry-run username@myhost.university.edu:/my/source /my/destination/
Do not copy certain directories from the source:
$ rsync --exclude mydir username@myhost.university.edu:/my/source /my/destination/
In this example, the directory /my/source/mydir will not be copied, and you can omit more directories by repeating the --exclude flag.

Copy the files pointed to by the symbolic links ("transform symlink into referent file/dir") with the --L flag:
$ rsync -azv -L --progress username@myhost.university.edu:/my/source /my/destination/

Note: using a trailing slash can alter the behavior of rsync. For example:
$ mkdir folder1 folder2 folder3 # make directories
$ touch folder1/{file1,file2}   # create empty files
$ rsync -av folder1 folder2     # no trailing slash
$ rsync -av folder1/ folder3    # trailing slash
No trailing slash copies the directory folder1 into folder2:
$ tree folder2
folder2
└── folder1
    ├── file1
    └── file2
whereas using a trailing slash copies the files in folder1 into folder2:
$ tree folder3
folder3
├── file1
└── file2

<PREV   NEXT>