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 destinationCopy 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
| Flag | What it does |
|---|---|
-r | Recursive (directories) |
-p | Preserve timestamps and permissions |
-P PORT | Non-default port (uppercase P — differs from ssh) |
-i FILE | Identity file |
-C | Enable compression |
-q | Quiet, no progress bar |
-l KBPS | Limit bandwidth (kbits/sec) |
-3 | Route remote-to-remote through local (default in newer OpenSSH) |
-J JUMP | Jump 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
scpby default (-sflag). 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)
| Command | What it does |
|---|---|
ls / lls | List remote / local files |
cd / lcd | Change remote / local directory |
pwd / lpwd | Print remote / local working dir |
get file | Download file |
get -r dir | Download directory |
put file | Upload file |
put -r dir | Upload directory |
mget *.log | Download multiple files |
mput *.csv | Upload multiple files |
rm file | Delete remote file |
rmdir dir | Remove remote directory |
mkdir dir | Create remote directory |
chmod 644 file | Change remote permissions |
rename old new | Rename remote file |
df -h | Remote disk usage |
!cmd | Run a local shell command |
bye / exit / quit | Exit 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/
byeComparison
| Tool | Best for |
|---|---|
scp | Quick one-off file copies |
rsync | Syncing directories, large files, resumable |
sftp | Interactive browsing, multiple operations |