Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

SFTP via the Command Line

Simple instructions for uploading or downloading files and directories using Secure File Transfer Protocol (sFTP) in Linux.

If you have a large quantity of files that you want to transfer from one server to another, the File Transfer Protocol (FTP) is the way to go. This comes in a secure version called SFTP which uses SSH (Secure Shell) to encrypt the data.

Traditionally uploads and downloads via FTP would be conducted using a GUI software programme such as WinSCP or FileZilla, but it’s actually possible to transfer files using SFTP on the command line. Here’s how it’s done:

Connect to the server

To connect to a remote SFTP server, establish a secure SSH connection and then create a SFTP session as shown:

$ sftp

To check the remote working directory, enter the pwd command:

sftp> pwd
Remote working directory: /group/home/adam/

To check the local working directory, type the lpwd command:

sftp> lpwd
Local working directory: t:\files\photos


Before uploading whole directories, you’ll need to create a target directory on the remote server. Let’s call this “photos”:

sftp> mkdir photos

If you don’t want to create a directory in the current path (as determined by pwd), cd to the location where you want to create your new directory:

sftp> cd /group/home/adam/path/to/my/files/
sftp> mkdir photos

To start the upload, your local working directory will need to be in the parent. So in this case, if we want to upload the entire directory from t:\files\photos, then our local working directory should be t:\files. We can easily change this via the lcd command then check via lpwd:

sftp> lcd t:\files
sftp> lpwd
Local working directory: t:\files

To upload, the put command is used:

sftp> put -pr photos

The -p flag preserves all modification times, access times, and modes from the original files transferred whilst the -r flag allows subdirectories and subfolders to be uploaded.

Typical output may look like this:

sftp> put -pr upload
Uploading upload/ to /group/home/adam/
Entering photos/
photos/IMG_1507.jpg          100%   10MB  265.1KB/s   00:39
photos/IMG_1507.xmp          100%   8703  21.3KB/s    00:00
photos/IMG_1511.jpg          100%   10MB  265.5KB/s   00:37
photos/IMG_1511.xmp          100%   8702  50.0KB/s    00:00
Entering photos/pix
photos/test/IMG_1507.CR2     100%   25MB  223.0KB/s   01:56
photos/test/IMG_1511.CR2     100%   25MB  188.9KB/s   02:16


To download files from a Linux server via the command line, you’ll need to change the remote working directory to the parent. So, if I wanted to download files from /group/home/adam/photos, I’d need to cd to /group/home/adam:

sftp> cd /group/home/adam/

The target location on the local server also needs to be specified and must already exist. If it does not exist, it can be created in the command line as follows:

sftp> lmkdir photos

The local working directory should also be the parent. So if we need to download to t:\downloads\photos, the local working directory should be set to t:\downloads.

The get command can then be used to download the files:

sftp> get -r photos

Typical output:

sftp> get -r photos
Fetching /group/home/adam/photos/ to
Retrieving /group/home/adam/photos
/group/home/adam/photos/IMG_1507.xmp       100%   8703    6.2KB/s   00:01
/group/home/adam/photos/IMG_1507.jpg       100%   10MB  488.9KB/s   00:21
Retrieving /group/home/adam/photos/pix
/group/home/adam/photos/pix/IMG_1511.CR2   100%   25MB  386.1KB/s   01:06
/group/home/adam/photos/pix/IMG_1507.CR2   100%   25MB  782.7KB/s   00:33
/group/home/adam/photos/pix/IMG_1511.jpg   100%   10MB  494.4KB/s   00:20
/group/home/adam/photos/pix/IMG_1511.xmp   100%   8702   39.4KB/s   00:00

Exiting the Shell

To exit, type exit into the command line.



No comments have yet been submitted. Be the first!

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.