|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ULTRACOMPARE SCRIPT FOR EXTRACTING DIFFERING FILES |
| 4 | +# COMPARE 'ORIGINAL' AND 'MODIFIED' |
| 5 | +# CREATE 'DIFF' WITH ONLY 'MODIFIED' FILES, THAT DON'T MATCH 'ORIGINAL' |
| 6 | + |
| 7 | +# Prompt user for original and modified paths |
| 8 | +read -p "Enter the path to the original directory: " ORIGINAL_PATH |
| 9 | +read -p "Enter the path to the modified directory: " MODIFIED_PATH |
| 10 | + |
| 11 | +# Verify paths exist |
| 12 | +if [ ! -d "$ORIGINAL_PATH" ]; then |
| 13 | + echo "Error: Original directory '$ORIGINAL_PATH' does not exist." |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +if [ ! -d "$MODIFIED_PATH" ]; then |
| 18 | + echo "Error: Modified directory '$MODIFIED_PATH' does not exist." |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +# Log files |
| 23 | +ORIGINAL_LOG="original-log.txt" |
| 24 | +MODIFIED_LOG="modified-log.txt" |
| 25 | +DIFF_LOG="diff-log.txt" |
| 26 | + |
| 27 | +# Output directories |
| 28 | +DIFF_DIR="diff" |
| 29 | +OLD_DIR="old" |
| 30 | + |
| 31 | +# Create a timestamped folder for cleanup |
| 32 | +TIMESTAMP=$(date "+%Y%m%d-%H%M%S") |
| 33 | +ARCHIVE_DIR="$OLD_DIR/$TIMESTAMP" |
| 34 | + |
| 35 | +# Ensure the old/ directory exists |
| 36 | +mkdir -p "$ARCHIVE_DIR" |
| 37 | + |
| 38 | +# Function to generate log with relative paths and SHA-256 hashes |
| 39 | +generate_log() { |
| 40 | + local base_path="$1" |
| 41 | + local log_file="$2" |
| 42 | + find "$base_path" -type f | while read -r file; do |
| 43 | + # Get relative path and hash |
| 44 | + relative_path="${file#$base_path/}" # Remove base path prefix |
| 45 | + hash=$(sha256sum "$file" | awk '{print $1}') |
| 46 | + # Write to log file |
| 47 | + echo "$relative_path | $hash" >> "$log_file" |
| 48 | + done |
| 49 | +} |
| 50 | + |
| 51 | +# Generate logs for original and modified paths |
| 52 | +echo "Generating log for original path..." |
| 53 | +> "$ORIGINAL_LOG" # Clear previous log |
| 54 | +generate_log "$ORIGINAL_PATH" "$ORIGINAL_LOG" |
| 55 | + |
| 56 | +echo "Generating log for modified path..." |
| 57 | +> "$MODIFIED_LOG" # Clear previous log |
| 58 | +generate_log "$MODIFIED_PATH" "$MODIFIED_LOG" |
| 59 | + |
| 60 | +# Copy the entire modified directory structure to the diff directory |
| 61 | +echo "Creating diff directory from modified path..." |
| 62 | +rm -rf "$DIFF_DIR" |
| 63 | +cp -a "$MODIFIED_PATH" "$DIFF_DIR" |
| 64 | + |
| 65 | +# Create diff-log.txt from modified-log.txt |
| 66 | +cp "$MODIFIED_LOG" "$DIFF_LOG" |
| 67 | + |
| 68 | +# Final step: Remove all matching lines from diff-log.txt |
| 69 | +echo "Finalizing diff-log.txt by removing matches from original-log.txt..." |
| 70 | +while IFS= read -r original_line; do |
| 71 | + # Extract relative path and hash from the original log |
| 72 | + original_relative_path=$(echo "$original_line" | cut -d'|' -f1 | xargs) |
| 73 | + original_hash=$(echo "$original_line" | cut -d'|' -f2 | xargs) |
| 74 | + |
| 75 | + # Create the matching pattern and remove it from diff-log.txt |
| 76 | + grep_pattern="^${original_relative_path//./\\.} | $original_hash$" |
| 77 | + sed -i "/$grep_pattern/d" "$DIFF_LOG" |
| 78 | +done <"$ORIGINAL_LOG" |
| 79 | + |
| 80 | +# Remove matching files from the diff directory |
| 81 | +echo "Removing matching files from diff directory..." |
| 82 | +while IFS= read -r original_line; do |
| 83 | + original_relative_path=$(echo "$original_line" | cut -d'|' -f1 | xargs) |
| 84 | + target_file="$DIFF_DIR/$original_relative_path" |
| 85 | + if [ -f "$target_file" ]; then |
| 86 | + rm -f "$target_file" |
| 87 | + fi |
| 88 | +done <"$ORIGINAL_LOG" |
| 89 | + |
| 90 | +# Clean up empty directories in the diff directory |
| 91 | +find "$DIFF_DIR" -type d -empty -delete |
| 92 | + |
| 93 | +# Move logs and diff directory to the archive |
| 94 | +echo "Archiving logs and diff directory..." |
| 95 | +mv "$ORIGINAL_LOG" "$ARCHIVE_DIR/" |
| 96 | +mv "$MODIFIED_LOG" "$ARCHIVE_DIR/" |
| 97 | +mv "$DIFF_LOG" "$ARCHIVE_DIR/" |
| 98 | +mv "$DIFF_DIR" "$ARCHIVE_DIR/" |
| 99 | + |
| 100 | +echo "Process complete. Logs and diff directory archived in $ARCHIVE_DIR." |
0 commit comments