2

Looking to rsync a drive that contains folders that begin with a single dash or a double dash, for example:

  • -Archives-
  • --Archives--

I've tried the following:

rsync -azP -e <source drive>/* <destination drive>

...however, the folders that start with dashes (-) or double-dashes (--) are not getting synced.

How can I ensure that any folders beginning with dashes (-) or double-dashes (--) get properly synced?

2
  • 1
    What's the exact command you're running? If you give the source files as foo/*, they shouldn't get mixed up with options, but if you just use *, they would. Also -e takes as option the command to use to launch a remote shell, so it'll eat one from the list of filenames
    – ilkkachu
    Nov 29 '21 at 12:55
  • @ilkkachu i took the command from one with an ssh connection — thanks for letting me know the -e doesn't belong for local transfers.
    – Mo Boho
    Nov 29 '21 at 14:03
14

You do this in the same way as with most tools:

  • You write the path so that it does not start with a dash, possibly by writing out ./ explicitly or using the absolute path to the directory.

    rsync ...options... /full/path/src ./dst
    
  • You use -- to stop option parsing before the pathname operands:

    rsync ...options... -- srcpath dstpath
    

In your case, using --, the following would create /some/other/path/--Archives- as a copy of the directory --Archives- in the current directory:

rsync -av -- --Archives- /some/other/path

The following would copy only the contents of --Archives- to the destination directory:

rsync -av -- --Archives-/ /some/other/path

It's the slash at the end of the source path that matters. A slash at the end of the destination path is irrelevant.

Note that there is seldom a need to use explicit globbing on the source path(s) unless you know you want to avoid copying any hidden names from the top-most source paths.

Your use of -e in your example command is not needed unless you need to call ssh in a special way to create the network connection to your source or destination. In the example code in the question, you seem to prefix the source path with -e, which is probably an error (and you mention copying between two drives, which would not involve connecting over a network at all).

Also, note that using -z (compression) is unlikely to speed up things unless you have an extremely slow network connection (i.e., if you can compress and decompress much faster than what you can transfer). Therefore, it is never necessary to use -zwhen doing local copying. In fact, this may slow the operation down.

5
  • good answer. I'd add that : the first trick ( ./filename ) works with every tools. The 2nd trick (-- to stop parsing options) only work with a subset of tools (those that include that "--" cabability. not all programs (especially old, pre-gnu ones) have it) Nov 30 '21 at 13:22
  • @OlivierDulac Standard command line tools will understand --, as would any tool using the C library's getopt() function (or a facility relying on this function, or compatible function, like the shell's getopts utility). In fact, there are very few tools that does not understand -- (and this is usually documented). The utility that is in use in this particular question, rsync, does understand --. Also note that this is not a "trick" :-) It's the way command line utilities work. Well, "most" at least (as I wrote in the answer).
    – they
    Nov 30 '21 at 13:45
  • "recent" (linux era) tools usually do, most other unixes have more commands that don't (not all, not many, but quite a few), hence my remark (and my preference to only do the "./" trick as it is the most portable) Nov 30 '21 at 15:01
  • @OlivierDulac The -- is part of the POSIX guidelines though, so I don't think it about Linux tools.
    – terdon
    Nov 30 '21 at 15:33
  • @OlivierDulac Using ./ is not a trick either :-) To know what utilities support -- and which ones do not is not a black or secret art or something that is purely random chance. You have a handful of utilities, like dd and echo, that have very different command line argument handling. This is clearly mentioned in their manuals. They are few enough to know by heart. Most tools work the same way in this regards, regardless of if they are Linux tools or BSD tools or Solaris tools. It's standardized.
    – they
    Nov 30 '21 at 15:34
7

Prefix the relative pathname (filename) with ./ and it'll no longer start with a dash

rsync -av ./-items* destination:destPath/

Or remove the wildcard * and transfer the parent directory

rsync -av ./ destination:destPath/dir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.