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
Maybe it would be possible to do this with find
...any ideas?
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
ortest
. For example, in shell: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.
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:
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:This kind of script is what tools like tripwire provide.
You can
touch
all files when you run your script. Thentouch
the script itself.Next time, you just
find
any files which is newer than your script.kev's solution in Python, works if you have permissions to touch the script: