Skip to content

Instantly share code, notes, and snippets.

@petergi
Created January 10, 2023 23:21
Show Gist options
  • Select an option

  • Save petergi/48a1edb67cc7b06fdfdb1434d25bb25a to your computer and use it in GitHub Desktop.

Select an option

Save petergi/48a1edb67cc7b06fdfdb1434d25bb25a to your computer and use it in GitHub Desktop.

Rsync cheatsheet

oh-my-zsh rsync aliases

Alias Command
rsync-copy rsync -avz --progress -h
rsync-move rsync -avz --progress -h --remove-source-files
rsync-update rsync -avzu --progress -h
rsync-synchronize rsync -avzu --delete --progress -h

OSX

--exclude '.Trashes'
--exclude '.Spotlight-V100'
--exclude '.fseventsd'

Transfer options

-z, --compress
-n, --dry-run
    --partial   # allows resuming of aborted syncs

Display options

-q, --quiet
-v, --verbose
-h, --human-readable
    --progress
-P                     # same as --partial --progress

Skipping options

-u, --update     # skip files newer on dest
-c, --checksum   # skip based on checksum, not mod-time & size

Backup options

-b, --backup           # backup with suffix
    --suffix=SUFFIX    # default ~ without --backup-dir
    --backup-dir=DIR

Include options

--exclude=PATTERN
--include=PATTERN
--exclude-from=FILE
--include-from=FILE
--files-from=FILE    # read list of filenames from FILE

Archive options

-a, --archive    # archive (-rlptgoD)
-r, --recursive
-l, --links      # copy symlinks as links
-p, --perms      # preserve permissions
-t, --times      # preserve times
-g, --group      # preserve group
-o, --owner      # preserve owner
-D               # --devices --specials
--delete         # Delete extra files

Examples

# To copy files from remote to local, maintaining file properties and sym-links
# (-a), zipping for faster transfer (-z), verbose (-v):
rsync -avz host:file1 :file1 /dest/
rsync -avz /source host:/dest

# To copy files using checksum (-c) rather than time to detect if the file has
# changed. (Useful for validating backups):
rsync -avc <src> <dest>

# To copy /src/foo folder into destination:
# This command will create /dest/foo if it does not already exist
rsync -auv /src/foo /dest

# To copy contents of /src/foo into destination:
# This command will not create /foo_bak/foo folder
rsync -auv /src/foo/ /foo_bak

# To copy file from local to remote over ssh with non standard port 1234 to
# destination folder in remoteuser's home directory:
rsync -avz -e "ssh -p1234" <source> <username>@<host>:<dest>

# Use the specified authentication key, instead of typing a password:
# (The key can be generated by ssh-keygen, and the public key should be placed
# in remote's authorized_keys, e.g. using ssh-copy-id)
rsync -avz -e "ssh -i ~/.ssh/id_rsa" <src> <dest>

# Log into remote as a user, but promote to root, to access privileged files:
rsync -avz --rsync-path="sudo rsync" user@<src> <dest>

# Rsync only symlinks and preserve them as symlinks (dont follow them):
find /path/to/files -type l -print | \
  rsync -av --files-from=- /path/to/files user@targethost:/dest_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment