To download a printable cheat sheet, click 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.
There are several ways to connect to SFTP:
- CLI - this stands for Command Line Interface. All modern operating systems come equipped with an SFTP CLI program, allowing users to type out text commands to communicate with an SFTP server.
- GUI - or Graphical User Interface, which 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.
In this post, we’ll focus on the command line interface route.
Getting started
To start the command line, open your terminal (cmd on Windows; Terminal on Mac and Linux) and type in sftp user@host/remote-dir
(replace the placeholders with the actual user name, sftp host name and remote directory). Then, either type in your chosen password or utilize the private/public key authentication option to login.
Once you’re in, it can only go up from here. Using the command line is quite simple once you understand the meaning of each given command. In this post, we got you covered and will explain the important and useful commands .
Note: Some commands have arguments (Don’t worry, we’ll explain what those do as well). Optional arguments (i.e. ones you can choose to drop) 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:
<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
<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:
- 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.
- <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:
- local-file - the path to the local file(s) to upload. Use wildcards (*) to upload multiple files in a single command.
- <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.
We hope this guide has proven how non-intimidating commonly used SFTP commands are to use and encourage you to get your command line interface on and try it out for yourself!
To download a printable cheat sheet, click here |
---|
Post Photo by Athul Cyriac Ajay on Unsplash