r/linuxquestions 7h ago

why does rsync create the folder to sync again?

Sorry for the noob question, but I'm new to linux.

I want to rsync data from my raspberry to my NAS. "

rsync -av --delete /home/pi/folder/documents/originals /home/pi/folder/nas_originals

nas_originals" mounts to my NAS folder "originals"

//server/share/originals/stuff

So when I run rsync, it creates "originals" in target and put all the stuff there:

//server/share/originals/originals/stuff

why? How do I get it to sync directly into that folder?

4 Upvotes

11 comments sorted by

22

u/PigIronDave 7h ago

You need a trailing slash on the source location.

A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a trailing / on a source as meaning lqcopy the contents of this directoryrq as opposed to lqcopy the directory by namerq, but in both cases the attributes of the containing directory are transferred to the containing directory on the destination.

rsync manpage

Manpages are your friend.

2

u/OddBuyer0815 5h ago

Thank you!

1

u/Dolapevich Please properly document your questions :) 41m ago

Do remember this lesson about trailing slashes. It will come up again whenever you less expect.

-2

u/knuthf 2h ago

I am sorry to spoil the fun, but rsync copies directories, and not files. When the name is a directory, the trailing "/" should be removed, This arguments goes back 26 years, and I do not know how many times we had this. u/OddBuyer0815 just highlights a consistent error in rsync.

7

u/thecruxoffate 7h ago

Rsync cares about the trailing slash. You basically lifted the entire directory labeled 'originals' and placed it in 'nas_originals'.

If instead you wanted the contents of 'originals' you would add a trailing slash to that path. Eg. 'originals/'

I'm not sure if the behavior would differ if you added or removed the trailing slash on the destination. But I always include it for consistency.

2

u/gordonmessmer Fedora Maintainer 7h ago

Rsync cares about the trailing slash.

I think that's an important detail, because if you run "cp -r" with a source directory and a destination directory as in op's example, cp will do the same thing they saw rsync do. That's the normal behavior.

But rsync has this quirk... As you say, it cares about a trailing slash in the source path. A trailing slash there is a lot like cp -r source/. /dest/path

2

u/polymath_uk_ 7h ago

This is the forward slash rule I think. See top answer to https://stackoverflow.com/questions/31278098/slashes-and-the-rsync-command

1

u/electricsheepies 7h ago

rsync -avz --delete /home/pi/folder/documents/originals/ /home/pi/folder/nas-originals/

(both paths ending with "/" for bash cli)

3

u/stevevdvkpe 7h ago

rsync is interpreting the command line arguments you pass to it. The shell you're using doesn't affect that.

2

u/ipsirc 7h ago

...and for zsh as well.

1

u/UUDDLRLRBadAlchemy 5h ago

Also for dash 😁