192

I am having trouble copying files from a remote server using SSH. Using PuTTY I log in to the server using SSH. Once I find the file I would like to copy over to my computer, I use the command:

scp username@host.com:/dir/of/file.txt \local\dir\

It looks like it was successful, but it only ends up creating a new folder labeled 'localdir' in the remote directory /dir/of/.

How can I copy the file to my local computer over SSH?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Vincent
  • 1,933
  • 2
  • 11
  • 6
  • 1
    Your question is not clear on this: are you running `scp` on your _local_ computer or inside the remote shell via SSH? – William Price May 31 '15 at 03:17
  • Inside the remote shell via SSH, I believe. – Vincent May 31 '15 at 04:02
  • I wanted to pop this in as I can't make it an answer... a program called FileZilla allows you to connect to your ssh and exchange files between your server and local device. – Lukali Jul 28 '21 at 11:02
  • you can install Git and enter ```scp username@host:path_to_file path_in_the_local_machine``` This will ask for your password in the remote machine. – EnthusiastiC Sep 11 '21 at 20:02

5 Answers5

359

It depends on what your local OS is.

If your local OS is Unix-like, then try:

scp username@remoteHost:/remote/dir/file.txt /local/dir/

If your local OS is Windows ,then you should use pscp.exe utility. For example, below command will download file.txt from remote to D: disk of local machine.

pscp.exe username@remoteHost:/remote/dir/file.txt d:\

It seems your Local OS is Unix, so try the former one.


For those who don't know what pscp.exe is and don't know where it is, you can always go to putty official website to download it. And then open a CMD prompt, go to the pscp.exe directory where you put it. Then execute the command as provided above

EDIT

if you are using Windows OS above Windows 10, then you can use scp directly from its terminal, just like how Unix-like OS does. Thanks to @gijswijs @jaunt @icanfathom

mainframer
  • 20,411
  • 12
  • 49
  • 68
  • My local OS is Windows. I downloaded the pscp.exe from the putty website. I tried opening it up and nothing came up. Also, I tried the command you listed above and was given: pscp.exe comman not found. Also tried it without the .exe and resulted in the same error. – Vincent May 31 '15 at 03:57
  • 2
    Open a CMD prompt, and go to the pscp.exe directory where you put it. Then execute the command as provided above. – mainframer May 31 '15 at 04:01
  • Also note that you can add the -i flag to scp if you need to pass your private key file over to ssh. – Patrick.SE Jun 07 '18 at 14:24
  • 1
    using scp on a mac has to be provided ./ parameter to signify that the file has to be downloaded in the current folder on mac machine. – Sarang Manjrekar Jul 02 '18 at 11:11
  • 2
    With Windows Subsystem for Linux (WSL, basically Ubuntu on Windows) you can now also open up Bash if your Local OS is Windows, and just use scp. Pro-tip: you can access your Windows file system from WSL. It's under /mnt/c. So you can do something like this: `scp username@remoteHost:/remote/dir/file.txt /mnt/c` – gijswijs Apr 25 '19 at 03:42
  • 4
    As of the 2018 Autumn update, Windows 10 now comes with OpenSSH and is on PATH. This means you can use the `scp` command on Windows 10, too. – jaunt Sep 23 '19 at 20:45
  • you can install winscp for windows. it's great UI tool. – Rahul Rajput Oct 06 '21 at 18:25
  • Windows 11 includes a new Terminal app, which gives you access to scp! Also, use `-r` to copy files and folders recursively. – icanfathom Dec 15 '21 at 01:49
39

Your question is a bit confusing, but I am assuming - you are first doing 'ssh' to find out which files or rather specifically directories are there and then again on your local computer, you are trying to scp 'all' files in that directory to local path. you should simply do scp -r.

So here in your case it'd be something like

local> scp -r username@host.com:/path/to/dir local/path 

If youare using some other executable that provides 'scp like functionality', refer to it's manual for recursively copying files.

gabhijit
  • 3,345
  • 2
  • 23
  • 36
24

You need to name the file in both directory paths.

scp username@host.com:/dir/of/file.txt \local\dir\file.txt
Andy
  • 1,215
  • 2
  • 11
  • 20
  • 3
    I don't believe that's correct. If the destination target is a directory, it will place the file there with the same filename as the source. – William Price May 31 '15 at 03:21
  • @WilliamPrice I think it depends on the local OS – 0yeoj May 31 '15 at 03:26
  • 1
    @0yeoj For my edification, which local OS is that? I dual boot Fedora and Win8.1 and verified that specifying just the target directory works with both the Linux version of _scp_ and the Putty-provided _pscp_ executables. – William Price May 31 '15 at 03:39
  • @WilliamPrice, try looking at `mainframer` 's answer your probably [check](https://kb.iu.edu/d/agye), and you are correct about _If the destination target is a directory, it will place the file there with the same filename as the source._ – 0yeoj May 31 '15 at 03:58
  • It seems I was indeed wrong about this. – Andy May 31 '15 at 04:04
  • @Andy, No that line is not wrong, its just that... it wont solve the problem. – 0yeoj May 31 '15 at 04:07
  • @Andy Then you should delete the answer. – Hashim Aziz Jul 06 '20 at 17:11
8

Make sure the scp command is available on both sides - both on the client and on the server.

BOTH Server and Client, otherwise you will encounter this kind of (weird)error message on your client: scp: command not found or something similar even though though you have it all configured locally.

0yeoj
  • 4,500
  • 3
  • 23
  • 41
3

that scp command must be issued on the local command-line, for putty the command is pscp.

C:\something> pscp username@host.com:/dir/of/file.txt \local\dir\
Jasen
  • 11,837
  • 2
  • 30
  • 48
  • I downloaded the pscp.exe from putty and tried to open it but nothing came up. Then I tried the command in the putty.exe application and was given an error: pscp command not found. – Vincent May 31 '15 at 03:59
  • 2
    it has to be installed in your path given that you seem unaware of that , you might find filezilla easier to use than scp. – Jasen May 31 '15 at 04:05
  • Filezilla is super simple to work with. I was able to setup FileZilla and connect to a Raspberry pi using the SSH credentials. – Bruce Seymour Jan 13 '22 at 15:57