225

I'm trying to synchronize two contents of folders with different name:

rsync -av ~/foo [email protected]:/var/www/bar

I'd like to copy the content of foo into bar at remote host, but not the directory foo itself. I've tried something like foo/*, but rsync doesn't support that.

rsync always creates

/var/www/bar/foo
0

4 Answers 4

338

rsync interprets a directory with no trailing slash as copy this directory, and a directory with a trailing slash as copy the contents of this directory.

Try rsync -av ~/foo/ [email protected]:/var/www/bar/

Sign up to request clarification or add additional context in comments.

7 Comments

This behavior is odd, compared to mv or cp.
Seems weird and inconsistent to make the trailing slash relevant for just this particular command. Wonder why it hasn't been changed.
In case someone was also wondering why this works: it's the trailing / after the directory name.
I also found it odd that the -a option was needed. If I don't use '-a' then I'll get an error skipping the directory and nothing happens. Reading the man pages, I wouldn't have concluded that. It seems like based on the docs that it should work without the -a option and the -v option is only for verbosity so that isn't relevant to the actual copy working. Weird. Thanks for the answer though.
I'm just speculating, but it might be that in all honesty the way * works is pretty strange. * expands, so if you have many files in a directory, your command cp src/* dest expands into potentially a massive command. Again, all speculation, but maybe rsync moved away from it for this reason
|
104

It's simple,

rsync /var/www/ /home/var - copies the contents of /var/www/ but not the www folder itself.

rsync /var/www /home/var - copies the folder www along with all its contents.

Comments

36

Not related only to rsync, but when you are looking for examples on how to use a GNU/Linux command, you can use "eg" which displays explicit examples. eg is available here, with instructions on how to install it: https://github.com/srsudar/eg

The result for eg rsync is as follow

# rsync


copy the folder source_dir and its content into destination_dir

    rsync -av source_dir destination_dir


copy the contents of source_dir (trailing slash) into destination_dir

    rsync -av source_dir/ destination_dir

1 Comment

just to note, without compression is currently (in 2024) the preferred method, unless you're transferring huge files over a very slow connection. see also eg unix.stackexchange.com/questions/188737/…
3

Navigate into the directory you would like to copy over, so:

cd ~/foo 

Run this:

rsync -avz . [email protected]:/var/www/bar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.