SFTP (SSH File Transfer Protocol) is a secure way to transfer files between computers over an encrypted SSH connection. This article shows you the most useful SFTP commands with concrete examples you can type right now, covering everything from basic uploads and downloads to batch operations and permission changes.
What Is SFTP and How Does It Differ from FTP?
SFTP stands for SSH File Transfer Protocol. It runs inside an SSH (Secure Shell) connection, which means all data—including your login credentials and the files you transfer—is encrypted. This is fundamentally different from legacy FTP (File Transfer Protocol), which sends everything in plain text and is vulnerable to eavesdropping. SFTP uses port 22 by default, the same port as SSH, whereas traditional FTP uses port 21. You can think of SFTP as a file-transfer feature built into the SSH protocol, not a separate daemon. Most modern servers that offer SSH also offer SFTP without any extra configuration.
How to Connect to an SFTP Server
The basic syntax for connecting to an SFTP server is:
sftp username@hostname
For example, to connect as user alice to server example.com:
sftp alice@example.com
You will be prompted for your password. If you use a non-standard SSH port (not 22), specify it with the -P flag (capital P, unlike the lowercase -p used in some other tools):
sftp -P 2222 alice@example.com
You can also use a key file for authentication, which is more secure and allows automation:
sftp -i ~/.ssh/id_rsa alice@example.com
Once connected, you’ll see an sftp> prompt. Type help to list all available commands.
Essential SFTP Commands for Navigation and Inspection
These commands help you see where you are and what’s there, both locally and remotely.
- pwd – Print working directory on the remote server.
- lpwd – Print working directory on your local machine.
- ls – List files in the current remote directory. Add
-lfor details,-afor hidden files. - lls – List files in the current local directory.
- cd – Change remote directory (e.g.,
cd /var/www/html). - lcd – Change local directory (e.g.,
lcd ~/Downloads). - mkdir – Create a remote directory.
- lmkdir – Create a local directory.
- rmdir – Remove an empty remote directory.
- lrmdir – Remove an empty local directory.
Example session:
sftp> pwd Remote working directory: /home/alice sftp> lcd ~/Desktop sftp> ls -l drwxr-xr-x 2 alice alice 4096 Jan 12 10:30 reports -rw-r--r-- 1 alice alice 12000 Jan 12 10:31 data.csv
Uploading and Downloading Files
These are the core operations. Use put to upload a file from your local machine to the remote server, and get to download a file from the server to your local machine.
- put localfile – Uploads
localfileto the current remote directory. - get remotefile – Downloads
remotefileto the current local directory. - put localfile remotefile – Uploads and renames.
- get remotefile localfile – Downloads and renames.
- mput *.txt – Uploads all .txt files in the local directory.
- mget *.csv – Downloads all .csv files from the remote directory.
Examples:
sftp> put report.pdf Uploading report.pdf to /home/alice/report.pdf report.pdf 100% 45KB 4.5MB/s 00:00 sftp> get backup.tar.gz Fetching /home/alice/backup.tar.gz to backup.tar.gz backup.tar.gz 100% 12MB 8.2MB/s 00:01
When using mput or mget, you’ll be prompted to confirm each file. To skip the prompts, add the -f flag (for newer OpenSSH versions) or use the prompt command to toggle prompting off.
Directory Operations and Recursive Transfers
To transfer an entire directory and its contents, use the -r (recursive) option with put or get.
sftp> put -r ./assets Uploading assets/ to /home/alice/assets
sftp> get -r /var/www/html Fetching /var/www/html/ to html/
To synchronize directories (only transfer files that have changed), you can use the mirror command available in newer OpenSSH versions (8.7+).
sftp> mirror put localdir sftp> mirror get remotedir
The mirror command can also delete files that no longer exist on the source side with the -d flag, but use that carefully.
File Management: Rename, Delete, and Permissions
SFTP lets you manage remote files without leaving the session.
- rename oldname newname – Rename a remote file or directory.
- rm filename – Delete a remote file.
- lrm filename – Delete a local file.
- chmod mode filename – Change permissions on a remote file (e.g.,
chmod 755 script.sh). - chown user:group filename – Change owner (if you have privileges).
- ln -s target linkname – Create a symbolic link on the remote server.
Example:
sftp> chmod 600 private.key sftp> rename old_report.pdf new_report.pdf sftp> rm temp.txt
Batch Mode and Scripting with SFTP
You can run SFTP commands non-interactively by putting them in a batch file and using the -b flag. This is useful for automated backups or scheduled transfers.
sftp -b batch.txt alice@example.com
Where batch.txt contains commands like:
cd /backups put /home/alice/daily.sql quit
Alternatively, you can pipe commands directly:
echo "put file.txt" | sftp alice@example.com
For more complex automation, consider using scp (simple one-off transfers) or rsync over SSH (for incremental syncs), but SFTP batch mode works well for simple scripted tasks.
Common Problems and Troubleshooting
- Connection refused – The server may not have SSH/SFTP enabled, or a firewall is blocking port 22. Try a different port with
-P. - Permission denied – Your username or password is wrong, or the server’s
sshd_configrestricts SFTP to certain users. Check with your admin. - Host key verification failed – The server’s host key has changed (possibly a security risk) or you’re connecting to a new server. If you trust the server, remove the old key from
~/.ssh/known_hosts. - File transfer stalls – Large files may take time. Use the
-v(verbose) flag to see what’s happening. - SFTP not found – The SFTP subsystem may be disabled. Check that
Subsystem sftp internal-sftpis insshd_config.
SFTP vs. SCP vs. rsync
SFTP, SCP, and rsync all use SSH for secure transfer, but they serve different purposes:
- SFTP – Interactive, supports file management (rename, delete, chmod), works well for browsing and occasional transfers.
- SCP – Simple, one-shot file copy. No file management. Good for quick transfers, but being deprecated in some OpenSSH versions.
- rsync – Best for large or repeated syncs. It only transfers changed parts of files, supports compression, and can preserve permissions and timestamps. Use
rsync -avz -e ssh source/ user@host:/dest/.
For most interactive use, SFTP is the most versatile. For automated backups, rsync is usually the better choice.
Frequently Asked Questions
How do I list all files including hidden ones in SFTP?
Use ls -la to show all files, including hidden ones (starting with a dot), with detailed permissions and sizes.
Can I resume an interrupted SFTP download?
Yes, the reget command resumes a download, and reput resumes an upload. For example, reget bigfile.iso will continue from where it stopped.
How do I quit SFTP?
Type exit, quit, or bye at the sftp prompt. You can also press Ctrl+D.
Is SFTP the same as FTPS?
No. FTPS is FTP over SSL/TLS, a separate protocol. SFTP is a subsystem of SSH. They are not compatible; use the one your server supports.
How do I transfer a file from one remote server to another using SFTP?
SFTP itself cannot directly transfer between two remote servers. You must download to your local machine and then upload to the second server, or use a tool like rsync with SSH agent forwarding.
Why do I get “Permission denied” when trying to upload?
The remote directory may not be writable by your user. Check the directory permissions with ls -ld and ask your admin to grant write access if needed.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It