How to rsync over ssh when directory names have spaces

I am trying to rsync from one server to another. The servers have the same directory structure but I am having trouble getting the remotes server to properly recognize the path name on the remote when there is a space in it.

Here are the details

The local directory is

mnt/xlses/split/v2/name with space

The remote directory is

mnt/xlses/split/v2/name with space

I have tried everything I can find the latest attempt was

rsync --size-only -avzPe ssh  /mnt/xlses/split/v2/name with space/"name with space"

when that runs the first thing it reports is that it is creating a new directory

I interrupt it and see that there is a new directory


all of my files are in that directory

I was expecting them to be in

mnt/xlses/split/v2/name with space
Asked By: PyNEwbie


Well I am going to answer this question myself though someone else might do a better job of explaining it.

Evidently the setup on the destination machine affects how arguments are parsed and we must have something setup that is making it difficult to use quotes or slashes to escape spaces but we can use wild cards so I did this

rsync --size-only -avzPe ssh  /mnt/xlses/split/v2/name with space/*

This works for me because there is only one directory that begins with name if I had multiple directories this would not work.

Ultimately I need to understand how to setup the remote server so that it can parse the path name more efficiently – I never use spaces in directory names but the person who set this up did and at least for now I am stuck

Answered By: PyNEwbie


rsync --size-only -avzPe ssh /mnt/xlses/split/v2/name with space

I took off the trailing slash / from the source directory path. This will make rsync copy the directory and all its contents, which means rsync will worry about getting the name correct on the remote host (which it will) instead of you.

Answered By: dg99


rsync -s --size-only -avzPe ssh  "/mnt/xlses/split/v2/name with space/" " with space"

From man rsync (updated 2023):

--secluded-args, -s

This option sends all filenames and most options to the remote rsync via the protocol (not the remote shell command line) which avoids letting the remote shell modify them. Wildcards are expanded on the remote host by rsync instead of a shell.


This option used to be called --protect-args (before 3.2.6) and that older name can still be used (though specifying it as -s is always the easiest and most compatible choice).

Answered By: johnLate

Try this way:

find /path/ -exec rsync -A -X -av -r -s root@xxxx:/path/ {} ;
Answered By: user94712

This works in bash: Escape the spaces with backslash and then use quotes:

rsync -avuz"/media/Music/Heavy Metal/Witch Mountain/*" .

Or if you have the path in the variable $remote_path, spaces can be escaped with substitution:

rsync -avuz"${remote_path// /\ }" .
Answered By: teekarna

Depending on the situation, a quick option is to create a symlink on the remote system:

user@remote$ ln -s ~/name with space/ ~/name_with_space

Then use the -L flag in rsync which tells it to follow directory contents:

user@local$ rsync -avz -L user@remote:~/name_with_space/ ~/name with space
Answered By: Chris Laskey

Generally speaking, quote the argument and escape the space characters in the argument. In the example you gave, try:

rsync --size-only -avzPe ssh  "/mnt/xlses/split/v2/name with space/" " with space"
Answered By: James

I understand this is a old question, but I thought I would add to the existing body of knowledge.

I have used rsync with multiple folders with space and this works. I have folders numbered from 1-10 as follows:

  • The Folder1
  • The Folder2
  • The Folder3
  • ..
  • The Folder10

The 2 instances you would want to use rsync is local and remote.

  1. Local – Note the lack of quotes.

    rsync -avu /media/data/The Folder* .
  2. Remote – Note the presence of quotes

    rsync -avu -e ssh"/media/data/The Folder*" .
Answered By: Gokul Wimalanathan
rsync /mnt/xlses/split/v2/name\ with\ space/\ with\ space

double escape works too

Answered By: Andrej Surkov

Use two pairs of quotes

  1. Don’t bother with all the backslashes, just use single quotes inside double quotes:

    ssh"'/home/me/test file'" .
  2. (recommended) You can also use the reverse, that is, double quotes inside single quotes:

    ssh'"/home/me/test file"' .

The 2nd one is better because you can then use wildcards on the server (because the single quotes will not get expanded on the client). I do this when trying to rsync multiple files with the same prefix.

Further info

Wildcards on the server side

If you want a * to be interpreted on the server rather than the client, the * must come inside just one of the 2 pairs of quotes. I find this counter-intuitive because logically the outer pair of quotes escapes the client interpretation, while the inner pair of quotes escapes the server interpretation.

Protect Args

The advantage, compared to the --protect-args solution, is that you actually do not have the restrictions of the --protect-args, so you can use special characters such as ~ or $. So you can write:

rsync host:'"$HOME/test file"' .


rsync host:'~"/test file"' .

Note that the tilde (~) in the latter example needs to be outside the double quotes.

You can put one of the pairs of quotes around the entire username@host:file part (e.g. ssh "'/home/me/test file'" .)

Answered By: Sridhar Sarnobat

From the rsync manpages:

   If you need to transfer a filename  that  contains  whitespace,  you  can
   either  specify  the --protect-args (-s) option, or you'll need to escape
   the whitespace in a way that  the  remote  shell  will  understand.   For

          rsync -av host:'file name with spaces' /dest
Answered By: Shammel Lee

On macOS I had to escape the first path normally, and the second path escaped + quotes. Example:

rsync -avhu VirtualBox VMs/Windows 10 bob@"/Users/bob/VirtualBox VMs/"

This is the only combination that worked with spaces on macOS.
Both machines are macs.

Answered By: lenooh

This answer applies only to macOS – at least that is all I’ve tested it on.

While trying to rsync the following folder:

SRC_Kind='/Users/jmoore/Library/Application Support/Kindle/My Kindle Content/'

The solution in the accepted answer did not work for me; i.e. --secluded-args, -s. Here’s what did work:

rsync -rAtgoDiv $SRC_Kind $DST_Kind

Some info re my environment:

This was on an "oldie" macOS ‘Mojave’, but I keep my command line tools updated w/MacPorts. Specifically, my rsync version is:

/opt/local/bin/rsync --version
rsync  version 3.2.7  protocol version 31

The DESTINATION of the backup is the rsync daemon on a Synology NAS w/ the btrfs file system; I think the version is meaningless as Synology "rolls their own" rsync.

The rsync command was run in a script using #!/opt/local/bin/bash, and the bash version is:

/opt/local/bin/bash --version
GNU bash, version 5.2.26(1)-release (x86_64-apple-darwin18.7.0)
Answered By: Seamus
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.