
md5sum
一部のコピーされたファイルを検証するのに少し問題があります。
2つのディレクトリがあります:dir1
とdir2
。その中には、およびdir1
5 つのファイルがあります。file1
file2
file3
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にコピーされたファイルのチェックサムと比較したいと思います。
答え1
努力する:
$ (cd dir1 && md5sum *) > checksums
$ cd dir2
$ md5sum -c ../checksums
checksums
内容は次のとおりです。
d41d8cd98f00b204e9800998ecf8427e file1
................................ file2
................................ file3
................................ file4
................................ file5
答え2
これを試してみることができます
#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をチェックする方法についての質問に対する直接の答えです... sedを使用するすべてのファイルの前に/dir2/を強制的に適用するだけです。検査ファイルの絶対パスを提供します。
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.
答え3
最も基本的な作業形態は、以下を実行してチェックサムファイルのコピーを作成することです。
md5sum Dir1/*
コピー完了の効率をテストするディレクトリに変更します。 (バックアップや同様の作業をしながら他のファイルを同時にコピーする場合は、別途行う必要はありません。)
cp checksum Dir2/checksum
cd Dir2
2番目のディレクトリに変更するとコマンドが簡単になり、欠落しているファイルを処理する必要がある場合は、端末で動作するファイル(およびそのコマンド履歴)への正しいパスを取得するのに役立ちます。そうでない場合は、コピーして貼り付けてください。後でコマンドラインに入力してください。
md5sum -c checksum
コピーの完全性を提供します。