查找出来,如果​​文件已更改 [英] Find out if file has changed

查看:111
本文介绍了查找出来,如果​​文件已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出文件是否已被自从我的shell脚本开始,也许通过创建一个布尔什么的最后一次修改...
也许这将有可能保存最后一次脚本是在一个文本文件中运行,当脚本下次启动时就应该阅读这个文件,那么就应该找出哪些文件已被更改,这样我可以检查是否有文件已使用像变了:

I want to find out if a file has been modified since the last time my shell script was started, maybe by creating a boolean or something... Maybe it would be possible to save the last time the script was run in a text file and when the script is started the next time it should read this file and then it should find out what file have been changed so that I can check if a file has changed using something like:

for file in *
do
    #Somecode here
    if [ $filehaschanged != "0" ]; then
    echo "Foobar" > file.txt
    fi
    #Somecode here
done

也许这将有可能与来做到这一点找到 ...任何想法?

推荐答案

迈克尔,通过改变了,你问,如果该文件已经触碰(即邮戳是较新的),或者是你问的内容是不同的?

Michael, by "changed", are you asking if the file has been touched (i.e. datestamp is newer), or are you asking if the content is different?

如果是前者,你可以用测试这一发现测试。例如,在外壳:

If the former, you can test this with find or test. For example, in 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

如果您正在寻找的是什么内容的测试,那么你的操作系统可能包含的东西,将测试文件的哈希值。

If what you're looking for is a test of the contents, then your operating system probably includes something that will test the hash of a file.

[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。

I'm in FreeBSD. If you're in Linux, then you probably have "md5sum" instead of "md5".

要把它放到一个脚本,你需要通过你的文件列表走路,存储他们的哈希值,然后有一个机制来测试当前的文件反对他们的存储哈希值。这是很容易的脚本:

To put this into a script, you'd need to walk through your list of files, store their hashes, then have a mechanism to test current files against their stored hashes. This is easy enough to script:

[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中运行它反对什么是重要的,也许是 / )在predictable位置,然后写一个快速的脚本来检查对文件的散列:

You can store this (instead of /bin run it against whatever's important, perhaps /) in a predictable location, then write a quick script to check a file against the hash:

#!/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

这样的脚本是什么样的工具提供。

This kind of script is what tools like tripwire provide.

这篇关于查找出来,如果​​文件已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆