File Transfer (SFTP, SCP, FTP)
File transfer using sftp
SFTP can be used for transfers between two systems on which you have accounts (e.g. from the PWF to your own machine. Where available, it is preferable to FTP for security reasons. Fetch is a graphical SFTP (and FTP) client for the Macintosh. On Windows, WinSCP can be used in sftp mode.
SFTP cannot be used for anonymous FTP (transfers from a publicly available system not requiring a user identifier or password).
SFTP on Unix systems
This rest of this page describes Unix sftp, which can be used on most Unix systems including MacOS X.
SFTP on Unix systems looks rather similar to FTP, so much of this section also applies if you are using FTP. SFTP does not have certain of the FTP facilities but many of these are provided by the ssh command instead, as explained later.
To invoke SFTP on a Unix system, use the command
sftp hostname
where hostname is the name of the remote system you
wish to contact, e.g. sftp hermes.cam.ac.uk or
sftp sftp.pwf.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 open hostname.
Finding files
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 directorydirwhich on some systems gives a more detailed listingcd namewhich changes to the subdirectory name within the current working directorycd ..which changes to the parent directory of the current directorypwdwhich 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.
Transferring files
The most useful commands for transferring files are
get (remote to local) and put (local to
remote). The syntax is
get file
(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.
Example
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. PWF Linux)
sftp hermes.cam.ac.ukset up a connection to the serverPassword for user sjc1000: *******ftp> lslist the contents of the directoryftp> lcd infopoint to the destination directory on the PWFftp> get fred newfredfetch and rename the fileftp> 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.
mget data*
executes a get command for every file in the current directory whose name begins with data. Files cannot be renamed using mget or mput.
Usually you can interrupt a transfer using ctrl/C
Other file operations
Various SFTP programs provide some or all of the following:
rmdeletes a file on the remote systemmkdircreates a directory on the remote systemrmdirdeletes a directory on the remote systemchmodchanges permissions on the remote system
Note that the sftp commands chgrp, chown, ln and symlink (not described here) are deliberately disabled when the host is Pelican.
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.pwf.cam.ac.uk quota -vssh linux.pwf.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.pwf.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.
The title of this document is:
University Computing Service: File Transfer using SFTP
URL:
http://www.cam.ac.uk/cs/filetransfer/sftp.html
Last updated: February 2009
- Useful Links
- Unix Support

