Automatic File Synchronization Between Two Directories

Automatic File Synchronization Between Two Directories

A Bash script that simplifies file synchronization between two directories is crucial for maintaining data consistency in different locations. The example below shows how easily this can be accomplished:

 1#!/bin/bash
 2
 3# Directories for synchronization
 4SOURCE_DIR="$HOME/source_directory"
 5DEST_DIR="$HOME/destination_directory"
 6
 7# File synchronization
 8rsync -av --delete "$SOURCE_DIR/" "$DEST_DIR/"
 9
10# Notification of completion
11echo "Synchronization completed: $SOURCE_DIR -> $DEST_DIR"

Script Explanation

The command rsync -av --delete "$SOURCE_DIR/" "$DEST_DIR/" copies all files and directories from the source directory to the target, while removing files in the target directory that are not present in the source. The -av option includes detailed copying and preserving access rights, which is particularly important for system administration and working with critical data.

comments powered by Disqus

Translations: