Linux: Sync Files Across Machines: rsync Tutorial

, , …,

This page is a tutorial on how to copy or sync between 2 machines using {rsync, unison, curl, wget}, and unix utilities for comparing directories.

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/ jane@example.org:~/

This will copy the local dir ~/web/ to the remote dir ~/ on the machine with domain name “example.org”, 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/ jane@example.com:~/

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@169.254.125.147:~/web/

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. 〔➤ Linux: File Permission System Tutorial

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

Two-way Synchronize with Unison

How to Use Unison for Syncing Files (Unison tutorial)

Downloading a Entire Website

Linux Tutorial: How to Fetch Websites by Command Line? wget, curl, HEAD, GET

Comparing Files and Directories

See: Linux: Comparing Files and Dir: diff.

See also: Linux Shell Text Processing Tutorial: grep, cat, awk, sort, uniq, …

blog comments powered by Disqus