SFTP Command Cheat Sheet

Secure File Transfer Protocol Reference Guide

What is SFTP?

SFTP (SSH File Transfer Protocol) is a secure file transfer protocol that uses SSH encryption to transfer files. Unlike regular FTP, SFTP encrypts both commands and data, providing a higher level of security.

Connection Commands

Command Description Example
sftp user@hostname Connect to an SFTP server sftp user@example.com
sftp -P port user@hostname Connect to an SFTP server with a specific port sftp -P 2222 user@example.com
sftp -i keyfile user@hostname Connect using a private key file sftp -i ~/.ssh/id_rsa user@example.com
exit or bye or quit End your SFTP session exit

Navigation Commands

Command Description Example
pwd Display current remote directory pwd
lpwd Display current local directory lpwd
cd directory Change remote directory cd /var/www/html
lcd directory Change local directory lcd ~/Documents
ls List contents of remote directory ls -la
lls List contents of local directory lls -la

File Transfer Commands

Command Description Example
get remotefile [localfile] Download a file from remote to local get data.txt ~/Downloads/data.txt
get -r remotedir [localdir] Download a directory recursively get -r /var/www/images ~/backup
put localfile [remotefile] Upload a file from local to remote put report.pdf /home/user/docs/
put -r localdir [remotedir] Upload a directory recursively put -r ~/project /var/www/
mget *.txt Download multiple files using wildcards mget *.jpg
mput *.txt Upload multiple files using wildcards mput *.html
Tip: Use the -r flag with get and put to transfer directories recursively.

File Management Commands

Command Description Example
mkdir directory Create a directory on the remote server mkdir /home/user/newdir
lmkdir directory Create a directory on the local machine lmkdir ~/newdir
rm file Delete a file on the remote server rm /home/user/oldfile.txt
rmdir directory Delete an empty directory on the remote server rmdir /home/user/emptydir
rename oldname newname Rename a file on the remote server rename file.txt newfile.txt
chmod permissions file Change permissions of a remote file chmod 644 file.txt
Warning: Be careful when using rm and rmdir commands as they permanently delete files and directories.

Helpful SFTP Client Commands

Command Description Example
help or ? Show available commands help
!command Execute a local shell command !ls -la
df -h Display disk space usage df -h
version Show SFTP version version
progress Toggle display of progress meter progress

Command Line SFTP Examples

Task Command
Non-interactive file upload echo "put file.txt" | sftp user@hostname
Non-interactive file download echo "get file.txt" | sftp user@hostname
Using a batch file of commands sftp -b batchfile.txt user@hostname
Setting password in script (not recommended) sshpass -p "password" sftp user@hostname
Security Note: Using passwords in scripts is not secure. Use SSH keys for authentication whenever possible.