scp file name with spaces

Just now, I was trying to scp a file from a remote machine to my laptop. The file path contains spaces (both directory name and file name). The normal way of escaping the spaces with backslash ‘\’ did not work with scp:

$  scp  guest@remote:/some\ directory/abc\ xyz.txt  ~/Desktop

Quote the whole file path also didn’t work.

$  scp  guest@remote:"/some directory/abc xyz.txt"  ~/Desktop

After some googling, finally, I found the following command that works:

$  scp  guest@remote:"\"/some directory/abc xyz.txt\""  ~/Desktop
$  scp  guest@remote:"\"/some directory\""/*.txt  ~/Desktop

9 responses to “scp file name with spaces

  1. Dave Armstrong July 6, 2009 at 10:27 pm

    I’ve tried a few times in the past but never could figure that one out!

    Thanks, Dave

  2. Edwin Fine September 25, 2010 at 1:01 am

    Actually, you can do it more easily by combining single and double quotes, avoiding the need to escape the double quotes. The single quotes surround the double quotes and prevent the double quotes from being interpreted by the shell:

    $ scp guest@remote:'”/some directory/abc xyz.txt”‘ ~/Desktop

  3. Steffen Möller November 13, 2010 at 1:01 am

    Thanks mate, that helped a lot.

  4. steve December 14, 2010 at 11:45 pm

    The single quotes around the double quotes worked well – Thanks.
    My destination file name also needed special characters. I found a second set of single quotes worked for the special characters in the destination name.

    $ scp me@server:'”/the dir/the file (1).txt”‘ ””/a dir/the file (1).txt””

    This was a Solaris 10 server with bash shell.

  5. steve December 14, 2010 at 11:51 pm

    Post above didn’t go though right – I used angle brackets and I think they got stripped as html tags.
    The destination file name reads [single][single][double][file_name][double][single][single]

  6. sonam February 14, 2012 at 5:52 pm

    can sumone suggest me out wid a simple example…..i have a presentation tomorrow……how to transfer files,,,,,???

    • wenlong February 15, 2012 at 7:40 am

      It’s simple. I assume that you are trying to transfer files from a linux machine to a SSH server:

      $ scp file1.txt file2.txt file3.txt username@servername:

      The command above uses scp to transfer 3 files (file1.txt, file2.txt and file3.txt) to a machine called “servername”. The files will be placed in the home directory of the user called “username”

  7. sonam February 14, 2012 at 5:54 pm

    it always shows msg:permission denied….:-(

    • wenlong February 15, 2012 at 7:42 am

      You need to make sure you have the permission to access the server. Try to login with the following command:

      $ ssh username@servername

      If you cannot login to the server (e.g. wrong username/password), then the previous scp command will also fail.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: