19

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.)

muru
  • 193,181
  • 53
  • 473
  • 722
Alcuin Arundel
  • 3,040
  • 2
  • 20
  • 28

2 Answers2

22

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:

andrew.46
  • 37,085
  • 25
  • 149
  • 228
  • How about `scp` multiple files with the same extension? I have tried `"{file1,file2}".pdf` and `{"file1,file2"}.pdf` but it doesn't work: `No such file or directory`. However, your first option for one file does work. But, for more than one file, how do you do that? Please help! – Pathros Jan 28 '22 at 18:41
  • @Pathros This looks like it could be a new question? I suspect what is happening is that the quoting has prevented the bash filename expansion you are after... – andrew.46 Jan 29 '22 at 23:21
  • @Pathros Not tested, but to do multiple files I would assume you can do {"file1","file2"} – CPH Feb 16 '22 at 15:20
0

Here is the fix for ZSH users,

  1. scp 'username@192.168.1.X:/media/"photos 4"/file.jpg' /home/username/Pictures
  2. scp 'username@192.168.1.X:/media/"photos 4"/"file with space.jpg"' /home/username/Pictures
  • How about `scp` multiple files with the same extension? I have tried `"{file1,file2}".pdf` and `{"file1,file2"}.pdf` but it doesn't work: `No such file or directory`. However, your first option for one file does work. But, for more than one file, how do you do that? Please help! – Pathros Jan 28 '22 at 18:42