File transfer using sftp on Unix systems
This page describes Unix sftp, which can be used on most Unix systems including MacOS X.
To invoke SFTP on a Unix system, use the command
where hostname is the name of the remote system you wish to contact, e.g.
sftp hermes.cam.ac.uk or
sftp sftp.ds.cam.ac.uk (note the slightly different form of the name).
You can also start the program by typing simply
sftp, and then use the
open command to set up the connection you require.
You will then be asked for your password. The program reports that a connection has been made (or gives details if it fails) and then awaits further commands. The commands available vary between systems but the simple ones are common to all. On any system, the help command lists the SFTP commands available. The
quit command closes the session.
Many remote systems will close the connection if it is idle for a certain time. You can re-open it using the command
Initially your home directory on the remote system is used as the current working directory.
Useful commands available in most versions (use help to check) for navigating around the remote filing system include:
lswhich lists the files in the current working directory
dirwhich on some systems gives a more detailed listing
cd namewhich changes to the subdirectory name within the current working directory
cd ..which changes to the parent directory of the current directory
pwdwhich displays the name of the current working directory
Many versions of SFTP provide similar commands for navigating through the directories on your local system (e.g. lcd to change directory on the local system). Others simply provide a command (usually the ! character) which escapes to the local operating system and allows you to issue whatever local commands are needed; exit returns you to the program.
The most useful commands for transferring files are
get (remote to local) and
put (local to remote). The syntax is
(which assumes the file is to be given the same name on the destination system as it has on the source), or
get file1 file2
where file1 is the source name, and file2 the destination name. The syntax for put is similar.
To fetch a file called
fred from your filespace on Hermes, and store it under a new name in subdirectory info on the client Unix system (e.g. MCS Linux)
sftp hermes.cam.ac.ukset up a connection to the server
Password for user sjc1000: *******
ftp> lslist the contents of the directory
ftp> lcd infopoint to the destination directory on DS-Filestore
ftp> get fred newfredfetch and rename the file
ftp> quitclose the connection and quit SFTP
Other useful commands for file transfer (not available in all versions of these programs) are mget and mput, which allow multiple transfers, e.g.
get command for every file in the current directory whose name begins with
data. Files cannot be renamed using
Usually you can interrupt a transfer using
Various SFTP programs provide some or all of the following:
rmdeletes a file on the remote system
mkdircreates a directory on the remote system
rmdirdeletes a directory on the remote system
chmodchanges permissions on the remote system
Unix sftp does not generally provide for checking your quota, or for displaying a file without fetching its contents to your filespace. These can be done using the ssh command, for example
ssh linux.ds.cam.ac.uk quota -v
ssh linux.ds.cam.ac.uk cat myfile|more
Some implementations of sftp require a numeric argument (rather than the more usual alphabetic codes) to the chmod command for changing access permissions; again, as an alternative you can use ssh:
ssh linux.ds.cam.ac.uk chmod -or “*.doc”
In each case you will be asked for your password on the remote system.
Note that the SFTP protocol provides only a very simple mechanism for reporting errors. This means that, if you run out of quota while uploading files to a remote system via sftp, you will be told only that the operation has failed, but not the reason (you can check your quota using ssh as described above). In general the common sftp clients do not have good error handling facilities. For this reason, non-interactive use of sftp is not recommended (and is not described here). The scp command has better error messages and more reliable exit codes, and is thus safer for batch use.