Linux: Sync Files Across Machines: rsync Tutorial

By Xah Lee. Date: . Last updated: .

This page is a tutorial on how to sync between 2 machines using rsync.

Note: common unix shell utilities such as {find, xargs, ps, diff, basename, …} on this page are the GNU version. Most linuxes is bundled with GNU versions. (as opposed to the BSD, Solaris, or other unix versions. GNU and BSD versions differ in options and features, and are not compatible. On Mac OS X, some utils are GNU while others are BSD. One easy way to tell is that if the tool supports the syntax --version, and check if its output says GNU.)

One-Way Copying a Directory to Server with rsync

How to copy local directory to a remote machine, in one shot?

For a one-way copying (or updating), use rsync. The remote machine must have rsync installed. Example:

rsync -z -a -v -t --rsh="ssh -l jane" ~/web/

This will copy the local dir ~/web/ to the remote dir ~/ on the machine with domain name “”, using login “jane” thru the ssh protocol.

For example, here's what i use to sync/upload my website on my local machine to my server.

rsync -z -a -v -t --exclude="*~"  --exclude=".DS_Store" --exclude=".bash_history"  --exclude="*/_curves_robert_yates/*.png" --exclude="logs/*"  --exclude="xlogs/*" --delete --rsh="ssh -l jane" ~/web/

The --exclude tells it to disregard any files matching that pattern (i.e. if it matches, don't upload it nor delete it on remote server)

Here's a example of syncing Windows and Mac.

rsync -z -r -v --delete --rsh="ssh -l xah" ~/web/ xah@

Note that -r is used instead of -a. The -r means recursive, all sub directories and files. Don't use -a because that will sync file owner, group, permissions, and others, but because Windows & unix have different permission systems and file systems, so -a is usually not what you want. 〔➤see Linux: File Permission System

You can create a bash alias for the long command ➢ for example: alias l="ls -al";, or use bash's back history by 【Ctrl+r】 then type “rsync”. 〔➤see Emacs: Interactive Abbrev in Shell

Two-way Synchronize with Unison

How to Use Unison for Syncing Files (Unison tutorial)

Downloading a Entire Website

Linux: Download Website by Command: wget, curl, HEAD, GET

Comparing Files and Directories

See: Linux: Comparing Files and Dir: diff.

See also: Linux: Text Processing: grep, cat, awk, uniq