Moving files through SFTP doesn't require command-line expertise, but knowing CLI commands gives you more control and automation options. While GUI clients like FileZilla or Cyberduck offer user-friendly interfaces, the command line provides powerful flexibility for file transfers.
The SFTP command line interface (CLI) is a method to perform SFTP commands using a text-based terminal like PowerShell. First, you'll need to connect to the SFTP server. You can do this in the application with the sftp command as below.
sftp username@hostname
You'll then be asked for the password. Enter the password when prompted to connect. After successfully connecting, you'll see a prompt like this:
sftp>
This prompt is your gateway to all SFTP commands. There are several SFTP commands but we'll focus on the most common and important ones and how they function.
The most essential SFTP commands fall into three main categories:
pwd # See current remote directory
cd # Change the current remote directory
lpwd # See current local directory
lcd # Change the current local directory
get # Download a file
put # Upload a file
mget # Download multiple files from a directory
mput # Upload multiple files to a directory
mkdir # Create remote directory
rmdir # Remove remote directory and contents
Think of SFTP as having the option to navigate two file explorers at the same time: one for the local device and another for remote directories. You'll need to specify which one to perform the action on, and then what you want to do.
Here are some examples of some common navigation commands:
Local Commands | Remote Commands |
---|---|
lcd |
cd |
lpwd |
pwd |
lls |
ls |
lmkdir |
mkdir |
Pro tip: Use pwd
and lpwd
frequently to avoid uploading files to the wrong location.
You can navigate the contents of both local and remote directories entirely using CLI commands. But CLI commands can be used for more than navigation, such as configuring SFTP workflows.
Here are typical SFTP workflows you'll encounter and how to complete them with the CLI:
lcd /local/project
put dist/* /remote/www/
This command uploads project files from a local directory to a remote server.
lcd /local/project
changes the local working directory to specify where the files will be uploaded fromput dist/*
selects all files and subdirectories in the dist
folder/remote/www/
indicates the destination directory on the remote server
Backing Up Remote Data
lcd /backup
get -r /remote/data/* .
This command creates a backup of remote data to a local directory.
lcd /backup
sets the local destination directory for the downloaded filesget -r
recursively downloads entire directory structures/remote/data/*
specifies all files and subdirectories from the remote data location.
represents the current local directory as the download destination
Managing Configurations
get /etc/config.ini
# Edit locally
put config.ini /etc/config.ini
This workflow gets a remote configuration file for local editing.
get /etc/config.ini
downloads the configuration file from the remote serverput config.ini /etc/config.ini
uploads the modified configuration back to the original locationSFTP provides several commands for managing file permissions. To change permissions, start by entering the command for what characteristic you want to modify, followed by the updated permissions. Permissions can be modified either in symbolic mode or absolute mode.
Symbolic mode uses letters to represent permissions. r is for read, w for write, x for execute, and then who it applies to, where u is the currently connected user, g is the group the user belongs to, and o is for others.
For example, u+w, o+r means the current user can write, while others can read only.
Absolute mode uses a three-digit number to represent permissions, where each digit represents a set of permissions and for whom they apply to, where 4 is for read, 2 is for write, and 1 is for execute. Each of those numbers are added up to specificy the permissions, where the first digit is the user, second is for the group, and third is for others.
For example, 764 would break down to
Here are some examples of how you can use some common commands to change permissions:
Command | Use Case | Example | Explanation |
---|---|---|---|
chmod |
Change file permissions | chmod 755 file.txt |
Changed permissions for file.txt so current user has read, write, execute. Group and others can read + execute. |
chown |
Change file owner | chown john file.txt |
Change the owner of file.txt to the user john. |
chgrp |
Change file group | chgrp devs file.txt |
Change the group ownership of file.txt to the group devs. |
The SFTP command line is a powerful tool, and there are a few pro tips that can make navigating and modifying simple.
Use Progress Display
progress # Toggle progress meter for large transfers
Resume Interrupted Transfers
reget large-file.zip # Resume download of large-file.zip
reput huge-backup.tar # Resume upload of huge-backup.tar
The !
command lets you temporarily escape to your local shell without disconnecting from SFTP:
!ls # Shows local directory contents
!pwd # Shows local working directory
After running your local shell command, you'll automatically return to the SFTP prompt. No need to reconnect.
Three commands will cleanly close your SFTP session:
exit # Most common and widely supported
quit # Works on most systems
bye # Friendly alternative
Using any of these commands will:
You should always use one of these commands instead of just closing the terminal to ensure a clean disconnect from the SFTP server.
Pro tip: For a full local shell session, just type !
by itself. You'll get your regular shell prompt. When running a local shell session with this method, type exit
to return to SFTP.
If you forget any SFTP commands or need information, try one of these helpful commands:
help # Show all commands
? # Same as help
version # Check SFTP version
For a quick reference guide to all these commands, download our SFTP Command Cheat Sheet.
The cheat sheet includes every command mentioned here plus many more advanced options for when you're ready to dive deeper into SFTP's capabilities.