Ubuntu – How to scp a filename with spaces

command linescpsshtransfer

I'm having a little difficulty using scp to transfer files from a remote computer. The issue apparently has to do with the name of the directory the files are contained in. They're on a CD drive called photos 4 (with a space between photos and 4). When I attempted the transfer, I used the following command:

scp [remote username]@192.168.1.X:/media/[remote username]/photos\ 4/file.jpg /home/[username]/Pictures

However, I get an error message in return saying No directory: /media/[remote username]/photos. I thought the backslash would escape the space so that the directory would be read as photos 4.

Can someone fill me in on what I'm doing wrong here?

(As an aside, I made a copy of file.jpg to the remote computer's desktop and then ran the command:

scp [remote username]@192.168.1.X:/home/[remote username]/Desktop/file.jpg /home/[username]/Pictures

and it worked, so that escape sequence seems to be the culprit. I just can't figure out what's wrong with it.)

Best Answer

Spaces in directories or filenames are the natural enemy of a Linux system but can of course be escaped with due diligence. There are 3 possibilities that you could try:

  1. scp [remote username]@192.168.1.X:"'/media/remote_username/photos 4/file.jpg'" .
  2. scp [remote username]@192.168.1.X:"/media/remote_username/photos\ 4/file.jpg" .
  3. scp [remote username]@192.168.1.X:/media/remote_username/photos\\\ 4/file.jpg .

All should work but some are syntactically easier to understand than others...

References:

Related Question