Using md5deep
md5deep -r path/to/dir > sums.md5
Using find
and md5sum
find relative/path/to/dir -type f -exec md5sum {} + > sums.md5
请注意,当您运行检查 MD5 和时md5sum -c sums.md5
,您需要从生成的同一目录运行它sums.md5
文件。这是因为find
输出相对于您当前位置的路径,然后将其放入sums.md5
file.
如果这是一个问题你可以relative/path/to/dir
绝对的(例如,通过输入$PWD/
在你的路径前面)。这样你就可以运行检查sums.md5
从任何地点。缺点是现在sums.md5
包含绝对路径,这使得它更大。
功能齐全,使用find
and md5sum
您可以将此功能添加到您的.bashrc
文件(位于您的$HOME
目录):
function md5sums {
if [ "$#" -lt 1 ]; then
echo -e "At least one parameter is expected\n" \
"Usage: md5sums [OPTIONS] dir"
else
local OUTPUT="checksums.md5"
local CHECK=false
local MD5SUM_OPTIONS=""
while [[ $# > 1 ]]; do
local key="$1"
case $key in
-c|--check)
CHECK=true
;;
-o|--output)
OUTPUT=$2
shift
;;
*)
MD5SUM_OPTIONS="$MD5SUM_OPTIONS $1"
;;
esac
shift
done
local DIR=$1
if [ -d "$DIR" ]; then # if $DIR directory exists
cd $DIR # change to $DIR directory
if [ "$CHECK" = true ]; then # if -c or --check option specified
md5sum --check $MD5SUM_OPTIONS $OUTPUT # check MD5 sums in $OUTPUT file
else # else
find . -type f ! -name "$OUTPUT" -exec md5sum $MD5SUM_OPTIONS {} + > $OUTPUT # Calculate MD5 sums for files in current directory and subdirectories excluding $OUTPUT file and save result in $OUTPUT file
fi
cd - > /dev/null # change to previous directory
else
cd $DIR # if $DIR doesn't exists, change to it to generate localized error message
fi
fi
}
跑完之后source ~/.bashrc
, 您可以使用md5sums
像普通命令一样:
md5sums path/to/dir
将生成checksums.md5
文件输入path/to/dir
目录,包含该目录及子目录下所有文件的MD5和。使用:
md5sums -c path/to/dir
检查金额path/to/dir/checksums.md5
file.
注意path/to/dir
可以是相对的或绝对的,md5sums
无论哪种方式都会工作得很好。结果checksums.md5
文件始终包含相对于的路径path/to/dir
。
您可以使用与默认不同的文件名checksums.md5
通过提供-o
or --output
选项。所有选项,除此之外-c
, --check
, -o
and --output
被传递给md5sum
.
上半场md5sums
函数定义负责解析选项。看这个答案 https://stackoverflow.com/a/14203146/6231376了解更多相关信息。后半部分包含解释性评论。