|

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
ExampleDescription
scp test.txt user@192.168.1.10:/home/userCopies 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:

OptionPurpose
-rRecursive copy of a directory tree
-vVerbose mode
-P portSpecify the port number to be used
-CCompression (uses zlib or bzip2)
-l limitSets a bandwidth limit in bytes/sec
-qQuiet 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: scp is being gradually deprecated in favor of sftp and rsync in newer OpenSSH versions because of a security weakness in the legacy protocol. It still works everywhere, but for serious work, consider rsync -avz -e ssh instead.


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]
ExampleDescription
sftp kronos@192.168.1.10Connects 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:

CommandDescription
rmdir dirnameDeletes the dirname directory
mkdir dirnameCreates a directory named dirname
rm filenameDeletes a file
get filename /home/kronosDownloads a file to the local location
put file /remote/locationUploads a file to the remote system
lsLists files in the remote directory
cd dirChanges the remote directory
lcd dirChanges the local directory
pwdShows the remote working directory
lpwdShows the local working directory
exit / bye / quitCloses the session
man sftpTo 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:

OptionPurpose
-P portSpecify the port
-i keyUse a specific private key
-b fileBatch mode โ€” read commands from a file
-vVerbose
-CEnable 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

CommandPurpose
scp FILE user@host:/pathLocal โ†’ remote
scp user@host:/path FILERemote โ†’ local
scp -r DIR user@host:/pathRecursive directory copy
scp -P PORT FILE user@host:/pathCustom port
scp -i KEY FILE user@host:/pathUse a specific key
scp -C FILE user@host:/pathEnable compression
scp -l N FILE user@host:/pathBandwidth limit
scp -q FILE user@host:/pathQuiet mode
scp -v FILE user@host:/pathVerbose mode

sftp

CommandPurpose
sftp user@hostConnect to a remote host
lsList remote files
cd DIRChange remote directory
lcd DIRChange local directory
pwd / lpwdShow remote/local directory
get FILEDownload a file
get FILE /local/pathDownload to a specific path
put FILEUpload a file
put FILE /remote/pathUpload to a specific path
mkdir DIRCreate remote directory
rmdir DIRDelete remote directory
rm FILEDelete remote file
bye / exit / quitClose session
man sftpShow all commands

sftp Options

OptionPurpose
-P PORTSpecify port
-i KEYUse a specific key
-b FILEBatch mode
-CCompression
-vVerbose

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

PitfallProblemSolution
Missing colonscp file host/pathAdd : โ†’ host:/path
Wrong case -p vs -PWrong option-P = port, -p = preserve
No -r for directoriesCopy failsAdd -r
Permission deniedNo write accessCheck perms on target
Host key changedMITM warningVerify fingerprint
Slow transferNo compressionAdd -C
No resumeRestart on failureUse rsync instead
Wrong keyAuth failsUse -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

CommandPurposeExample
scp FILE user@host:/pathLocal โ†’ remotescp a.txt kronos@host:/tmp/
scp user@host:/path FILERemote โ†’ localscp kronos@host:/tmp/a.txt .
scp -r DIR user@host:/pathRecursivescp -r dir/ kronos@host:/tmp/
scp -P PORT FILE user@host:/pathCustom portscp -P 2222 a.txt kronos@host:/tmp/
scp -i KEY FILE user@host:/pathSpecific keyscp -i ~/.ssh/id_rsa a.txt kronos@host:/tmp/
scp -C FILE user@host:/pathCompressionscp -C big.log kronos@host:/tmp/
scp -l N FILE user@host:/pathBandwidth limitscp -l 500 a.txt kronos@host:/tmp/
sftp user@hostConnectsftp kronos@192.168.1.10
get FILEDownloadget readme.txt
put FILEUploadput local.txt
ls / cd / pwdBrowse remotels, cd logs, pwd
lcd / lpwdBrowse locallcd ~/downloads
mkdir / rmdirManage dirsmkdir newfolder
rm FILEDelete remote filerm old.txt
byeExitbye
sftp -b FILE user@hostBatch modesftp -b cmds.txt kronos@host

Key takeaways:

  • scp copies files in one shot โ€” perfect for scripts and quick transfers
  • sftp opens an interactive session โ€” perfect for browsing and managing files
  • Both run over SSH, so everything is encrypted
  • Use -r for directories, -P for ports (uppercase!), -i for keys, -C for compression
  • scp syntax is source then destination โ€” the colon (user@host:/path) is what makes it remote
  • sftp commands mirror FTP: get, put, ls, cd, mkdir, rm
  • Use batch mode (sftp -b) for automated transfers in scripts
  • For large or resumable transfers, prefer rsync over scp
  • 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!