Mac上快速比較兩個文件夾差異

將下面腳本保存為 campare.sh 文件,執(zhí)行

sh campare.sh 文件夾1 文件夾2
#!/bin/bash

# 檢查參數(shù)數(shù)量
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <dir1> <dir2>"
    exit 1
fi

dir1="$1"
dir2="$2"

# 確保目錄存在
if [ ! -d "$dir1" ] || [ ! -d "$dir2" ]; then
    echo "Both arguments must be directories."
    exit 1
fi

# 找出所有的文件,并進行內(nèi)容比較
find "$dir1" -type f | while read -r file1; do
    # 獲取相對路徑
    rel_path="${file1#$dir1/}"
    file2="$dir2/$rel_path"
    
    # 檢查文件是否存在
    if [ -f "$file2" ]; then
        # 先比較文件大小
        size1=$(stat -f%z "$file1")
        size2=$(stat -f%z "$file2")
        
        if [ "$size1" -ne "$size2" ]; then
            echo "Different (size mismatch): $rel_path"
        else
            # 使用 shasum 進行哈希比較
            hash1=$(shasum -a 256 "$file1" | awk '{print $1}')
            hash2=$(shasum -a 256 "$file2" | awk '{print $1}')
            if [ "$hash1" != "$hash2" ]; then
                echo "Different (content mismatch): $rel_path"
            fi
        fi
    else
        echo "Missing in dir2: $rel_path"
    fi
done

# 處理 dir2 中存在但 dir1 中不存在的文件
find "$dir2" -type f | while read -r file2; do
    rel_path="${file2#$dir2/}"
    file1="$dir1/$rel_path"
    
    if [ ! -f "$file1" ]; then
        echo "Missing in dir1: $rel_path"
    fi
done
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容