Skip to content

Instantly share code, notes, and snippets.

@ichoake
Forked from crankin/tips--rsync.md
Created November 8, 2022 20:27
Show Gist options
  • Select an option

  • Save ichoake/f51e16e741fd88698595835d1b307e7f to your computer and use it in GitHub Desktop.

Select an option

Save ichoake/f51e16e741fd88698595835d1b307e7f to your computer and use it in GitHub Desktop.

rsync notes

rsync -av --delete [source] [destination]

rsync [options] source destination sftp://198.101.206.164//home/klru/klru.org rsync -e ssh -avz AllReviews \

The -e ssh option tells rsync to use the secure shell connection. The -a option does an archive and preserves access modes (permissions, owner and group), the -v option means verbose (returns details about the copy process), and the -z option compresses the files using gzip.

rsync -avz --delete --exclude '.DS_Store' [email protected]:/Users/gina/Documents/ d:/data/mac

Let's break this command down. Here you're calling rsync using the -a, v, z options (archive, verbose and use compression, respectively). It should delete any files that exist on the destination that don't exist on the Mac (—delete), and it should exclude all files called '.DS_Store' (—exclude, because we don't need to backup the Mac's annoying little system files). The file source is /Users/gina/Documents on the Mac (addressed at 192.168.3.2, as specified in the Mac's System Preferences pane) and the destination is the d:/data/mac folder on the PC.

You must replace your source user@server:/file/path and destination path with your computers' details. Once you do and hit Enter, rsync will spring into action, and start transferring the files on the Mac and mirroring them in the destination directory on your PC. The -v switch (verbose) means you'll see all the action as it's happening.

Rsync switches

Like all good command line interaction, the secret sauce which bends rsync to your will lies in the usage switches you provide it in the rsync call (ie, rsync -avz). To see all the available options, type rsync -h at the command line. A few of the more interesting ones are: -r, --recursive          recurse into directories -u, --update             skip files that are newer on the receiver -n, --dry-run            show what would have been transferred     --existing           only update files that already exist on receiver     --delete             delete files that don't exist on the sending side -z, --compress           compress file data during the transfer     --exclude=PATTERN    exclude files matching PATTERN

If you're just getting started with rsync, the -n ("dry run") switch with -v (verbose) is a great way to see what files would get copied without actually performing the copy. Use that switch to test out your rsync recipe before you run it.

rsync [options] [source] [target]



The options I used in my example are the defaults I use for most of my rsyncing. Here is a breakdown of the options I used and how the affect the outcome of the rsync:



-a  - "archive" rsync, includes ownership info and extended attributes extremely useful for moving large volumes of data and keeping AD/OD/POSIX permissions intact



-v - "verbose" gives the user more information on the rsync display



-x  - prevents crossing filesystem boundaries

--progress  - combined with the "-v" option, gives you the best in-terminal display of rsync's progress

Now that you've exposed yourself to a very basic rsync, here are a few tips to make using this software easier to use. Not only will these tips help you speed up your rsync use, some of them will help with you feeling comfortable on the command line overall.



First, rsync has an option that allows a "dry run", so that you can test an rsync execution without actually moving any data. By default I always include this option in my first run of an archive to make sure my directories in order. This option is:



-n  - "test run", "dry run", shows output but doesn't actually copy anything



Very often this just translates to adding an "n" to your option string, so instead of typing 

 -avx you will type 

 -avxn   The second tip for efficient command line execution of rsync is how your "Tab" key operates when typing out directory names. If you think you need to type out "/Volumes/Macintosh\ HD/Documents/blahbalhbalbhalbahaba" every single time, you are wrong! Here are a few CLI shortcuts to help you avoid excessive typing:



up arrow / down arrow - cycle through commands previously typed 
 TAB  - autofill known directory names, for example, if I type "/Volu" and hit TAB the CLI will auto-fill the rest and display "/Volumes".



Lastly, spaces in command line directory names can be a little frustrating. Make use of the backslash to indicate where a space exists:

directory name:        /Volumes/MacHD/Users/Joe/Stuff I Like/

is actually

directory name:        /Volumes/MacHD/Users/Joe/Stuff\ I\ Like/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment