Skip to content

sftp Command Cheat Sheet

sftp is an interactive file transfer program, similar to ftp, but it performs all operations over an encrypted ssh transport.


Synopsis

sftp [options] [user@]host

Connecting

sftp user@remote.server.com
Type your password or use SSH keys. You get an sftp> prompt.


Interactive Commands

Command Description
ls List remote files
lls List local files
cd path Change remote directory
lcd path Change local directory
pwd Print remote working directory
lpwd Print local working directory
get file Download file
put file Upload file
mkdir dir Create remote directory
rm file Delete remote file
!cmd Run shell command locally
exit / bye Quit Sftp

Transferring Files

Download

get data.zip
Rename while downloading:
get data.zip local_backup.zip

Upload

put screenshot.png

Recursive Directory Transfer (-r)

get -r /var/www/html/
put -r my_project/
(Requires -r flag when starting sftp on some older versions, but mostly automatic inside the shell now).


Automated Batch Mode (-b)

Create a batch file commands.txt:

cd /upload
put data.csv
bye

Run non-interactively:

sftp -b commands.txt user@remote


Resuming Transfers

If a download fails, use reget. If an upload fails, use reput.

reget huge_file.iso

Notes

  • Security: Unlike FTP, passwords and data are never sent in cleartext.
  • Port: Uses standard SSH port (22). Use -P 2222 to specify custom.