Skip to main content

3.2.3 sftp

sftp is a secure file transfer protocol. sftp is another mechanism for transferring files between different machines connected over the network. sftp allows you to also operate on files and directories on a remote machine. sftp connects and logs into the specified host, then enters an interactive command mode. In general, to invoke an sftp connection to a remote machine (called, say, faraway.csc.warwick.ac.uk ), the syntax is:

sftp userid@faraway.csc.warwick.ac.uk

Where again, the userid is for the remote machine. Your connection will then be authenticated (by password/passphrase, etc). If your authentication is successful, you will have connected to the remote machine, and an interactive prompt will appear (shown below)
 sftp> 
There are many commands you can use at this interface. First, there are operations on files and directories that work just like the standard linux counterparts

cd - change directory on the remote machine
lcd - change directory on the local machine
ls - list files in the current working directory on the remote machine
lls - list files in the current working directory on the local machine
pwd - print the current directory on the remote machine
lpwd - print the current directory on the local machine.
exit - exit from the sftp program.

There are also commands for getting files (copying files from the remote machine to your local machine) and putting files (copying files from your local machine to the remote machine).

Getting files

The get command in sftp allows you to download files from the remote machine. For example, to download a file named foo.bar, the following command would be used:
 sftp>get foo.bar 
To download this file and save it as readme.txt, the following command would be used:
 sftp>get foo.bar readme.txt 
To download more than one file from the sftp server use the mget command.
mget works by expanding each filename listed and running a get command on each file. The files are copied into the local working directory, which can be changed with the lcd command.
For example, to download all the files in the remote working directory, the following command would be used:
 sftp> mget ./* 
To download all of the files ending with .txt the following command would be used:
 sftp> mget ./*.txt 

You will find that you can't do recursive copying in sftp - so applying get or mget to a directory will result in an error message. For recursive copying, use the scp command.

Putting files

The put command in sftp allows you to upload files on to the remote machine. For example, to upload a file named foo.bar, the following command would be used:
 sftp>put foo.bar 
There is a lot more to find out about sftp, including options - read the man pages to find out more about this command.