Secure Copy Protocol (SCP) is built on top of SSH and provides a simple way to transfer files between hosts while maintaining encryption. One of its most useful features is the ability to recursively copy entire directory structures with a single command.
When you need to transfer a folder that contains subdirectories (like a typical web project with css
, js
, and images
subfolders), the -r
flag is your best friend:
scp -r /local/path/to/site username@remotehost:/remote/path
This command will:
1. Preserve the entire directory structure
2. Copy all files in all subdirectories
3. Maintain file permissions (when using -p flag)
For your specific case of deploying a static website:
scp -rp /Users/yourname/site user@ubuntu-server:/var/www/html
Additional useful flags:
-p : Preserve modification times and modes
-v : Verbose output (helpful for debugging)
-C : Enable compression (faster transfers for text files)
When dealing with large directories:
# Use compression for text-heavy projects
scp -Crp site/ user@server:/path/
# Limit bandwidth if needed (1000 = 1MB/s)
scp -rp -l 1000 site/ user@server:/path/
While SCP is great for initial transfers, consider rsync
for subsequent updates as it only transfers changed files:
rsync -avz --delete /local/site/ user@server:/remote/site/
Key differences:
- Only transfers changed files (faster)
- --delete removes files not present locally
- -z enables compression
- -a preserves all attributes
If you encounter problems:
# Ensure SSH access works first
ssh user@server
# Check directory permissions on remote
ssh user@server "ls -ld /remote/path"
# For connection timeouts
scp -o ConnectTimeout=30 -r site/ user@server:/path/
When working with static websites or project structures containing nested directories, the -r
(recursive) flag becomes essential. SCP handles this perfectly by preserving the entire directory hierarchy during transfer.
For transferring your website folder structure (like the example with index.html, css/, js/, and images/):
scp -r /local/path/to/site user@remote.server:/remote/path/
Combine recursive transfer with other useful flags:
scp -rpC /local/site/ user@example.com:/var/www/html/
Where:
-r
: Recursive copy-p
: Preserves modification times and modes-C
: Enables compression for faster transfers
Here's exactly how I deploy my static sites:
scp -rpC ~/projects/mysite/ deploy@myserver.com:/var/www/mysite.com/public_html/
For large projects, consider:
tar czf - /local/site/ | ssh user@server "tar xzf - -C /remote/path"
This creates a compressed tarball and pipes it directly to the remote server.
Permission issues: Always ensure you have write permissions on the target directory:
ssh user@server "mkdir -p /remote/path && chmod -R 755 /remote/path"
Hidden files: By default, SCP transfers hidden files (like .htaccess) when using -r
.