Hacker News new | ask | show | jobs
by ryandrake 1135 days ago
I've never been able to memorize rsync's syntax such that it handles directory contents the way I want to. If I want to copy the contents of directory foo to directory bar, with cp it's easy:

    cp -r foo/* bar
With rsync, I never know what to type:

    rsync -a foo bar
    rsync -a foo/* bar
    rsync -a foo/ bar/
    rsync -a foo bar/
    rsync -a foo/* bar/
I never quite know. Whatever I do, I always seem to end up with bar/foo/<stuff> until I trial and error my way to the right syntax.
3 comments

> cp -r foo/* bar

Ah, you fell into a trap already! Using foo/* like that is generally "weird", since you let the shell expand. It will, for example, miss files that start with a dot ("."). But not necessarily, it depends on what the shell does!

As for the "trailing /" syntax, I know what you mean, but once you've internalized it, it's easy.

Without trailing slash: Copy the directory (and its contents). With trailing slash: Copy what's in the directory. This makes sense, because with the / you say you want to go "into the directory" and copy then.

And the latter includes files starting with . and all of that, you don't have to worry about it, or about what the shell would do, it's all the directory contents.

So what you want for your example is:

    rsync -a foo/ bar
rsync -r foo/* bar would do the same (wrong) thing as cp -r foo/* bar, for the same reasons, the syntax is the same in that regard.
> Without trailing slash: Copy the directory (and its contents). With trailing slash: Copy what's in the directory. This makes sense, because with the / you say you want to go "into the directory" and copy then.

Semi-related: with other stuff symlinks act the same way, for example "ls foo" will show you the symlink (even if it's to a directory), "ls foo/" will traverse the symlink (and show you what's inside the directory).

Note that a trailing slash means to copy the contents when using rsync, but not when using cp (which will copy the directory even if there is a trailing slash). For cp, the correct way to copy only the contents is to use `cp -r foo/. bar`. (This also works with rsync.)
The source is the only thing to worry about. Without a trailing slash it makes a copy of the directory, and fills it. With a trailing slash, it copies just the *Contents* of the directory.

    rsync -avP foo bar   # puts foo inside bar so bar/foo/* 
    rsync -avP foo/ bar  # puts the the contents of foo, in bar (without making foo)
I like to add the v & P flags for verbosity & progress.
It turns out "bar/" is equivalent to "bar/."

That means "bar/" is just a contraction of "bar/."

For some reason I find the presence of the "." makes it much easier to reason about. Probably that because turns it into the same pattern as "rsync foo/x bar/y". Most people know what that does, even if "foo/x" or "foo/y" are directories.