Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to run script from a cron to check if any new files have arrived in a directory since the script last ran and mail a user. Is there an easy way to do this?
Many thanks in advance for your time.

Each time your cron job runs, it can update the date stamp on a control file which allows it to remember when it ran last. The controlfile and the output list of files should not be in the directory where you are checking for recent files because they would always end up quailifying as recent files.
Using "find -newer", we will find all files with a recent date stamp (since the last cron execution), and that could include new files or old files that have been recently modified. If you want new files only, and not old files that have already been processed but now have a recent date stamp, you will need a more sophisticated approach.
The first cron execution will not find any files because there is no control file yet, but that first execution will create a control file.
Your mail command would include /home/recentfiles in the e-mail body.
find . -type f -newer /home/laststamp > /home/recentfiles
if [ -s /home/recentfiles ] ; then
echo 'recent files found - need to send e-mail here'
else
echo 'no recent files found'
fitouch /home/laststamp
One little problem: If any new files arrive after the "find" begins and before updating the control file with the touch command, those files would get missed. Therefore, a safer approach would be:
touch /home/newstamp
find . -type f -new /home/laststamp
(process recent files, such as send e-mail)
mv /home/newstamp /home/laststamp

![]() |
awk/nawk field assignment...
|
2 NewB questions
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |