213 points · by pabs3 · 4y ago · 51 comments
Sometimes people would complain that they didn't need a "beginner" class but I bet there wasn't a single person who didn't learn some important subtlety like how the trailing / works in different situations.
I've never had another company require or even offer a training like that. Do other companies still teach this stuff?
edit: typos
I will also note that I think skills like this end up being way more valuable to me as a mentor than hard CS knowledge.
It was a tech support job for mostly dial up customers. The manager had developed a binder of training materials that included many things to help us troubleshoot.
This annoys people who know rsync well, but newbies are very happy with it!
Even though I know rsync extremely well I am super careful with this. Test first with `--dry-run`!
rsync has "weird" syntax for a reason. Unlike other unix-like commands, it treats trailing slashes as significant AND consistently. If a directory has a trailing slash, it means "contents" of the directory". No slash means "the directory itself". These are two different concepts, and a program that copies directories should take the difference into account. scp (and cp, for that matter) don't take this difference into account. That leads to gotchas with recursive (-r) copying. Most importantly, scp isn't idemopotent:
scp -r fromdir todir
If todir doesn't exist, scp will copy the contents of directory fromdir to a new directory named todir.
Execute the same command again (now that todir exists), and scp will copy fromdir to todir/fromdir .
On the other hand:
rsync -a fromdir/ todir
will always copy the contents of fromdir into a directory named todir (effectively, a directory rename operation), whether todir exists or not.
rsync -a fromdir todir
will always copy the directory fromdir into the directory todir, whether todir exists or not.
These rsync operations are idempotent, which it important because rsync is designed to incrementally re-sync directories. It is expected that it will commonly be run more than once, which is why it needed to address this IMHO fundamental bug/limitation in cp and scp.
Because such cases are so rare, it does not make any difference for me if I have to add "/*" instead of the slightly shorter "/" (even when I also have to add "/.*" for hidden files/directories).
So I would like if rsync would have a command-line option to ignore trailing slashes, like other programs have (and which I always use with them).
As it is, rsync is the only program where I have to be extra cautious before executing a command-line, because the directory names are in most cases provided by auto-complete, and most shells terminate them with a trailing slash, which I have to be very careful to always delete, which slows me a lot.
An exception is when using zsh, which sometimes, but not always, deletes itself the trailing slash added by auto-complete (zsh might be confused by the aliases used by me, so it usually deletes the trailing slashes when they do not matter, but retains them when I would want them to be deleted).
When you are worrying about idempotent re-sync of trees, I think it is so much more important to think about where the roots of the synchronization should match up between local and remote, and to make that explicit in the calling convention.
rsync -Ra /src-only/./{a,b,c} remote:/remote-only/./
I also think you should be writing this idempotent call in a script to presumably be reused over time, and this helps document the goal.I think it's `cp` that uses that syntax
To make backups, you clone a directory into the rsync repository, and when you later want to update the backup, you pull the newest changes from the directory into the repository.
And it would be impossible to destroy the backup by forgetting a slash or syncing another directory on the wrong backup because it would remember which directory belongs to which backup repository.
This way there can be no confusion whether you're copying the contents of a directory or the whole directory, and you can't create a duplicated directory within your destination by mistake.
Unfortunately, rsync lacks a helpful option like the "--strip-trailing-slashes" of GNU cp and mv.
I never use bare cp or mv, but only aliases which replace their undesirable default options with other more useful options, including "--strip-trailing-slashes", which makes the presence or absence of a trailing slash irrelevant.
tl;dr: Basically, it's a way to say "do this operation inside this folder".
For example, if you do
cp -r A B
an A is a directory, the behavior is different depending on whether B already exists or not. If B is a file, you get an error. If B is a directory, then it tries to make a copy of A inside B (i.e. B/A). If B doesn't exist, B itself becomes a copy of A.This is pretty terrible behavior. The question is how do you avoid this. On Linux, if you always want A to be overlaid on top of B, then you can do
cp -T A B
which disambiguates in favor of overlay. On the other hand, if you always want A to be copied inside B, then you can do cp A B/.
which disambiguates in favor of creating a subdirectory (and gives you an error if B doesn't exist).Note: I have not checked whether this behavior is uniform across other POSIX systems, but I hope it is.
Reflink copies are basically invisible, except when you need to meter disk usage. Symlinks and hardlinks add a billion corner cases to file handling.