File Transfer (scp & sftp)
Objective
Transfer files securely between local and remote hosts using scp for quick copies and sftp for interactive sessions.
Tools & Technologies
scpsftprsyncssh
Key Commands
scp file user@host:/pathscp -r dir/ user@host:/pathsftp user@hostrsync -avz src/ user@host:/dest/Architecture Overview
sequenceDiagram
participant L as Local Machine
participant S as SSH Daemon
participant R as Remote Host
L->>S: SSH connection (port 22)
S-->>L: Authentication OK
L->>R: scp: send file data
R-->>L: Acknowledgement
Note over L,R: All data encrypted via SSH tunnel
Step-by-Step Process
01
scp — Secure Copy
scp copies files over SSH. Syntax mirrors cp but with user@host: prefix for remote paths.
# Upload: local → remote
scp report.pdf user@matrix:~/
# Download: remote → local
scp user@matrix:~/data.csv ./
# Copy directory recursively
scp -r project/ user@matrix:~/
# Specify port
scp -P 7800 file user@matrix:~/
02
sftp — Interactive Transfer
sftp gives you an interactive shell for browsing and transferring files.
sftp user@matrix
# SFTP commands:
ls # list remote
lls # list local
cd remote_dir
lcd local_dir
get remote_file
put local_file
mget *.txt # multiple files
bye # exit
03
rsync — Efficient Sync
rsync only transfers changed data, making it ideal for backups and keeping directories in sync.
# Sync local dir to remote
rsync -avz ~/projects/ user@host:~/projects/
# Delete files removed locally
rsync -avz --delete src/ user@host:/dest/
# Dry run to preview changes
rsync -avzn src/ user@host:/dest/
Challenges & Solutions
- SCP transfers the whole file even if one byte changed — use rsync for large files
- SFTP and FTP are completely different protocols
Key Takeaways
- rsync --dry-run (-n) always before --delete to avoid surprises
- Set up SSH keys to avoid passwords on every scp/rsync