Networking & SSH EssentialsLesson 5.3
Transferring files with scp and rsync
scp syntax, rsync flags -avz, rsync --delete, dry run, bandwidth limit, partial transfers, rsync over SSH, exclude patterns
scp for Simple Transfers, rsync for Everything Else
scp is simple: it copies files over SSH. rsync is smarter: it transfers only changed parts of files, supports resuming interrupted transfers, and can delete destination files that no longer exist at source. Use rsync for anything beyond a quick one-off copy.
scp Basics
# Copy local file to remote
scp ./deploy.sh user@prod:/home/user/
# Copy remote file to local
scp user@prod:/var/log/app.log ./
# Copy directory recursively
scp -r ./dist/ user@prod:/var/www/html/rsync: The Right Tool for Syncing
# Sync local dir to remote (-a=archive -v=verbose -z=compress)
rsync -avz ./dist/ user@prod:/var/www/html/
# Delete files on remote that no longer exist locally
rsync -avz --delete ./dist/ user@prod:/var/www/html/
# Dry run — see what would change without doing it
rsync -avz --dry-run --delete ./dist/ user@prod:/var/www/html/
# Exclude node_modules and .git
rsync -avz --exclude='.git' --exclude='node_modules/' ./ user@prod:~/app/
# Limit bandwidth (in kbps)
rsync -avz --bwlimit=1000 large_file.tar user@prod:~/Always run with --dry-run first when using --delete to verify you are not about to wipe important files on the remote server.
