title: rsync: filenames with spaces, quotes and japanese letters
body: <p>I'm writing a script for syncing completed torrents from a remote server and remembering already synced files/folders. But filenames with quotes or japanese letters are causing some trouble, so rsync have to run two times at the moment.</p>
  
  <p><code>rsync -aP ovh:"$src/$(printf "%q" "$1")" "$dst/"</code></p>
  
  <p>working:</p>
  
  <ul>
  <li>brackets</li>
  <li>whitespaces</li>
  <li>quotes</li>
  </ul>
  
  <p>not working:</p>
  
  <ul>
  <li>japanese letters</li>
  </ul>
  
  <p>Every single japanese letter will be converted to something like <code>?\#211</code>, <code>\#215島</code> or <code>\#227?\</code>. Since I'm using OS X, I also tried <code>--iconv=utf-8-mac,utf-8</code> - without success.</p>
  
  <p><code>rsync -aP ovh:"'$src/${1/\'/\\'}'" "$dst/"</code></p>
  
  <p>working:</p>
  
  <ul>
  <li>brackets</li>
  <li>whitespaces</li>
  <li>japanese letters</li>
  </ul>
  
  <p>not working:</p>
  
  <ul>
  <li>quotes</li>
  </ul>
  
  <p>For example <code>$1</code> is <code>test's file</code>, the string send to the server becomes <code>"'/data/rtorrent/complete/test\'s file'"</code><br>
  Error message: <code>zsh:1: unmatched '</code></p>
  
  <p>Seems like escaping within a single quoted text doesn't work. But if the outer single quotes are removed, rsync interprets a whitespace as separator for another file.</p>
  
  <p>I thought maybe it would help to convert every single character to unicode (like <code>\u1337</code>) and send this string to the server, but didn't find a way to do that. Just endless scripts for the other way around.</p>
  
  <p>Also sed wasn't helpful either - way too much work to escape everything manually. This script should work reliable and I don't want to check every now and then if I covered every possibility which maybe needs escaping.</p>
  
  <p>Any idea how to merge the two commands into one?</p>
  
  <p>Edit: my temporary solution was this:</p>
  
  <pre class="lang-sh prettyprint-override"><code>sync() {
    # 1. escape quotes / 2. escape kana
    rsync -aP ovh:"$src/$(printf "%q" "$1")" "$dst/" &gt;&amp; /dev/null &amp;&amp; success "$1" || \
   (rsync -aP ovh:"'$src/${1/\'/\\'}'" "$dst/"       &gt;&amp; /dev/null &amp;&amp; success "$1" || unlock "$1")
  }
  </code></pre>