Checksum

使用“md5sum -c”的困難

  • December 20, 2018

我在md5sum驗證一些複製的文件時遇到了一些困難。

我有兩個目錄:dir1dir2. 其中dir1有五個文件:file1file2file3和。是空的。file4``file5``dir2

如果我這樣做cp dir1/* dir2

然後:md5sum dir1/* > checksums

然後:md5sum -c checksums

結果是:

dir1/file1: OK
dir1/file2: OK
dir1/file3: OK
dir1/file4: OK
dir1/file5: OK

但這不好。我希望它將文本文件中的校驗和與 dir2 中復製文件的校驗和進行比較。

嘗試:

$ (cd dir1 && md5sum *) > checksums
$ cd dir2
$ md5sum -c ../checksums

checksums的內容看起來像:

d41d8cd98f00b204e9800998ecf8427e  file1
................................  file2
................................  file3
................................  file4
................................  file5

可以試試這個

#Create your md5 file based on a path - recursively
pathtocheck=INSERTYOURPATHHERE
find $pathtocheck -type f -print0 | xargs -0 md5sum >> xdirfiles.md5

#Compare All Files
md5results=$(md5sum -c xdirfiles.md5)

#Find files failing Integrity Check
echo "$md5results" | grep -v OK

#Count the files good or bad.
lines=0
goodfiles=0
badfiles=0
while read -r line;
do
 lines=$(($lines + 1))
 if [[ $line == *"OK"* ]]; then
   goodfiles=$(($goodfiles + 1))
 else
   badfiles=$(($badfiles + 1))
 fi
done <<< "$md5results"
echo "Total Files:$lines Good:$goodfiles - Bad: $badfiles"

那是你自己的遊戲……直接回答你關於如何檢查dir2的問題……只需在每個文件前面強制使用/ dir2 / w / sed。它提供了檢查文件的絕對路徑。

sed -I "s/  /  \/dir2\//g" xdirfiles.md5

[root@server testdir]# md5sum somefile
d41d8cd98f00b204e9800998ecf8427e  somefile
[root@server testdir]# md5sum somefile > somefile.md5
[root@server testdir]# sed -i "s/  /  \/dir2\//g" somefile.md5
d41d8cd98f00b204e9800998ecf8427e  /dir2/somefile

使用的 sed 命令的細分

sed -i <- Inline replacement.
s/ <- Means to substitute. (s/thingtoreplace/replacewiththis/1)
"  " <- represented a double space search.
/ <- to begin the Replacement String values
"  \/dir2\/" <-  Double Spaces and \ for escape characters to use /. The
final /g means global replacement or ALL occurrences. 
/g <- Means to replace globally - All findings in the file. In this case, the md5 checksum file seperates the hashes with filenames using a doublespace. If you used /# as a number you would only replace the number of occurrences specified.

引用自:https://unix.stackexchange.com/questions/213933