Get updates via: rss | twitter | email | google+

sftp Tutorial

Contents

  1. Introduction
  2. Syntax
  3. Examples
  4. Windows
  5. Mac OS X

Introduction

sftp stands for (Secure File Transfer Protocol) and it is another implementation of file transfer protocol over ssh (Secure Shell) like scp but newer and trying a more platform independent.

sftp is now a days mostly implemented as a replacement for FTP, mainly in web hosting providers, because of its security and because passwords are not passed unencrypted as they are in FTP.

Syntax

There are three different ways to invoke sftp program.

sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config] [-o ssh_option] [-P sftp_server_path] [-R num_requests] [-S program] [-s subsystem | sftp_server] host

sftp [user@]host[:file ...]

sftp [user@]host[:dir[/]]

sftp -b batchfile [user@]host

The first one is very similar to the ftp command, when you use this form, you enter in an interactive mode, you can create folders, navigate in the structure and retrieve and put files from and to the server.

The second way, works almost the same as scp it will ask for the password (unless you are using secret/public key to authenticate) and once you are authenticated the files will be downloaded or uploaded depending on the case.

The third form works almost the same as the first one but it let's you start in a specific remote folder.

The fourth form let's you specify a batch file where the commands to be run should be, in that case you need to enable a way to log in with no password, you can use this guide

Examples

sftp interactive mode

Download a file with sftp

If you want to retrieve a file from a sftp server, you can do so by using the interactive mode in the next way. First log into the server.

sftp user@server

Once logged in, you can list files with ls command, or navigate in the folders with cd command, just like as if you were using the shell. To retrieve a file, use this command.

get /path/to/remote/file

Download multiple files with sftp

mget *.txt

Will download all .txt files from current remote folder to current local folder

mget *

Will download all files from current remote folder to current local folder

Upload a file with sftp

If you want to put a file into the server, you can use something similar.

put /path/to/local/file

Download multiple files at once

mput *.txt

Will upload all .txt files

mput *

Will upload all files in current local folder to current remote folder.

Navigate in local and remote sides

In the above examples we assume you are located where you want to be, in case you may need to change your local or remote location, that can be done.

List files in remote side

ls

List files in local side

lls

Change folder in remote side

cd /path/to/remote-folder

Change folder in local side

lcd /path/to/local-folder

Create folders in remote side

mkdir folder

Create folders in local side

lmkdir folder

Getting help

You can get the complete set of commands available under interactive mode with the help command, once logged in run:

help

You will get this set of commands:

Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp grp path                     Change group of file 'path' to 'grp'
chmod mode path                    Change permissions of file 'path' to 'mode'
chown own path                     Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                   filesystem containing 'path'
exit                               Quit sftp
get [-Ppr] remote [local]          Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-Ppr] local [remote]          Upload file
pwd                                Display remote working directory
quit                               Quit sftp
rename oldpath newpath             Rename remote file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

sftp non-interactive mode

You can download files from a single command when using not interactive mode:

Retrieve a single file

sftp user@host:/path/to/file

Retrieve multiple files

sftp user@host:/path/to/files/*

That will get all files in the remote folder, if you want a specific type of files, narrow the option using wildcards.

sftp user@host:/paht/to/files/*.txt

Will get all files with .txt extension.

Download multiple files recursively

sftp -r user@host:/path/to/files/*

That command will copy all files from the remote folder and its sub-folders with all its contents.

Windows

WinSCP

When you are working on a Windows machine your best bet to use sftp protocol is WinSCP which is a scp, sftp and ftp client for Windows.

WinSCP is a complete SFTP client, and it has lots of features like:

Full Sync
This feature will keep both remote and local folders in sync, so any change you perform in any of the ends of the connection will be updated on the other end. The application should be kept on, and connected to keep this functionality running
Sync Remote

This feature will keep only the remote end in sync with the local, so any change made on the local side, will be updated in the remote side. But this will not work in other way. That is any change in remote will not be updated in local.

You can decide to delete remote files or not at the time of starting the syncronization. If you turn it on, any files deleted on local, will be deleted on remote, but if not, only new added files or changes to existing ones will be updated

Drag and Drop

You can also use the drag and drop feature to copy files from remote to local or viceversa

WinSCP screenshot, SFTP for Windows

Cyberduck

Another open source GUI tool for Windows is Cyberduck, released under the GNU licence, you can perform the same things with Cyberduck that you can with WinSCP, the main difference is that Cyberduck does not have the two panes window, and I must say I prefer the WinSCP two panes window for better browsing on both remote and local sides of the sftp connection.

With Cyberduck you need to open the Windows Explorer to copy files and paste them later on Cyberduck window, or you can drag and drop from one window to the other. Here is how it looks when loggin in:

Cyberduck on Windows loggin in

And the main window looks this way.

Cyberduck on Windows loggin in

You can emulate the view of WinSCP by putting both Windows Explorer and Cyberduck windows side by side in the screen, like this:

Cyberduck on Windows two panes

Mac OS X

Bein Mac OS X an Operating System based on Unix, you have both sftp client and sftp server (which is OpenSSH) already installed out of the box from the command line.

There are of course a lot of GUI clientes available for Mac

Paid Ones

Open Source Client

The one I use is CyberDuck, It supports Amazon S3 connection besides SFTP, you can drag and drop to and from the Finder app, you can keep folders in sync between your Mac and the remote OpenSSH server, you can create and delete files and folders.

CyberDuck SFTP GUI client for Mac OS X

You can do anything available from the command line, but using a graphic interface, which makes it a lot easier to work with sftp servers.


Last edit on: January 17, 2015

By: Guillermo Garron