Directory Transfer
SCP can copy entire directory trees recursively using the -r flag. This is useful for deploying application folders, copying configuration directories, or moving project structures between servers.
Syntax
scp -r [OPTIONS] SOURCE_DIR DESTINATION
Unlike rsync, SCP does not distinguish between dir and dir/. Both will copy the directory itself (not just the contents). The result is always destination/dir/.
Copy Patterns
1. Push a Local Directory to Remote
Command:
scp -r ./my-project/ deploy@server:/opt/apps/
Output:
index.html 100% 4096 2.1MB/s 00:00
style.css 100% 2048 1.8MB/s 00:00
app.js 100% 8192 3.4MB/s 00:00
config.json 100% 1024 1.2MB/s 00:00
The result on the remote server:
/opt/apps/my-project/
├── index.html
├── style.css
├── app.js
└── config.json
2. Pull a Remote Directory to Local
Command:
scp -r admin@server:/etc/nginx/ ./nginx-backup/
Output:
nginx.conf 100% 4096 2.5MB/s 00:00
sites-available/default 100% 1024 1.1MB/s 00:00
sites-enabled/default 100% 35 0.5MB/s 00:00
mime.types 100% 5120 3.0MB/s 00:00
3. Recursive Copy with Preserved Attributes
Command:
scp -rp ./configs/ admin@server:/etc/app/
Output:
app.conf 100% 2048 1.5MB/s 00:00
db.conf 100% 512 0.8MB/s 00:00
cache.conf 100% 256 0.6MB/s 00:00
The -p flag ensures modification times, access times, and file permissions are preserved.
4. Recursive Copy with Bandwidth Limit
Command:
scp -r -l 10000 ./uploads/ admin@server:/var/www/uploads/
Output:
photo1.jpg 100% 5MB 1.2MB/s 00:04
photo2.jpg 100% 3MB 1.2MB/s 00:02
photo3.jpg 100% 8MB 1.2MB/s 00:06
The -l 10000 limits bandwidth to approximately 10 Mbit/s (1.2 MB/s).
5. Recursive Copy with Compression
Command:
scp -rC ./logs/ admin@server:/archive/logs/
Output:
access.log 100% 150MB 25.0MB/s 00:06
error.log 100% 50MB 22.0MB/s 00:02
debug.log 100% 200MB 28.0MB/s 00:07
The -C flag compresses data in transit. Text-heavy files like logs benefit most.
Common Pitfalls
| Pitfall | What Happens | Prevention |
|---|---|---|
Forgetting -r | Only copies the directory entry, not its contents | Always use -r for directories |
| Symlinks in tree | Symlinks are followed and real files are copied | Be aware of link targets on the source |
| Large nested trees | Very slow compared to rsync (no delta) | Use tar + SCP or switch to rsync |
| Permission mismatch | Remote user may not own copied files | Use -p or chown after transfer |
What's Next?
Learn how to use wildcards and transfer multiple files in Wildcard and Multiple Files.