Skip to main content

sha256sum 校验信息

macOSUbuntu

Q: 我这明明好用的,他那怎么就不行,怎么办?

A: 先对比下文件信息,别只是文件不完整罢了。

发布文件,别忘了给出校验信息哟~

sha256 校验码

sha256sum <file> 输出 sha256 文件校验信息。

$ sha256sum test.txt
f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2 test.txt

$ sha256sum test.txt | cut -d ' ' -f 1
f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2

文件大小

du -h <file> 输出 "Human-readable" 文件大小信息。

$ du -h test.txt
4.0K test.txt

$ du -h test.txt | cut -f 1
4.0K

写个脚本

info.sh

#!/usr/bin/env bash

if [ $# -eq 0 ]; then
echo "Usage: ./info.sh <file or directory>"
exit 1
fi

_info() {
file="$1"
echo "file: `basename "$file"`"
echo "size: `du -h "$file" | cut -f 1`"
echo "sha256: `sha256sum "$file" | cut -d ' ' -f 1`"
echo
}

for path in "$@"; do
if [ -d "$path" ]; then
find "$path" -type f | while read -r f; do
_info "$f"
done
else
_info "$path"
fi
done

使用

$ ./info.sh test.txt
file: test.txt
size: 4.0K
sha256: f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2

写进文件:

$ file=test.txt && ./info.sh "$file" > $(basename "$file").info

$ cat test.txt.info
file: test.txt
size: 4.0K
sha256: f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2