Linux / Unix: Rsync Copy Files & Directories Names With WhiteSpaces and Blanks

I need to transfer a file called ‘some cool movie.avi’ that contains whitespace under rsync. How do I escape a filename or directory name with spaces in them while using rsync command under Linux / *BSD / Unix like operating systems? How do I use rsync when files / dirs contain spaces?

You escape spaces in a remote path when using rsync over a remote SSH connection under Apple OS X / Linux or Unix like operating systems. [donotprint][/donotprint] If a filename contains whitespace, you can use any one of the following method to get rid of it.


The syntax is as follows to copy a file called “/path/to/This is a test youtube video.mp4“:

rsync -av user@desktop:'This is a test youtube video.mp4' /path/to/here


rsync -av 'user@desktop:This is a test youtube video.mp4' /path/to/here

You can also pass the -s or --protect-args option to avoid no space-splitting ( wildcard chars only ). The syntax is (note black slashes are removed with this option):

rsync -avs 'user@desktop:This is a test youtube video.mp4' /path/to/here


rsync -av --protect-args 'user@desktop:This is a test youtube video.mp4' /path/to/here


rsync -av --protect-args 'user@desktop:This is a test youtube video.mp4' '/path/to/My Download Folder'

To copy files to remote server called nas01 at /data/old.macbook.june-2013/MacMini/VirtualBox VMs/ folder type:

# cd to "$HOME/VirtualBox VMs/" #
cd ~/VirtualBox VMs/
# Make a backup to nas01 server #
rsync -avrPz . root@nas01:'/data/old.macbook.june-2013/MacMini/VirtualBox VMs/'

Sample outputs:

Fig.01: rsync over ssh when directory names have spaces on local or remote server

A note about older versions of rsync

If you are using an older versions of rsync Try the following syntax:

rsync -av user@server2:'dir1/file1 dir2/file2' /path/to/dest
rsync user@macbook.local::'modname/dir1/file1 modname/dir2/file2' /path/to/here/dest
See also:
  1. BASH Shell: For Loop File Names With Spaces
  2. man page rsync

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....