SSH Cheatsheet

scp and sftp

Use this SSH reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

scp — Secure Copy

scp copies files over SSH. Syntax mirrors cp but paths can be [user@]host:path.

scp [options] source destination

Copy Files

# Local → Remote
scp file.txt user@host:/remote/path/

# Remote → Local
scp user@host:/remote/path/file.txt ./local/

# Remote → Remote (routed through local machine)
scp user@host1:/file.txt user@host2:/dest/

Copy Directories

# Recursive copy
scp -r ./mydir user@host:/remote/path/

# Recursive with compression
scp -rC ./mydir user@host:/remote/path/

Common scp Flags

FlagWhat it does
-rRecursive (directories)
-pPreserve timestamps and permissions
-P PORTNon-default port (uppercase P — differs from ssh)
-i FILEIdentity file
-CEnable compression
-qQuiet, no progress bar
-l KBPSLimit bandwidth (kbits/sec)
-3Route remote-to-remote through local (default in newer OpenSSH)
-J JUMPJump host
# Non-default port example
scp -P 2222 file.txt user@host:/tmp/

# With jump host
scp -J user@bastion file.txt user@target:/tmp/

Note: OpenSSH 9.0+ uses the SFTP subsystem internally for scp by default (-s flag). The old SCP protocol is legacy.

rsync over SSH (Better than scp for Large Transfers)

# Basic sync
rsync -avz ./src/ user@host:/dest/

# Dry run first
rsync -avzn ./src/ user@host:/dest/

# Delete files on remote that no longer exist locally
rsync -avz --delete ./src/ user@host:/dest/

# Custom SSH port
rsync -avz -e "ssh -p 2222" ./src/ user@host:/dest/

# Limit bandwidth (KB/s)
rsync -avz --bwlimit=5000 ./src/ user@host:/dest/

sftp — Interactive File Transfer

sftp user@host
sftp -P 2222 user@host          # non-default port (uppercase P)
sftp -i ~/.ssh/mykey user@host

sftp Commands (Inside the Session)

CommandWhat it does
ls / llsList remote / local files
cd / lcdChange remote / local directory
pwd / lpwdPrint remote / local working dir
get fileDownload file
get -r dirDownload directory
put fileUpload file
put -r dirUpload directory
mget *.logDownload multiple files
mput *.csvUpload multiple files
rm fileDelete remote file
rmdir dirRemove remote directory
mkdir dirCreate remote directory
chmod 644 fileChange remote permissions
rename old newRename remote file
df -hRemote disk usage
!cmdRun a local shell command
bye / exit / quitExit sftp

sftp Batch Mode (Scripted)

# Execute commands from a file
sftp -b commands.txt user@host
# commands.txt
cd /var/www
put index.html
put -r assets/
bye

Comparison

ToolBest for
scpQuick one-off file copies
rsyncSyncing directories, large files, resumable
sftpInteractive browsing, multiple operations