Linux CLI 34 ๐ง scp and sftp commands
scp -v test.txt user@192.168.1.10:/home/user
scp user@192.168.1.10:/home/user/text.txt /home/user/
sftp kronos@192.169.1.10
scp and sftp both move files between systems over SSH โ meaning everything is encrypted. The difference is style: scp is a one-shot command (copy this file there and exit), while sftp is an interactive session (browse, upload, download, manage files on a remote system).
Key point: Both tools use SSH for transport, so they inherit SSH’s security โ encrypted transfers, key-based authentication, and the same port (22 by default). If you can ssh to a host, you can scp and sftp to it too.
a – scp command
scp (secure copy) is used to copy files between different Linux systems. This happens securely and over SSH. It’s the simplest way to move a file from one machine to another when you don’t need an interactive session.
Syntax:
scp [options] source_file destination_path
| Example | Description |
|---|---|
scp test.txt user@192.168.1.10:/home/user | Copies test.txt from the local system to the remote at /home/user |
scp user@192.168.1.10:/home/user/text.txt /home/user/ | Copies text.txt from the remote to the local system |
Common options:
| Option | Purpose |
|---|---|
-r | Recursive copy of a directory tree |
-v | Verbose mode |
-P port | Specify the port number to be used |
-C | Compression (uses zlib or bzip2) |
-l limit | Sets a bandwidth limit in bytes/sec |
-q | Quiet mode |
Examples:
# Basic: copy a local file to a remote host
$ scp test.txt user@192.168.1.10:/home/user
test.txt 100% 1024 1.2MB/s 00:00
# Copy a remote file to the local system
$ scp user@192.168.1.10:/home/user/text.txt /home/user/
text.txt 100% 2048 2.1MB/s 00:00
# Copy with a different name on the destination
$ scp report.pdf user@192.168.1.10:/home/user/final-report.pdf
# Verbose mode โ useful for debugging
$ scp -v test.txt user@192.168.1.10:/home/user
Executing: program /usr/bin/ssh host 192.168.1.10, user user, command sftp
...
test.txt 100% 1024 1.2MB/s 00:00
# Copy a whole directory (recursive)
$ scp -r project/ user@192.168.1.10:/home/user/
...
# Use a custom port
$ scp -P 2222 test.txt user@192.168.1.10:/home/user
...
# Compress during transfer (good for slow links)
$ scp -C bigfile.log user@192.168.1.10:/home/user/
...
# Limit bandwidth to 500 KB/s
$ scp -l 500 test.txt user@192.168.1.10:/home/user/
...
# Quiet mode (no progress bar)
$ scp -q test.txt user@192.168.1.10:/home/user
$
# Copy between two remote hosts (via your machine)
$ scp user1@host1:/path/file.txt user2@host2:/path/
...
# Use a specific SSH key
$ scp -i ~/.ssh/id_rsa test.txt user@192.168.1.10:/home/user
...
Wildcards work too โ the shell expands them locally, and scp transfers each match:
# Copy all .log files to the remote
$ scp *.log user@192.168.1.10:/home/user/logs/
access.log 100% 4096 4.1MB/s 00:00
error.log 100% 2048 2.1MB/s 00:00
Note:
scpis being gradually deprecated in favor ofsftpandrsyncin newer OpenSSH versions because of a security weakness in the legacy protocol. It still works everywhere, but for serious work, considerrsync -avz -e sshinstead.
b – sftp command
sftp (secure file transfer protocol) is used to transfer files between remote systems. This also happens securely and over SSH. It is similar to FTP but uses encryption and authentication, so your credentials and data are never sent in plain text.
Syntax:
sftp [options] [user@]host[:directory]
| Example | Description |
|---|---|
sftp kronos@192.168.1.10 | Connects to 192.168.1.10 with user kronos |
Example connection:
$ sftp kronos@192.168.1.10
Connected to 192.168.1.10.
sftp>
Once connected, you get an sftp> prompt where you can use commands. Many are familiar from FTP or a regular shell:
| Command | Description |
|---|---|
rmdir dirname | Deletes the dirname directory |
mkdir dirname | Creates a directory named dirname |
rm filename | Deletes a file |
get filename /home/kronos | Downloads a file to the local location |
put file /remote/location | Uploads a file to the remote system |
ls | Lists files in the remote directory |
cd dir | Changes the remote directory |
lcd dir | Changes the local directory |
pwd | Shows the remote working directory |
lpwd | Shows the local working directory |
exit / bye / quit | Closes the session |
man sftp | To see all the options and available commands |
Examples:
# Connect to a remote host
$ sftp kronos@192.168.1.10
Connected to 192.168.1.10.
sftp>
# List remote files
sftp> ls
readme.txt report.pdf logs/
# Change remote directory
sftp> cd logs
sftp> pwd
/remote/user/logs
# Change local directory
sftp> lcd /home/kronos/downloads
sftp> lpwd
/home/kronos/downloads
# Download a file
sftp> get readme.txt
Fetching /remote/user/readme.txt to readme.txt
readme.txt 100% 1024 1.2MB/s 00:00
# Download to a specific local path
sftp> get report.pdf /home/kronos/downloads/report.pdf
Fetching /remote/user/report.pdf to /home/kronos/downloads/report.pdf
report.pdf 100% 4096 4.1MB/s 00:00
# Upload a file
sftp> put localfile.txt
Uploading localfile.txt to /remote/user/localfile.txt
localfile.txt 100% 512 512kB/s 00:00
# Upload to a specific remote path
sftp> put localfile.txt /remote/user/archive/
Uploading localfile.txt to /remote/user/archive/localfile.txt
localfile.txt 100% 512 512kB/s 00:00
# Create a remote directory
sftp> mkdir newfolder
# Delete a remote file
sftp> rm oldfile.txt
Removing /remote/user/oldfile.txt
# Delete a remote directory
sftp> rmdir emptydir
# Exit
sftp> bye
$
Non-interactive use โ useful in scripts:
# Run sftp with a batch file
$ sftp -b commands.txt kronos@192.168.1.10
# commands.txt:
# cd /remote/path
# get file.txt
# put local.txt
# bye
Common sftp options:
| Option | Purpose |
|---|---|
-P port | Specify the port |
-i key | Use a specific private key |
-b file | Batch mode โ read commands from a file |
-v | Verbose |
-C | Enable compression |
Complete Example Session
# ============================================
# PART 1: SCP โ ONE-SHOT COPIES
# ============================================
# Local โ Remote
$ scp test.txt user@192.168.1.10:/home/user
test.txt 100% 1024 1.2MB/s 00:00
# Remote โ Local
$ scp user@192.168.1.10:/home/user/text.txt /home/user/
text.txt 100% 2048 2.1MB/s 00:00
# Verbose
$ scp -v test.txt user@192.168.1.10:/home/user
Executing: program /usr/bin/ssh host 192.168.1.10, user user, command sftp
...
test.txt 100% 1024 1.2MB/s 00:00
# Recursive directory
$ scp -r project/ user@192.168.1.10:/home/user/
...
# Custom port
$ scp -P 2222 test.txt user@192.168.1.10:/home/user
...
# Compression + bandwidth limit
$ scp -C -l 500 bigfile.log user@192.168.1.10:/home/user/
...
# ============================================
# PART 2: SFTP โ INTERACTIVE SESSION
# ============================================
$ sftp kronos@192.168.1.10
Connected to 192.168.1.10.
sftp> ls
readme.txt report.pdf logs/
sftp> pwd
/remote/user
sftp> cd logs
sftp> pwd
/remote/user/logs
sftp> lpwd
/home/kronos
sftp> lcd /home/kronos/downloads
sftp> lpwd
/home/kronos/downloads
# Download a file
sftp> get readme.txt
Fetching /remote/user/readme.txt to readme.txt
readme.txt 100% 1024 1.2MB/s 00:00
# Download to a specific path
sftp> get report.pdf /home/kronos/downloads/report.pdf
Fetching /remote/user/report.pdf to /home/kronos/downloads/report.pdf
report.pdf 100% 4096 4.1MB/s 00:00
# Upload a file
sftp> put localfile.txt
Uploading localfile.txt to /remote/user/localfile.txt
localfile.txt 100% 512 512kB/s 00:00
# Create a directory
sftp> mkdir newfolder
# Delete a file
sftp> rm oldfile.txt
Removing /remote/user/oldfile.txt
# Delete a directory
sftp> rmdir emptydir
# Exit
sftp> bye
$
# ============================================
# PART 3: SFTP โ BATCH MODE (SCRIPTING)
# ============================================
$ cat commands.txt
cd /remote/path
get file.txt
put local.txt
bye
$ sftp -b commands.txt kronos@192.168.1.10
sftp> cd /remote/path
sftp> get file.txt
Fetching /remote/path/file.txt to file.txt
...
sftp> put local.txt
Uploading local.txt to /remote/path/local.txt
...
sftp> bye
Quick Reference
scp
| Command | Purpose |
|---|---|
scp FILE user@host:/path | Local โ remote |
scp user@host:/path FILE | Remote โ local |
scp -r DIR user@host:/path | Recursive directory copy |
scp -P PORT FILE user@host:/path | Custom port |
scp -i KEY FILE user@host:/path | Use a specific key |
scp -C FILE user@host:/path | Enable compression |
scp -l N FILE user@host:/path | Bandwidth limit |
scp -q FILE user@host:/path | Quiet mode |
scp -v FILE user@host:/path | Verbose mode |
sftp
| Command | Purpose |
|---|---|
sftp user@host | Connect to a remote host |
ls | List remote files |
cd DIR | Change remote directory |
lcd DIR | Change local directory |
pwd / lpwd | Show remote/local directory |
get FILE | Download a file |
get FILE /local/path | Download to a specific path |
put FILE | Upload a file |
put FILE /remote/path | Upload to a specific path |
mkdir DIR | Create remote directory |
rmdir DIR | Delete remote directory |
rm FILE | Delete remote file |
bye / exit / quit | Close session |
man sftp | Show all commands |
sftp Options
| Option | Purpose |
|---|---|
-P PORT | Specify port |
-i KEY | Use a specific key |
-b FILE | Batch mode |
-C | Compression |
-v | Verbose |
Best Practices
โ Do This:
# Use scp for one-off file copies
scp file.txt user@host:/path # โ
# Use sftp for interactive browsing
sftp user@host # โ
# Use -r for directories
scp -r dir/ user@host:/path # โ
# Use -C on slow links
scp -C big.log user@host:/path # โ
# Use a specific key with -i
scp -i ~/.ssh/id_ed25519 file user@host:/path # โ
# Use batch mode for scripts
sftp -b commands.txt user@host # โ
# Verify destination paths before transfer
ls /path/to/dest # โ
โ Don’t Do This:
# Don't forget the colon in scp
scp file.txt user@host/path # โ missing :
# Don't copy sensitive files without checking perms
scp secret.key user@host:/tmp/ # โ world-readable
# Don't use scp for huge trees โ use rsync
scp -r huge_dir/ user@host:/path # โ slow, no resume
# Don't forget -P (uppercase) for scp ports
scp -p 2222 file user@host:/path # โ -p is preserve, not port
# Don't leave sftp sessions open on shared machines
sftp user@host # โ always `bye`
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
| Missing colon | scp file host/path | Add : โ host:/path |
Wrong case -p vs -P | Wrong option | -P = port, -p = preserve |
No -r for directories | Copy fails | Add -r |
| Permission denied | No write access | Check perms on target |
| Host key changed | MITM warning | Verify fingerprint |
| Slow transfer | No compression | Add -C |
| No resume | Restart on failure | Use rsync instead |
| Wrong key | Auth fails | Use -i KEY |
Real-World Examples
1. Copy a File to a Server
$ scp report.pdf kronos@192.168.1.10:/home/kronos/
report.pdf 100% 4096 4.1MB/s 00:00
2. Copy a File from a Server
$ scp kronos@192.168.1.10:/var/log/syslog ./
syslog 100% 10240 10.2MB/s 00:00
3. Copy a Whole Directory
$ scp -r project/ kronos@192.168.1.10:/home/kronos/
...
4. Copy Between Two Remote Hosts
$ scp kronos@host1:/data/file.txt kronos@host2:/backup/
# Traffic flows through your machine
5. Use a Non-Standard Port
$ scp -P 2222 file.txt kronos@192.168.1.10:/home/kronos/
6. Interactive SFTP Session
$ sftp kronos@192.168.1.10
Connected to 192.168.1.10.
sftp> ls
sftp> cd /var/log
sftp> get syslog
sftp> bye
7. Upload with SFTP
$ sftp kronos@192.168.1.10
sftp> put localfile.txt /home/kronos/upload/
sftp> bye
8. Batch SFTP Script
$ cat backup.sftp
cd /var/log
get syslog
get auth.log
bye
$ sftp -b backup.sftp kronos@192.168.1.10
9. Copy with Compression and Bandwidth Limit
$ scp -C -l 1000 bigfile.iso kronos@192.168.1.10:/backup/
# Compressed, limited to ~1 MB/s
10. Verify Transfer with Checksums
# Local
$ sha256sum file.txt
abc123... file.txt
# Remote
$ ssh kronos@192.168.1.10 "sha256sum /home/kronos/file.txt"
abc123... /home/kronos/file.txt
# โ
Match = transfer integrity confirmed
Visual: scp vs sftp
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ scp (One-shot command) โ
โ โ
โ $ scp file.txt user@host:/path โ
โ [============> ] 50% โ
โ โ
done โ exit โ
โ โ
โ โข Single command โ
โ โข Great for scripts โ
โ โข No interactive shell โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ sftp (Interactive session) โ
โ โ
โ $ sftp user@host โ
โ sftp> ls โ
โ sftp> cd /path โ
โ sftp> get file.txt โ
โ sftp> put local.txt โ
โ sftp> mkdir newfolder โ
โ sftp> rm old.txt โ
โ sftp> bye โ
โ โ
โ โข Persistent session โ
โ โข Browse + manage files โ
โ โข Batch mode for scripts โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Command | Purpose | Example |
|---|---|---|
scp FILE user@host:/path | Local โ remote | scp a.txt kronos@host:/tmp/ |
scp user@host:/path FILE | Remote โ local | scp kronos@host:/tmp/a.txt . |
scp -r DIR user@host:/path | Recursive | scp -r dir/ kronos@host:/tmp/ |
scp -P PORT FILE user@host:/path | Custom port | scp -P 2222 a.txt kronos@host:/tmp/ |
scp -i KEY FILE user@host:/path | Specific key | scp -i ~/.ssh/id_rsa a.txt kronos@host:/tmp/ |
scp -C FILE user@host:/path | Compression | scp -C big.log kronos@host:/tmp/ |
scp -l N FILE user@host:/path | Bandwidth limit | scp -l 500 a.txt kronos@host:/tmp/ |
sftp user@host | Connect | sftp kronos@192.168.1.10 |
get FILE | Download | get readme.txt |
put FILE | Upload | put local.txt |
ls / cd / pwd | Browse remote | ls, cd logs, pwd |
lcd / lpwd | Browse local | lcd ~/downloads |
mkdir / rmdir | Manage dirs | mkdir newfolder |
rm FILE | Delete remote file | rm old.txt |
bye | Exit | bye |
sftp -b FILE user@host | Batch mode | sftp -b cmds.txt kronos@host |
Key takeaways:
scpcopies files in one shot โ perfect for scripts and quick transferssftpopens an interactive session โ perfect for browsing and managing files- Both run over SSH, so everything is encrypted
- Use
-rfor directories,-Pfor ports (uppercase!),-ifor keys,-Cfor compression scpsyntax issourcethendestinationโ the colon (user@host:/path) is what makes it remotesftpcommands mirror FTP:get,put,ls,cd,mkdir,rm- Use batch mode (
sftp -b) for automated transfers in scripts - For large or resumable transfers, prefer
rsyncoverscp - Always verify permissions on the destination and checksums after transfer
Remember: scp is a command, sftp is a session. Use scp when you know exactly what you want to copy; use sftp when you need to explore or manage files. Both rely on SSH, so if ssh user@host works, so will they. Watch out for -P vs -p โ uppercase is port, lowercase is preserve. And if your transfer is huge or keeps failing, reach for rsync โ it resumes, verifies, and is faster on repeat runs.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!