192

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

rsync -av ~/foo user@remote.com:/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
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
Tombart
  • 30,520
  • 16
  • 123
  • 136

4 Answers4

291

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/ user@remote.com:/var/www/bar/

NiloCK
  • 571
  • 1
  • 11
  • 33
Protostome
  • 5,569
  • 5
  • 30
  • 45
  • 54
    This behavior is odd, compared to `mv` or `cp`. – zeekvfu Dec 05 '13 at 10:55
  • 2
    Seems weird and inconsistent to make the trailing slash relevant for just this particular command. Wonder why it hasn't been changed. – Luke Davis Jan 12 '17 at 22:29
  • 55
    In case someone was also wondering *why* this works: it's the trailing `/` after the directory name. – Etienne Bruines Jan 14 '17 at 18:24
  • 1
    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. – shawn1874 Aug 14 '20 at 20:15
  • 1
    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 – Nick Bull Sep 10 '21 at 22:26
  • This doesn't work on my machine for some reason. It still attempts to create the directory foo and as such I get a permission error. – USB_S0lderer Dec 06 '21 at 10:12
  • `-a` is shorthand for `-rlptgoD`; it's possible that you need the `-r`/`--recursive` to copy directories at all. And `*` is usually a shell-side thing, not rsync; the filenames get expanded before they even hit rsync (unless you escape it with something like `rsync src/\* dest`. – chronospoon May 10 '22 at 23:42
92

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.

Marcelo Guedes
  • 1,419
  • 11
  • 10
34

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
Olivier
  • 1,982
  • 3
  • 23
  • 36
3

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

cd ~/foo 

Run this:

rsync -avz . user@remote.com:/var/www/bar
Dan612
  • 31
  • 1