迈克尔,通过“更改”,您是在询问文件是否已被触及(即日期戳较新),还是在询问内容是否不同?
如果是前者,您可以使用以下命令进行测试find
or test
。例如,在 shell 中:
#!/bin/sh
touch file1
sleep 1
touch file2
if [ "file1" -nt "file2" ]; then
echo "This will never be seen."
else
echo "Sure enough, file1 is older."
fi
如果您正在寻找的是内容测试,那么您的操作系统可能包含可以测试文件哈希的内容。
[ghoti@pc ~]$ date > testfile
[ghoti@pc ~]$ md5 testfile
MD5 (testfile) = 1b2faf8be02641f37e6d87b15444417d
[ghoti@pc ~]$ cksum testfile
3778116869 29 testfile
[ghoti@pc ~]$ sha1 testfile
SHA1 (testfile) = 5f4076a3828bc23a050be4867549996180c2a09a
[ghoti@pc ~]$ sha256 testfile
SHA256 (testfile) = f083afc28880319bc31417c08344d6160356d0f449f572e78b343772dcaa72aa
[ghoti@pc ~]$
我在 FreeBSD 中。如果您使用的是 Linux,那么您可能有“md5sum”而不是“md5”。
要将其放入脚本中,您需要遍历文件列表,存储它们的哈希值,然后使用一种机制来根据其存储的哈希值测试当前文件。这很容易编写脚本:
[ghoti@pc ~]$ find /bin -type f -exec md5 {} \; > /tmp/md5list
[ghoti@pc ~]$ head -5 /tmp/md5list
MD5 (/bin/uuidgen) = 5aa7621056ee5e7f1fe26d8abb750e7a
MD5 (/bin/pax) = 7baf4514814f79c1ff6e5195daadc1fe
MD5 (/bin/cat) = f1401b32ed46802735769ec99963a322
MD5 (/bin/echo) = 5a06125f527c7896806fc3e1f6f9f334
MD5 (/bin/rcp) = 84d96f7e196c10692d5598a06968b0a5
您可以存储它(而不是 /bin 针对重要的内容运行它,也许/
)在可预测的位置,然后编写一个快速脚本来根据哈希检查文件:
#!/bin/sh
sumfile=/tmp/md5list
if [ -z "$1" -o ! -f "$1" ]; then
echo "I need a file."
exit 1
elif ! grep -q "($1)" $sumfile; then
echo "ERROR: Unknown file: $1."
exit 1
fi
newsum="`md5 $1`"
if grep -q "$newsum" $sumfile; then
echo "$1 matches"
else
echo "$1 IS MODIFIED"
fi
这种脚本就是类似的工具tripwire http://sourceforge.net/projects/tripwire/提供。