I was on a journey to get a script that would check to see if a file (with wildcard) has changed in the last minute, and if so then do something. Here are the 2 that I came up with: I changed the filenames since the real filenames were very very very long. pandora jewelry . The reason I have $DATE in them is because my process writes files with the current date hour and minute the file is written.DATE=`date +%F`FILE=`ls -t /tmp/file_$DATE*.xml | head -1`while [ $(($(date +%s) - $(stat -c %Y $FILE))) -gt 60 ]; do sleep 58 FILE=`ls -t /tmp/file_$DATE*.xml | head -1`done// do somethingandDATE=`date +%F`VALUE=0while [ $VALUE == 0 ]; do sleep 30 find /tmp/file_$DATE*.xml -mmin -1 if [ $? -eq 0 ]; then FILE=`find /tmp/file_$DATE*.xml -mmin -1 | tail -n 1` VALUE=1 fidone// do somethingThanks to the opensuse mailing list for getting me on the right path for the second script and jan from linuxquestions for getting me going with the first. couples therapy . (he showed me a new process with using stat as I was originally only going to use find). roofing company .
Script to look for file modified in the last minute while loop
12
Feb
Ben,
You should at least consider availing yourself of the capabilities of the File Alteration Monitor (FAM) () rather than rolling your own entirely from scratch. For shell scripting purposes, the “fam” package supplies the “fileschanged” command. A bit of Web searching suggests that support for FAM has been integrated into Perl and PHP (and probably other scripting languages, as well).
Randall Schulz
@Randall R Schulz
Sorry… I included a link in a bad format leading to the empty parentheses in my previous comment. It was actually a link to the Wikipedia article on FAM: http://en.wikipedia.org/wiki/File_alteration_monitor
Randall,
Thanks for pointing that out to me. I didn’t know it existed. I will be checking that out. The issue I have is the filenames are down to the hour / minute when written, so it isn’t a static file that changes (or else I could have used something like swatch, or FAM in this case (i need to read more about it to see how it functions)).
However, I think this will be useful for me.
You can use FAM to monitor directories and directory hierarchies, too, not only specific existing files.
RRS
@Randall R Schulz
the problem is, other things get written to that directory. I can’t trigger the copy on those files, it would have to be on a certain type of file, with a semi similar string (see the scripts above). I’ll look into FAM, to see what it can do. It looks quite promising.
Maybe that’ll be my next write up :)..
Have you looked into inotify at all? It looks like what you’re after is possibly easier if you let incron set off the script.