SFTP commands cheat sheet

Download a printable SFTP commands cheat sheet here.

SFTP is a secure file transfer protocol, based on SSH (Secure Shell), which is the standard way to connect to UNIX/LINUX servers. SFTP works in a client-server architecture, meaning that a client connects to a server and uploads files to it or downloads files from it.

Additionally, the SFTP client allows you to list or delete files, create directories, and change file ownership and permissions. To begin an SFTP session, you can either use the option of password authentication, or create SSH keys for a passwordless SFTP login.

💡
Read our guides on SFTP security, comparing SFTP to FTP and FTPS and our SFTP vs. FTPS benchmarks for more insight into the protocol and how to optimize your SFTP workflow.

There are several ways to connect to SFTP:

  • CLI (Command Line Interface): Most modern operating systems include an SFTP CLI (commonly the OpenSSH sftp client), allowing users to type out text commands to communicate with an SFTP server.
  • GUI (Graphical User Interface): This allows you to list, download, and upload files using a graphical interface which is friendlier and supports the mouse or trackpad as an input device to allow drag & drop to upload and download files. The most popular SFTP GUI clients are Filezilla and Cyberduck.
  • Programming libraries: There are a number of standard programming libraries, for pretty much any programming language, that let programmers interact with SFTP servers in their code.

This cheat sheet covers the commands you run in a command-line SFTP session (the sftp> prompt), and we’ll focus on the command line interface route.


Getting started

To start the SFTP command line, open your terminal (Command Prompt on Windows; Terminal on Mac and Linux) and connect using one of the following formats:

  • sftp user@host
  • sftp user@host:/remote-dir replace the placeholders with your username, SFTP host name, and remote directory; note the colon before the remote path)

When prompted, enter your password, or authenticate using your private/public SSH key pair (passwordless login).

Once you’re connected, you’ll be in an interactive SFTP session (often shown as the sftp> prompt). From there, you can run commands to list files, switch directories, upload, and download.

💡
Note: Some commands take arguments. Optional arguments (ones you can choose to omit) are enclosed in <angle brackets>.

Listing Files

Command: ls <options> <path>

ls lists the contents of the current directory on the remote server.

When using ls with no arguments, all the files found within the current directory will be displayed in a jumbled fashion. To change the current directory, use the cd command as demonstrated below.
Two optional arguments are available when using the ls command:

  1. <options> - The <options> argument can be populated with any of the following options or a combination of them; for example: the command `-alt` is acceptable.
  • -1 - List the output in a single column
  • -a - Show all files - include files that start with a dot (.)
  • -h - Show human readable file sizes
  • -l - Long listing format, including additional file information such as file sizes and permissions.

Use the following options to change the order of files in the list:

  • -S - Sort list by file size
  • -t - Sort list by last modification time
  • -r - Sort in reverse order
  1. <path> - Passing the path argument to the ls command allows you to display the list of files in a specified remote path. The example below indicates how to do it. Use the command ls -l and a directory file list should be available like so:
sftp> ls -l /outgoing-invoices
-rwxr--r--   1        -        -    21451 Nov 24 08:28 inv-UIf2IS.json
-rwxr--r--   1        -        -    69070 Nov 22 09:12 inv-UPrGAT.json
-rwxr--r--   1        -        -    15714 Jul 14 15:18 inv-v5rg8c.j
💡
Note: To list files within your local host, use lls instead of ls in the sftp console.

Switching Directories

Command: cd path

Use cd to switch from one directory to another on the remote server.

To switch the full, absolute, path to the directory, prefix the path with a /. To switch to a relative path (e.g. a directory that resides within your current directory), simply type in its name.

Example:

sftp> cd outgoing-invoices


Command: lcd path

lcd works just like cd, except it only changes the active directory on the local host.

Example:

sftp> lcd /Users/john/Documents/invoices

Download and Upload Files

Command: get remote-file <local-dir>

Get lets you download a file (or files) from the remote server to the local computer. The get command has two arguments:

  1. remote-file - the path to the remote file(s) to download. To download multiple files in a single command, add a wild card (*) to the remote-file argument. This argument is mandatory.
  2. <local-dir> - The target directory to place the downloaded files on the local host. If left empty, files will be automatically downloaded to the current local directory. This argument is optional.

Example:

sftp> get inv-UIf2IS.json
Fetching /outgoing-invoices/inv-UIf2IS.json to inv-UIf2IS.json
/inv-UIf2IS.json                                                                                                                                          100% 21451     9.1KB/s   00:00   

The following example makes use of a wildcard to download any json file whose name starts with inv:

sftp> get inv*.json

Command: put local-file <remote-path>

Use put to upload a file (or files) from the local computer to the remote host. Two arguments can be added to the put command:

  1. local-file - the path to the local file(s) to upload. Use wildcards (*) to upload multiple files in a single command.
  2. <remote path> - The target directory to place the uploaded files on the remote server. If left empty, files will be automatically uploaded to the current remote directory.

Example:

sftp> put inv-jKv72b.json /outgoing-invoices/
Uploading inv-jKv72b.json to /outgoing-invoices/inv-jKv72b.json
inv-jKv72b.json                                                                                                                                           100% 34596     9.0KB/s   00:00 

Check Present Working Directories

Command: pwd

This command shows the present working directory path on the remote host.

Example:

sftp> pwd
Remote working directory: /outgoing-invoices

Command: lpwd

lpwd shows the present working directory on the local computer.

Example:

sftp> lpwd
Local working directory: /Users/john/Documents/invoices

Create or remove Directories

Command: mkdir remote-path

Create a new directory on the remote machine. For example:

sftp> mkdir outgoing-invoices

Command: rmdir remote-path

Remove an empty directory on the remote host. If the directory is not empty, an error message will appear. Another example follows:

sftp> rmdir dummy-dir

Remove files

Command: rm remote-path

Delete a file or files on the remote host. You can use wildcards(*), just like with the get command.

Example:

sftp> rm inv-jKv72b.json
Removing /outgoing-invoices/inv-jKv72b.json

Disconnecting

Command: quit or bye

When you're done fiddling with files on your SFTP server, close the connection and leave the SFTP CLI program.

sftp> quit

Simple, isn't it?



And finally...

This cheat sheet covered the SFTP command line basics you’ll use most: connecting, listing files, navigating directories, uploading and downloading, and ending a session. If you want a fast reference while you work, download the printable cheat sheet and keep it next to your terminal.

Download a printable SFTP commands cheat sheet here.

Frequently asked questions

What are the most common SFTP commands I’ll use day to day?

Most workflows rely on ls to list files, cd to change remote folders, pwd to confirm your remote path, get to download, put to upload, mkdir and rmdir to manage remote directories, rm to delete remote files, and quit or bye to exit the session.

How do I start an SFTP session from the command line?

Open a terminal and connect with sftp user@host, then authenticate with a password or SSH key. Once connected, you’ll see an interactive prompt (often sftp>) where you can run commands like ls, cd, get, and put.

Can I start in a specific remote directory when I connect?

Some command-line SFTP clients let you specify a remote path as part of the destination, for example user@host:/remote-dir (the colon separates the host from the remote path). If your client does not support this, connect normally and then cd into the directory after login.

What’s the difference between local and remote commands in SFTP?

Remote commands act on the server you’re connected to. Local commands act on your own machine. Many SFTP CLIs provide local equivalents prefixed with l, such as lpwd (local working directory), lcd (change local directory), and lls (list local files).

How do I upload or download multiple files in one command?

Many SFTP clients support wildcards. For example, get inv*.json can download multiple matching remote files, and put *.csv can upload multiple matching local files. Use extra caution with wildcards when deleting files with rm.

Is SFTP the same as FTP, and is SFTP encrypted?

SFTP is not FTP. SFTP is SSH File Transfer Protocol and runs over SSH. In common setups, the session is encrypted in transit because it is carried inside the SSH connection.