Təkrarlanan Fayl Tapıcı

Qovluqda təkrarlanan faylları tapır və hesabat verir. MD5 hash istifadə edərək eyni məzmuna malik faylları aşkarlayır.

Yayımlanma: 02.05.2024

Kod

#!/bin/bash

# Duplicate File Finder

if [ -z "$1" ]; then
    echo "Usage: $0 <directory>"
    exit 1
fi

DIR="$1"
REPORT="duplicates_$(date +%Y%m%d_%H%M%S).txt"

if [ ! -d "$DIR" ]; then
    echo "Error: Directory not found: $DIR"
    exit 1
fi

echo "Scanning for duplicate files..."
echo "This may take a while for large directories..."
echo ""

declare -A file_hashes
duplicates_found=0

while IFS= read -r -d "" file; do
    if [ -f "$file" ]; then
        hash=$(md5sum "$file" | cut -d" " -f1)
        if [ -n "${file_hashes[$hash]}" ]; then
            echo "DUPLICATE FOUND:" >> "$REPORT"
            echo "  Original: ${file_hashes[$hash]}" >> "$REPORT"
            echo "  Duplicate: $file" >> "$REPORT"
            echo "  Size: $(du -h "$file" | cut -f1)" >> "$REPORT"
            echo "" >> "$REPORT"
            ((duplicates_found++))
        else
            file_hashes[$hash]="$file"
        fi
    fi
done < <(find "$DIR" -type f -print0)

if [ $duplicates_found -eq 0 ]; then
    echo "No duplicates found!"
else
    echo "Found $duplicates_found duplicate(s)"
    echo "Report saved to: $REPORT"
fi

İstifadə

chmod +x find_duplicates.sh
./find_duplicates.sh /path/to/directory

Teqlər

duplicate file finder md5 hash