rsync cheat sheet
Things I use rsync for:
- deploy static webpage to server:
rsync -rvzlpt --delete "out/" "user@myserver:/path/to/served/folder/" - backing up pictures onto my HDD:
rsync -av "/path/to/pictures/folder/picts/" "/path/to/mounted/disk/picts/"
rsync is a good program for these tasks because it will only copy the file if it changed.
Connecting to servers is done with ssh. If you can ssh user@server you can rsync user@server:/some/path.
flags
rsync -v: print which file is being worked onrsync -z: use compression in transit (really useful when copying files over the internet)rsync --dry-run: don’t actually write anything to the destinationrsync --delete: delete files at the destination that no longer exist at the source
the -a flag is “archive mode”, which enables all of -rlptgoD:
rsync -r: recursiversync -l: copy symlinks as symlinksrsync -p: copy the permission bitsrsync -t: copy the modification timesrsync -g: copy owning group idrsync -o: copy owning user idrsync -D: copy devices and copy “specials” (named sockets and fifos? character and block devices?)
Some of this is overkill unless you’re doing “backups”; and copying from one computer to another can result in nonsense group/ownership information if the computers don’t have their groups and users set up exactly the same way.
trailing slashes
let’s say we have a file at /path/to/source/hello.txt
rsync path/to/source/ path/to/destwill create the filepath/to/dest/hello.txtrsync path/to/source path/to/destwill create the filepath/to/dest/source/hello.txt
A trailing slash on the source is usually what you want. No-trailing-slash is more like “dragging-and-dropping source into dest using a graphical file manager.”
trailing slash on the destination
For completeness, even though this only makes a difference iff the source is a file and the dest doesn’t exist, which is kind of a weird way to use rsync:
rsync path/to/source/hello.txt path/to/dest/will create the directorypath/to/destand put the filepath/to/dest/hello.txtinsidersync path/to/source/hello.txt path/to/destwill create the filepath/to/destwith the contents ofhello.txt
i.e.
- if there is a trailing slash, it will create a directory with that name and copy the file inside
- if there is no trailing slash, it copies the file to
dest(basically it acts likecp)
cp would error in the first case (copying to a trailing-slashed path). rsync is implementing a “do what i mean” behavior here
Termux
Copying from Termux onto a normal computer is probably going to mangle the permission bits and owning group/user information on the destination. (Termux runs under some weird umasks, and rsync dutifully copies them.)
I work around this with an ssh dest_computer "chmod +rX -R /path/to/dest/; chown owning_user:owning_group -R /path/to/dest/" lmao, kind of a sledgehammer but it works.
TODO: Maybe rsync --super will also help (“This tells the receiving side to attempt super-user activities even if the receiving rsync wasn’t run by the super-user”)