Problem Statement
Compare different methods for transferring files between Linux systems: scp, rsync, and sftp. When should you use each?
Explanation
SCP (Secure Copy) uses SSH protocol for encrypted file transfers. Basic usage: scp source user@host:destination copies to remote, scp user@host:source destination copies from remote. Copy directories: scp -r directory/ user@host:path. Preserve timestamps and permissions: scp -p. Simple and secure but copies entire files even if destination has similar file - no delta transfer optimization.
Rsync is more efficient, only transferring changed portions of files (delta transfer). Basic usage: rsync -avz source/ user@host:destination. Flags: -a (archive mode preserving permissions, timestamps, symlinks), -v (verbose), -z (compression), -P (progress and partial transfers). Rsync over SSH: rsync -avz -e ssh. Exclude files: rsync --exclude='*.tmp'. Dry run: rsync -n tests without changes.
Rsync advantages: resume interrupted transfers, only transfer changed data (efficient for large files or slow connections), delete files in destination not in source (--delete), bandwidth limiting (--bw-limit), preserve hard links, ACLs, and extended attributes. Use rsync for backups, syncing large directories, or when source and destination have similar content. Rsync is ideal for incremental backups and mirroring.
SFTP (SSH File Transfer Protocol) provides interactive file transfer over SSH with commands similar to FTP. Connect: sftp user@host. Commands: ls (remote list), lls (local list), cd (remote change dir), lcd (local change dir), put file (upload), get file (download), mkdir, rm. Batch mode: sftp -b batchfile user@host processes commands from file. Good for interactive transfers or when GUI-like interface needed.
Choose SCP for simple one-time transfers of small files, rsync for efficient transfers of large directories or when files might change (backups, syncing), and SFTP for interactive browsing and transferring. All use SSH for security. For very large files, consider rsync with compression (--compress-level=9) and progress monitoring. Understanding these tools enables efficient secure file transfers in DevOps workflows.