1. 程式人生 > >liunx-shell比較兩個資料夾下的文字內容

liunx-shell比較兩個資料夾下的文字內容

兩個資料夾下面的檔案個數及其名稱完全一樣,但是內容有的可能不一樣,這個指令碼遍歷資料夾下的所有子資料夾,包括巢狀多層的,然後使用linux系統的diff命令對兩個名稱一樣的檔案進行比較,指令碼如下:

#!/bin/sh


if [ $# -ne 2 ]

then
    echo Usage: compare.sh fold_name_1 fold_name_2
    exit
fi

if [ ! -d $1 ]
then
    echo $1 is not a
    exit
fi

if [ ! -d $2 ]
then
    echo $2 is not a
    exit
fi

fold1=$(echo $1 | sed 's|\(^[^/]*\).*|\1|')
fold2=$(echo $2 | sed 's|\(^[^/]*\).*|\1|')

compareFOLD()
{
    for file in $1/*
    do
        if [ -d $file ] then
            compareFOLD $file
        elif [ -f $file ]
        then
            if [ ! -L $file ]
            then
                file2=$(echo $file | sed "s|^.[^/]*\(.*\)|$fold2\1|")
                diff $file $file2
                if [ $? -ne 0 ]
                then
                    echo -e "\033[32m"$file
                    echo $file2
                    echo -e "\033[0m"---------------------------------------------------------------------
                fi
            fi
        fi
    done
}

compareFOLD $fold1

轉載:https://blog.csdn.net/lhf19891003/article/details/19114653?utm_source=jiancool

           http://blog.chinaunix.net/uid-11911430-id-2801535.html