BASH
Sync Directories Efficiently with Rsync
Automate seamless file synchronization between local and remote directories using rsync for efficient backups or deployments, skipping unchanged files and deleting old ones.
rsync -avz --delete /path/to/local/dir user@remote_host:/path/to/remote/dir
# Example usage:
# rsync -avz --delete ~/my_project_build/ [email protected]:/var/www/html/my_project/
How it works: This snippet demonstrates how to use `rsync` for robust directory synchronization. The `-a` flag (archive mode) preserves permissions, ownership, timestamps, and recursively copies directories. The `-v` flag provides verbose output, and `-z` enables compression during transfer, which is especially useful over network connections. The `--delete` flag removes files from the destination directory that no longer exist in the source, ensuring an exact mirror. Replace `/path/to/local/dir` and `user@remote_host:/path/to/remote/dir` with your actual paths and SSH credentials.