rsync 

Send to Kindle
home » snippets » linux » rsync



Examples

# rsync and preserve more and also delete
rsync -aAHX --delete --progress --exclude '.Trashes' --exclude '*.pyc' src/ dst/

# rsync like behavior
rsync -avz --delete --progress --exclude '.Trashes' --exclude '*.pyc' src/ dst/

# don't preserve permissions and times
rsync -rlDvz --delete --progress --exclude '.Trashes' --exclude '*.pyc' src/ dst/

# Simpler (if you won't encounter special files when traversing.)
rsync -rl --delete src1/ src2/ dst/

# copy a directory resolving the symlinks that point outside the tree along the way.
rsync --progress -rlv --copy-unsafe-links --delete ~/play/webdev/dart/node_bind_ex01/ ant.c-k.me:~/ck/web_roots/play/webdev/dart/node_bind_ex01/

Command line options

See also: Full List of Options

Flag * Description
--backup-dir=DIR Make backups into this directory
--delete * delete extraneous files from dest dirs
--devices preserve device files (super-user only)
--force force deletion of dirs even if not empty
--log-file=FILE * log what we're doing to the specified FILE
--partial keep partially transferred files
--progress show progress during transfer
--specials preserve special files
--stats * give some file-transfer stats
--super * receiver attempts super-user activities
-0, --from0 all from/filter files are delimited by 0s
-A, --acls preserve ACLs (implies -p)
-D same as --devices --specials
-H, --hard-links * preserve hard links
-L, --copy-links when symlinks are encountered, the file that they point to is copied, rather than the symlink
--copy-unsafe-links * copy the referent of symbolic links that point outside the source tree.  Absolute symlinks are also treated like ordinary files, and so are any symlinks in the source path itself when --relative is used.
--safe-links * ignore symbolic links that point outside the destination tree.  All absolute symlinks are also ignored. Using this option in conjunction with --relative may give unexpected results.
-P * --partial --progress
-R, --relative use relative path names
-S, --sparse * handle sparse files efficiently
-X, --xattrs preserve extended attributes
-a Equivalent to -rlptgoD
-b, --backup Make backups (see --suffix & --backup-dir)
-c, --checksum skip based on checksum, not mod-time & size
-f, --filter=RULE * add a file-filtering RULE
-g, --group preserve group
-h, --human-readable * output numbers in a human-readable format
-i, --itemize-changes output a change-summary for all updates
-l, --links copy symlinks as symlinks
-o, --owner preserve owner (super-user only)
-p, --perms preserve permissions
-r, --recursive recurse into directories
-s, --protect-args * Send filenames and most options to the remote rsync without allowing the remote shell to interpret them.
Spaces are not split in names, and any non-wildcard special characters are not translated
(such as ~, $, ;, &, etc.<br>).
Wildcards are expanded on the remote host by rsync (instead of the shell doing it).
-t, --times preserve modification times
-u, --update update only (don't overwrite newer files)
-x, --one-file-system * don't cross filesystem boundaries

-a-rlptgoD

-a is equivalent to -rlptgoD.  For handy reference, here's what they mean.

Flag Description
-r, --recursive recurse into directories
-l, --links copy symlinks as symlinks
-p, --perms preserve permissions
-t, --times preserve modification times
-g, --group preserve group
-o, --owner preserve owner (super-user only)
-D same as --devices --specials

-R, --relative: Use relative paths

Use relative paths. This means that the full path names specified on the command line are sent to the server rather than just the last parts of the filenames. This is particularly useful when you want to send several different directories at the same time. For example, if you used the command

rsync foo/bar/foo.c remote:/tmp/

then this would create a file called foo.c in /tmp/ on the remote machine. If instead you used

rsync -R foo/bar/foo.c remote:/tmp/

then a file called /tmp/foo/bar/foo.c would be created on the remote machine. The full path name is preserved.

Filter (and Include / Exclude patterns)

Note

rsync checks each name to be transferred against the list of include/exclude patterns in turn, and the first matching pattern is acted on:

Rsync builds an ordered list of filter rules as specified on the command-line. Filter rules have the following syntax:

RULE [PATTERN_OR_FILENAME]
RULE,MODIFIERS [PATTERN_OR_FILENAME]

You have your choice of using either short or long RULE names, as described below. If you use a short-named rule, the ',' separating the RULE from the MODIFIERS is optional. The PATTERN or FILENAME that follows (when present) must come after either a single space or an underscore (_). Here are the available rule prefixes:

Rule Description
exclude, - specifies an exclude pattern.
include, + specifies an include pattern.
merge, . specifies a merge-file to read for more rules.
dir-merge, : specifies a per-directory merge-file.
hide, H specifies a pattern for hiding files from the transfer.
show, S files that match the pattern are not hidden.
protect, P specifies a pattern for protecting files from deletion.
risk, R files that match the pattern are not protected.
clear, ! clears the current include/exclude list (takes no arg)

When rules are being read from a file, empty lines are ignored, as are comment lines that start with a "#".

Modifiers

The following modifiers are accepted after a "+" or "-":

Patterns

Note that, when using the --recursive (-r) option (which is implied by -a), every subcomponent of every path is visited from the top down, so include/exclude patterns get applied recursively to each subcomponent's full name (e.g. to include "/foo/bar/baz" the subcomponents "/foo" and "/foo/bar" must not be excluded). The exclude patterns actually short-circuit the directory traversal stage when rsync finds the files to send. If a pattern excludes a particular parent directory, it can render a deeper include pattern ineffectual because rsync did not descend through that excluded section of the hierarchy. This is particularly important when using a trailing '*' rule. For instance, this won't work:

+ /some/path/this-file-will-not-be-found
+ /file-is-included
- *

This fails because the parent directory "some" is excluded by the '*' rule, so rsync never visits any of the files in the "some" or "some/path" directories. One solution is to ask for all directories in the hierarchy to be included by using a single rule: "+ */" (put it somewhere before the "- *" rule), and perhaps use the --prune-empty-dirs option. Another solution is to add specific include rules for all the parent dirs that need to be visited. For instance, this set of rules works fine:

+ /some/
+ /some/path/
+ /some/path/this-file-is-found
+ /file-also-included
- *

Examples