33

Other than by renaming the files first, how can I rsync files that may have colons in their names?

3
  • Make sure you are using dir/ and not dir/* in rsync arguments.
    – user1686
    Apr 19, 2011 at 20:19
  • The problem happens when I rsync from a hard drive to a flash memory stick: rsync: mkstemp "/media/verd/rence/.Using an expressive work: fact or fiction.pdf.RbjlKK" failed: Invalid argument (22) rsync: mkstemp "/media/verd/rence/.What's in a concept: structural foundations for semantic networks.pdf.tLXoZz" failed: Invalid argument (22)
    – 象嘉道
    Apr 20, 2011 at 18:40
  • For future readers, please also note that this is filesystem dependent: Windows-typical hard drives with NTFS cannot contain files with : in the first place
    – phil294
    Dec 6, 2019 at 16:36

1 Answer 1

33

Colons are only special in the first directory component of a command line argument. So if you have what looks like a relative path, prepend ./.

$ mkdir sou:rce
$ rsync -a sou:rce/ de:st/
The source and destination cannot both be remote.
$ rsync -av ./sou:rce/ ./de:st/
sending incremental file list
created directory ./de:st
./

In a script:

case $source in
  /*) :;;
  *) source=./$source;;
esac
case $dest in
  /*) :;;
  *) dest=./$dest;;
esac
rsync "$source" "$dest"
6
  • Thank you, Guilles. The problem happens when I rsync from a hard drive to a flash memory stick: rsync: mkstemp "/media/verd/rence/.Using an expressive work: fact or fiction.pdf.RbjlKK" failed: Invalid argument (22) rsync: mkstemp "/media/verd/rence/.What's in a concept: structural foundations for semantic networks.pdf.tLXoZz" failed: Invalid argument (22)
    – 象嘉道
    Apr 20, 2011 at 18:36
  • 3
    @Kejia柯嘉: Ah, ok, your problem is to rename-and-rsync, not to pass the colons to rsync. This question came up before on the Ubuntu site: How can I substitute colons when I rsync on a USB key? Unfortunately no one's produced a satisfying answer. Apr 20, 2011 at 18:46
  • @Gilles'SO-stopbeingevil' How does rsync know, with rsync src ./hello@b.com:/test/, if the dest is SSH or local? Here it could be both. Your answer states that prepending ./ makes rsync be sure it's a local path. But it's not 100% sure: ./hello is a valid Linux username (I just tried with useradd). TL;DR: how to rsync with a remote with username ./hello, host b.com, and path /test/?
    – Basj
    Nov 24, 2020 at 15:17
  • @Basj I don't think rsync supports user names containing /. If you're using SSH for transport, I think you can use an ssh_config Host block with a User directive. Note that rsync is likely not the only program that doesn't support / in user names. POSIX only allows ASCII letters, digits and -_.. Debian adduser only allows this and @ (which surprises me because a lot of programs don't support @ in user names!). Nov 24, 2020 at 15:43
  • @Gilles'SO-stopbeingevil' It would make sense indeed. adduser a/b gives adduser: To avoid problems, the username should consist only of letters, digits, underscores, periods, at signs and dashes, and not start with a dash (as defined by IEEE Std 1003.1-2001). For compatibility with Samba machine accounts $ is also supported at the end of the username.. I should have tried this first instead of useradd!
    – Basj
    Nov 24, 2020 at 15:52

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.