Shell script to find if any new entry in directory

I require a shell script to find if any new entry of dump files present in a particular directory and to send an email if any new entry exists.I had a crontab to run the script for every 5 min. Below are the file names.

dump.20150327.152407.12058630.0002.phd.gz
dump.20150327.152407.12058630.0005.phd.gz

Where are you having trouble making that script to work?
Could you post the script?

Use the find command...and post something.

I am not sure whether my way is correct or not.First i manually fill 1.txt how many files are there then it will automatically updates.by comparing i can check the new file is created or not.But the code is not working as expected..

#!/bin/sh
i=`head -1 1.txt`
echo $i
j=`ls -ltr *dump*|wc -l`
echo $j
if [ $i -gt $j ]
then
k=`ls -ltr *dump* | tail -1`
echo $k
exit 1
fi
ls -ltr *dump* | wc -l >1.txt

 

You could use find like this:

#!/bin/sh
touch 2.txt
[ -f 1.txt ] && find . -maxdepth 1 -name "*dump*" -newer 1.txt -exec ls -l {} +
mv -f 2.txt 1.txt

Note that if a new "dump" file is created after the touch 2.txt command is run and before the find command finishes this file may be listed twice as a new file.

Hi I am getting error

find: bad option -maxdepth

.How the above script will notify when the new file is created in the directory.

-maxdepth

is an extension of the GNU find. Probably your command find does not support it.

Yes -maxdepth is not supported in all find implementations, we could try the -prune option as I believe this is more widely supported.

If this is run as a cron job you will be notified via email in a similar manor to how you were notified from your ls script above, by default all output from cron jobs is mailed to the jobs owner.

#!/bin/sh
touch 2.txt
[ -f 1.txt ] && find . ! -name . -prune -name "*dump*" -newer 1.txt -exec ls -l {} +
mv -f 2.txt 1.txt

Hi,

#!/bin/sh
HOM_DIR=/full/path/to/source/dir
        i=$(head -1 $HOM_DIR/1.txt)
        j=$(ls -ltr $HOM_DIR/dump* | wc -l)
if [ $j -gt $i ]
then
        NEW=$(expr $j - $i)
        K=$(ls -ltr $HOM_DIR/dump* | nawk '{print $9}' | tail -$NEW)
        echo $K | xargs -n 1 | mailx -s "Subject" "mailaddr" -r "fromaddr"
else
        echo "0 files updated" | mailx -s "Subject" "mailaddr" -r "fromaddr"
fi
        ls -ltr $HOM_DIR/dump* | wc -l >$HOM_DIR/1.txt
exit;

Thanks

venkat

Two mistakes.

  1. You want the new number of dumps greater than the old number so it must be if [ $j -gt $i ] or if [ $i -le $j ] .
  2. If you exit then 1.txt is not updated.

---------- Post updated at 03:12 AM ---------- Previous update was at 03:08 AM ----------

Chubler's solution with find can avoid (most) duplicates by

find . ! -name . -prune -name "*dump*" -newer 1.txt \! -newer 2.txt -exec ls -l {} +