SSH File Transfer with PuTTY

How to upload and download files through SSH using PuTTY (for Windows users)

In this tutorial we'll show you how to transfer files from your local computer to your hosting account and vice versa through SSH (Secure Shell) using PuTTY. PuTTY is an application with which Windows users can connect to their hosting accounts through SSH and execute commands, transfer files, etc. To learn more about PuTTY, where to find it, how to configure it and what you have to do to connect to your account through SSH, read the tutorial on connecting to your account through SSH (for Windows users).

In this tutorial we assume that you already have installed the whole PuTTY installation package on your local computer and you know how to configure it to connect to your account.

As you have probably noticed the PuTTY installation package contains several other applications, besides PuTTY itself. These include Pageant, PSCP, PSFTP, etc. What you need for file transferring is either PSCP or PSFTP:

PuTTY Applications

SCP (Secure Copy) and SFTP (SSH File Transfer Protocol) are network protocols which use SSH for file transfer. When it comes to transferring speed, they are a bit slower than the more commonly used FTP but they are much more secure.

PSCP and PSFTP are the PuTTY applications for transferring files over SCP and SFTP. SFTP is a newer protocol designed to work with SSH-2 (Secure Shell version 2). Despite its name, PSCP also uses SFTP as its first option for transferring files when the server to which PSCP is connected is an SSH-2 server. Practically all servers, including HostKnox servers, are SSH-2 servers.

The main difference between PSCP and PSFTP is that PSCP is primarily used for downloading and uploading files (it can also be used to list files in a remote directory). PSFTP is more like an FTP client (e.g. FileZilla), meaning that it can be used not only to transfer files, but also to browse the files and folders on your account, to delete them, etc. PSFTP and PSCP are, however, command line applications. This means that they are used through the Command Prompt of your Windows operational system and you have to type the commands manually.

So, to start PSCP or PSFTP you have to start the Command Prompt first. To do this, click on the Windows Start button in the lower left corner of your screen, then click on Run... and in the window that opens type cmd and click on OK. This will launch the Command Prompt from which you can start PSCP or PSFTP.

In order to connect to your account and transfer files you also have to start Pageant and add your private SSH key. For more information on this check the tutorial on connecting to your account through SSH using PuTTY.

First we'll go over PSCP and we'll show you how to start it and use it to transfer files from your local computer to your hosting account and vice versa. To start it, you have to type a command in the Command Prompt to add PSCP to the system path. If, for example, you have installed all the PuTTY applications in a folder called putty on your local D drive, the command should look like this: set path=%path%;d:\putty. After you type it press Enter on your keyboard to execute the command.

Command Prompt PSCP Start

After you do that you're ready to type commands to transfer files with PSCP. To upload a file from your local computer to the public_html directory on your account (the directory where you have to put the files you need for your site), you have to execute a command in the Command Prompt that looks like this:

pscp d:\documents\example.txt username@e.hostknox.com:/home/username/public_html

This command will transfer a text file called example.txt from the documents folder on your local D drive to the public_html directory on your account. You have to replace username in both places with your actual master username and, if needed, the server name. The part between the @ symbol and the colon is the address of the server and the part after the colon is the path to the public_html folder (/home/username/public_html).

To download a file from your account to your local computer just type first the path to the file on your hosting account and then the place on your local computer where you want to download it:

pscp username@e.hostknox.com:/home/username/public_html/example.txt d:\documents

The only spaces in the pscp command are after pscp and between the two paths. If you want to upload a file which has spaces in its file name (or the directory containing it has spaces), you have to put the whole path to the file on your local computer in quotation marks. The same has to be done if you download a file on your local computer in a directory that has spaces in its name. For example, you want to upload a file called my example file.txt from a folder called documents on your local D drive to the public_html directory on your account:

pscp "d:\documents\my example file.txt" username@e.hostknox.com:/home/username/public_html

If you want to download the same file (a file with spaces in the name) from your hosting account, you only need to put quotation marks around the file name:

pscp username@e.hostknox.com:/home/username/public_html/"my example file.txt" d:

The above command will download a file called my example file.txt from your account to the D drive on your local computer.

Notice also that when you specify the path to your account you have to use forward slashes (/) while for the path on your local computer you have to use backward slashes (\).

The above commands will only work for single files. You can transfer multiple files by using wildcards (*). For example, the following command will download all the files (without any subdirectories) inside the documents folder from you account to the D drive on your computer:

pscp username@e.hostknox.com:/home/username/public_html/documents/* d:

If you want to transfer a directory together with all the files and subfolders in it, you have to add the option -r. For example:

pscp -r d:\documents username@e.hostknox.com:/home/username/public_html

The above command will upload the entire documents folder with everything inside it from your local computer to the public_html directory on your account.

A tip that may save you some time: by pressing the up arrow key on your keyboard you can browse through the previous commands that you have executed. With the left and right arrow keys you can move the line cursor to correct mistakes in your commands.

Now, let's move to PSFTP. To start PSFTP, open the Command Prompt and use the same command as with PSCP to add PSFTP to the system path (e.g. set path=%path%;d:\putty). Don't forget that you also need to start Pageant and add your private SSH key. After you execute the set path command, you just need to type psftp followed by a space and your master username and the name of the server: e.g. psftp username@e.hostknox.com.

This will log you in your account in the directory /home/username. Besides transferring files, you can use Unix/Linux commands to browse and manage the files and folders on your account. For example, once you're logged in if you type just ls and press Enter you'll see a list of the directories inside /home/username. One of them is the public_html directory. You can open that folder and make it your current working directory by typing cd public_html. With cd .. you'll open the parent directory of your current directory. Here is a list with some useful commands:

ls - shows the content of the current directory

cd directoryname - changes the current working directory to the one specified (the specified directory has to be a subdirectory of the current one, otherwise you have to type the full path)

cd .. - go one directory up

mkdir newfolder - creates a new directory called newfolder inside the current directory

rm filename - deletes a file with the specified name (e.g. filename); the file has to be in the current directory or you have to type the full path

rmdir directoryname - deletes a directory, it has to be empty

lcd directoryname - changes the current working directory on your local computer to the one specified; for example, lcd d:\documents will change the working directory on your local computer to the documents folder on the D drive

lpwd - displays the name of the current working directory on your local computer

Now, let's go back to the main purpose of PSFTP: transferring files. Files can be downloaded with the get command:

get /home/username/public_html/examplefile.txt d:\documents\examplefile.txt

You have to replace username with your actual master username.

The above command will download a file called examplefile.txt from the public_html directory on your account to a folder called documents on your local D drive. Notice that here with the command in the above form we have also specified the name of the target file that we're going to save on the local computer (d:\documents\examplefile.txt). This has to be done when you're downloading a file to a directory on your local computer that is not your current working directory. Unlike PSCP, where if you don't specify a name the file will be saved with the original name, with PSFTP if you don't specify a name for the file the get command might not work. You have the choice whether to use the same name for the target file or to use a different one, but in both cases the content of the file will be the same as that of the source file.

Since with PSFTP you can browse and change the working directory on your account (cd command), you don't have to type the full path to the file on your account that you want to download. Using the above example, if you use the cd command (e.g. cd public_html) to make public_html the current working directory on your account, the command would look like this:

get examplefile.txt d:\documents\examplefile.txt

The same is true for the current working directory on your local computer. If you change it with the lcd command to d:\documetns (lcd d:\documents) the above command would look like this:

get examplefile.txt examplefile.txt

Assuming the working directory on your account is public_html and the working directory on your local computer is d:\documents, the above command will download a file called examplefile.txt from the public_html directory on your account to the documents folder on your local D drive. In this case you don't even have to type the name of the target file when you download it to the current working directory on your computer. You can just use get examplefile.txt and the file will be downloaded in your working local directory with the same name as the source file.

To download a directory with all its content (including subdirectories), you have to add, as with PSCP, the option -r:

get -r newfolder d:\newfolder

The above command will download a directory called newfolder from the current directory on your account to the D drive on your local computer. If you don't specify a name for the directory on you local drive (e.g. get -r newfolder d:) PSFTP will download only the contents of newfolder to your local D drive without the folder itself. So the contents of newfolder will be scattered on your D drive. This, as with files, is the case when the current working directory on your local computer is not your local D drive. If the working directory on your local computer is the D drive, you can just use get -r newfolder. This will download not only the contents but also the folder itself to the working directory on your computer (D drive in this example). Again, as with files, if you want to you can specify a different name for the target directory (e.g. get -r newfolder newfolder1).

With the command mget you can download multiple files at the same time. For example, mget examplefile1.txt examplefile2.txt will download the two files from the current working directory on your account to the current working directory on your local computer. With the -r option you can use mget to download multiple directories (e.g. mget -r dir1 dir2).

The PSFTP command for uploading files is put. Basically the same rules apply to this command as the ones for get. Only in this case first you specify the file and path on your local computer and then on your account:

put d:\documents\examplefile.txt /home/username/public_html/examplefile.txt

The above command will upload a file called examplefile.txt from the documents folder on your local D drive to the public_html directory on your account. Similarly to the get command, if you are uploading a file to a directory that's not your current working directory you have to specify not only the path on your account but also the name with which the file will be saved.

If you're uploading the same file as in the above example but you have changed the current working directory on your local computer to d:\documents and the working directory on your account to public_html, you just need the command:

put examplefile.txt

With the put command you can also use the -r option to upload a directory together with its subdirectories and files (e.g. put -r exampledir). You can use mput to upload multiple files at once (e.g. mput examplefile.txt examplefile1.txt) or mput -r to upload multiple directories (e.g. mput -r exampledir exampledir1).

Another thing useful to know is that with both commands (put and get) if the file name or directory name has spaces in it, you have to put quotation marks around the name (e.g. put "my example file.txt").