IC數字前端設計開發18 (用shell指令碼編寫區別兩個資料夾內檔案的不同)
阿新 • • 發佈:2019-01-10
#!/bin/bash mkdir tmp basedir="./tmp" workdir="./" #這是定義兩個目錄,按照你的說法,這兩個目錄下檔名應該都是相同的 dir1="txt_test0" dir2="txt_test1" #這是用來儲存查詢dir1目錄的所有檔案路徑和名稱 tempf1="$(mktemp -p $basedir)" #這是定義比較檔案輸出到的日誌檔案 diff_out="diff_between_${dir1}_and_${dir2}.log" #比較總是需要以一個目錄為基準,這裡假設以dir1目錄為比較基準,這裡是遞迴查詢所有檔案,並將結果儲存到臨時檔案 find ${workdir}/$dir1 -type f >$tempf1 echo "trace id=0" #判斷如果目錄中存在對比後的log,就改名 if [ -f "${diff_out}" ];then mv "${diff_out}" "${diff_out}-$(date +%H%M%S)" fi echo "trace id=1" #如果查詢dir1目錄沒有查詢到任何檔案就退出 if [ -z "$tempf1" ];then exit 1 fi #通過迴圈來比較dir1目錄和dir2目錄的檔案 for myfile in $(cat $tempf1) do myfile2="$(echo $myfile | sed "s/${dir1}/${dir2}/")" if [ -f "$myfile2" ];then diff -u $myfile $myfile2 | tee -a "$diff_out" else echo "$myfile2 not exist!" fi done rm -f $tempf1