5

My command on macOS is:

rsync -rav mydir/ /Volumes/usb1/

My expection was that due to the slash at the end of mydir/, rsync would create a directory Volumes/usb1/mydir, and copy the directory tree below mydir into this directory. However, the files inside mydir were copied flat into /Volumes/usb1.

I've installed rsync 3.2.7 which isn't the default for my MacOS.

What did I do wrong?

2 Answers 2

10

The source file should not end in a /.

Explanation

Note: I am using macOS Catalina 10.15.7.

For rsync, the behaviour is the opposite of what your question expects, as documented from man rsync. An excerpt is given below.

       rsync -avz foo:src/bar/ /data/tmp


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 "copy the contents of this directory"
as  opposed  to  "copy  the  directory  by name", but in both cases the
attributes of the containing directory are transferred to the  contain-
ing  directory on the destination.  In other words, each of the follow-
ing commands copies the files in the same way, including their  setting
of the attributes of /dest/foo:

       rsync -av /src/foo /dest
       rsync -av /src/foo/ /dest/foo

The cp command also behaves like rsync, as documented from man cp. An excerpt is given below. 

-R    If source_file designates a directory, cp copies the directory and
      the entire subtree connected at that point.  If the source_file
      ends in a /, the contents of the directory are copied rather than
      the directory itself.  This option also causes symbolic links to be
      copied, rather than indirected through, and for cp to create spe-
      cial files rather than copying them as normal files.  Created
      directories have the same mode as the corresponding source direc-
      tory, unmodified by the process' umask.
0

The -a option implies the -r option and more. You can just use the -a option. rsync --help produces a nice list of options.

Navigate to mydir 's parent folder

rsync -av ./mydir /Volumes/usb1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .