How do I scp a filename with spaces?

14,019

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:

Share:
14,019

Related videos on Youtube

Alcuin Arundel
Author by

Alcuin Arundel

Updated on September 18, 2022

Comments

  • Alcuin Arundel
    Alcuin Arundel over 1 year

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

    • Admin
      Admin almost 8 years
      Main lesson here: quit using spaces in file names. Even Apple corporation agrees on this now: support.apple.com/en-us/HT202808
    • Admin
      Admin almost 8 years
      That's fine, unless you're scp'ing files that you didn't name.
  • Pathros
    Pathros over 2 years
    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
    Pathros over 2 years
    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!
  • andrew.46
    andrew.46 over 2 years
    @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...
  • CPH
    CPH about 2 years
    @Pathros Not tested, but to do multiple files I would assume you can do {"file1","file2"}