To rsync a file or directory that contains spaces, you must escape both the remote shell and the local shell. I tried doing one or the other and it never worked. Now I know that I need to do both!
So let's say I'm trying to rsync a remote directory with my local machine and the remote directory contains a space (oh so unfortunately common with Windows files). Here's what the command should look like:
rsync '[email protected]:/path/with spaces/' /local/path/
The single quotes are used to escape the space for my local shell and the forward-slash is used to escape the remote shell.
Thanks, very useful article!
You’re welcome, AaL! 🙂
Thanks – I’ve been trying to work out how to do this for months! It never occurred to me to escape both the local and remote paths.
No problem! Glad I could help!
or the rsync option –protect-args (-s), no slashes needed
rsync manual describes how the remote shell interprets all filenames and options. with –protect-args the filenames and options are interpreted by the remote rsync only.
no description in the manual how filenames and options are interpreted at the local shell by shell or rsync source sender. at least we know to write quoted filenames to rsync like most shell programs that read filenames with spaces.
an rsync transfer, minimally escaped
rsync -s andrew@example:/path/’path spaced’/ “/to/destination spaced/”
for filenames, notice that you could either quote the spaced part of a path only, or the path full. either escape form may single quote or double quote
That’s helpful info, Andrew! Thanks! 🙂
Thanks for the tip about –protect-args!
This saved me a whole lot of trouble since although what Raam Dev wrote is correct it’s impossible (or very difficult) to use in scripts where the path with spaces is in a variable.
Thanks Andrew, for me worked when i put -s argument and shaped only directory with space
Hello all,
pls help.
I need some help with rsync functionality for ‘files-from=’.
sorry there is impossible to post some lines of script, so there is link, where I try my luck too.
http://www.procata.com/blog/archives/2005/03/01/rsync-to-remote-server-via-ssh/#comment-85796
Hi Petr,
I’m not sure what the problem is but perhaps someone else who visits here will be able to help. Good luck!
I found it, probably black hole in old OS (was debian 3.1) today no problem in my 10.04 kubuntu with remote processing –file-from=list
Cheers !
Great! Thank you for the update, Petr! 🙂
Nice tip 🙂
[trolling] While there is no such thing as ‘God’ -as those mind control systems known as religions wants us believe-, and while every Mac user on earth is a faggot[/trolling] I find your About section somewhat interesting and that’s a lot for me; I’ve already bookmarked your site to read it on my free time.
Best
Haha, thanks and nice to meet you too. 🙂
Hi, what about handling spaces in all the files and subdirectories? Thanks.
That should already work for files and subdirectories.
Thanks! One of those things that shouldn’t be forgotten but often is.
You’re most welcome, Jeff! 🙂
Thanks for this!! – I’d been scratching my head RE this and your post came up on a google search!
Awesome! You’re welcome. Glad I could help out. 🙂
Cheers.
Thanks! This helped!
Colin
You’re welcome, Colin! Glad I could help. 🙂
This fixed my RSYNC problem on mac-os 10.8. I was copying my documents file to a windows network drive i created on my windows box using an external USB 3.0 hd.
Here is the error:
rsync error: some files could not be transferred (code 23)
Thanks for the info, JoseB! I’m glad to hear this fixed your problem. 🙂
Thank you!!!!
This was driving me nuts:)
You’re welcome, Harry! 🙂
You can also use Long Path Tool for such issues, it works good.
Thanks for the tip! 🙂
Thank you very much for this, I am not an expert so this saved me many years of hardship trying to find a solution!
My pleasure! I’m glad this helped you. 🙂
Thank you.
Have had many folders in the form of “Fred & Charleen” for years under windows. Now in a mixed environment of Linux all appeared lost!
You’re welcome, Jim! I’ve grown to avoid spaces in filenames and directories like the plague. I immediately think about all the issues I might encounter if I need to deal with that file or directory on the command line. 🙂
Unfortunately I continue to have issues with remote paths that include spaces. My local machine is Ubuntu 14.04 (rsync 3.1.0) and my remote is OS X 10.10 (rsync 3.1.1), both with bash as default shell. I have tried all variations, but nothing seems to work. Very frustrating!
rsync [email protected]:/path/’with spaces/ /local/path/
rsync ‘[email protected]:/path/with spaces/’ /local/path/
rsync “[email protected]:/path/with spaces/” /local/path/
rsync [email protected]:/path/”with spaces”/ /local/path/
rsync [email protected]:/path/with spaces/ /local/path/
rsync ‘[email protected]:/path/with spaces/’ /local/path/
Hey Marckr,
I’m sorry, if what I posted doesn’t help I’m really not sure what to suggest. You might try asking a question on StackOverflow.com or ServerFault.com.
Muchas Gracias! Me estaba matando la cabeza con eso!
Thanks so much! It was losing my head!
You’re most welcome! I’m glad I could help. 🙂
What backslash are you talking about? There is no backslash in your example.
Ha! Good point. I’ve fixed that. (I actually wrote about backslash vs. forward-slash, so I should know this!)