I have a bash script that uses scp to copy a file from my machine to another. The script keeps on exiting after I enter the SSH password with the error:

<filename>: No such file or directory

Yet, in the script, I check the file, and it is just fine. I did set -o verbose at the beginning and here's what I get at the end of the script:

scp /Volumes/FX4\ HDD/Users/matthewdavies/Downloads/NCIS.S11E01.HDTV.x264-LOL.mp4 [email protected]:"/media/3TB/TV\ Shows/NCIS"
[email protected]'s password:
/Volumes/FX4\ HDD/Users/matthewdavies/Downloads/NCIS.S11E01.HDTV.x264-LOL.mp4: No such file or directory

So, I tried executing the scp command as outputted and it was fine; it copied. What's going wrong???

  • try with scp /Volumes/FX4\ HDD/Users/matthewdavies/Downloads/NCIS.S11E01.HDTV.x264-LOL.mp4 [email protected]:"/media/3TB/TV Shows/NCIS"
    – thkang
    Oct 2, 2013 at 5:59
  • What's the difference between that and what I pasted there? And it says "scp: ambiguous target" because it's missing a ``, but I added that in, and it works just fine…
    – daviesgeek
    Oct 2, 2013 at 6:06
  • you don't have to escape the whitespace ` ` in double-quotes. actually, I think it had to be scp /Volumes/FX4\ HDD/Users/matthewdavies/Downloads/NCIS.S11E01.HDTV.x264-LOL.mp4 "[email protected]:/media/3TB/TV Shows/NCIS" because the last part(destination) is counted as a whole argument.
    – thkang
    Oct 2, 2013 at 6:12
  • Sorry…I meant it's missing a \ in the remote dir.
    – daviesgeek
    Oct 2, 2013 at 6:22
  • @thkang see my updated question…
    – daviesgeek
    Oct 2, 2013 at 6:26

1 Answer 1


I'm not entirely sure what you're doing, but when I try the command you have in your example I get the following:

$ scp /home/saml/projects/Cooks.com\ -\ Recipe\ -\ Coconut\ Chicken.mht \
       root@remotey:"/root/some spaced out file.mht"
scp: ambiguous target

This is because you're quoting the target path and it also includes backslashes which are escaping the spaces. However when the current shell peels off the double quote, it will also peel off the single backslash, leaving the target path as a bare string with spaces. You need to do one of the following to nest it further, so that the spaces are correctly escaped:


method #1 - double quote, single quote

$ scp /path/with\ spaces/file\ with\ spaces.txt \
       user@remotey:"'/home/user/some spaced out file.txt'"

method #2 - single quote, double quote

$ scp /path/with\ spaces/file\ with\ spaces.txt \
       user@remotey:'"/home/user/some spaced out file.txt"'

method #3 - single quote, backslash

$ scp /path/with\ spaces/file\ with\ spaces.txt \
       user@remotey:'/home/user/some\ spaced\ out\ file.txt'

method #4 - double quote, backslash

$ scp /path/with\ spaces/file\ with\ spaces.txt \
       user@remotey:"/home/user/some\ spaced\ out\ file.txt"

method #5 - triple backslashes

$ scp /path/with\ spaces/file\ with\ spaces.txt \
       user@remotey:/home/user/some\\\ spaced\\\ out\\\ file.txt
  • No, the destination is correct. "foo\ bar" is equivalent to 'foo\ bar', not 'foo bar' — a backslash between double quotes is only erased if it's followed by \"$, backquote or newline. Oct 2, 2013 at 23:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .