Chapter 4. SSH (secure) file transfer (SFTP)

[Tip]Tip

If you want to transfer just one file (or a directory compressed into a tarball[37]), and you know the exact path of the source and destination, you can use scp (a secure version of cp[35]) instead of SFTP.

You can upload a file (which we'll call my_file) from the current directory on your machine to your home directory[13] on the server by typing:

scp ./my_file your_username@server_name:/path_to_your_home_directory/my_file

To download a file (say, a file called my_file from the path ~/classes/cse332[36] on the server to the current directory on your machine), type:

scp your_username@server_name:/path_to_your_home_directory/classes/cse332/my_file ./my_file

Note that you must specify the file name twice: on the local end (your machine) and on the remote end (the server).

If you're not sure what the path to your home directory on the server is, try typing pwd (mentioned in Table 3.1, “Commands for navigating the file system”) when you're connected to the server via SSH[2] and in your home directory (typing cd to get to your home directory if necessary, as shown in Example 3.1, “Using cd”).

Be careful, though: you can easily overwrite files (without notification from scp that you're doing so, of course) if you specify the wrong path -- the author managed to do this with his home page!

You can use SFTP to transfer files to or from a Linux server.

Start SFTP by typing sftp username@server_name at the prompt, filling in the appropriate values for your username and the server's name. The commands that you'll most likely need are listed in Table 4.1, “SSH file transfer (SFTP) commands”.

[Note]Note

SFTP commands look a lot like those discussed in Section 3.2, “Navigating the file system” and Section 3.3, “Manipulating the file system”, but commands to be run on the local machine (the one in front of you) have an l prefix (such as lls), while commands to be run on the remote machine (the one you're connecting to) don't have an l prefix (such as ls).

As far as I can tell, command history and command completion (discussed in Section 3.1, “Features of the shell”) are not available in SFTP. In addition, you cannot transfer entire directories through SFTP; you must compress them first into a tarball[37] and then transfer the tarball.

Example 4.1. Using SSH file transfer (SFTP)

sftp jg18@grid.cec.wustl.edu

[Note that not even asterisks will be displayed on the screen as you type your password.]

You'll then have a prompt like sftp> at which you can type commands.


Table 4.1. SSH file transfer (SFTP) commands

CategoryCommandAction
Basic SFTP Commands---
~

help

or

?

View list of SFTP commands
~

exit

or

quit

Exit SFTP
Working with File Systems---
~

(l)cd dir_name

Change directory on remote (local) machine
~

(l)ls [path]

List remote (local) directory contents
~

(l)mkdir dir_name

Create remote (local) directory
~

(l)pwd

Print name of working remote (local) directory
~

rm file

(there is no "lrm")
Remove remote file
~

rmdir dir_name

(there is no "lrmdir")
Remove remote empty directory
Downloading and Uploading---
~

get remote_path [local_path]

Download from remote_path to working local directory [to local_path instead]
~

put local_path [remote_path]

Upload from local_path to working remote directory [to remote_path instead]
Other Commands---
~

!some_command

Execute some_command on local machine



[35] The command cp (copy) is discussed in Section 3.3, “Manipulating the file system”.

[36] The symbol ~ indicates your home directory, as noted in Table 3.2, “Common symbols from the file system”.


Back to Guide main page