In a directory ,files are dynamically generated.I need a script to do the following
if files are not received for more than 2 hours or if the received file is empty
then do something
How can I put that in a script.Thank you
eg. in cd /dir_name the files are like below
name date size
xyz123 20141104 05:07 578
xyz233 20141003 04:10 432
xyz567 20140907 03:15 987
file="/dir_name/xyz123"
found=`find "$file" -prune -mmin +120`
if [ -n "$found" ]; then
echo "$file is older than 2 hours
fi
Test if any file is older that 2 hours or empty:
dir="/dir_name"
found=`find "$dir" -type f \( -mmin +120 -o -size 0 \)`
if [ -n "$found" ]; then
echo "There are files older than 2 hours OR with size 0:
$found"
fi
NB size 0 is typically a new file that is just being created.
Hi,thanks for replying to my post.the above script will return files which is older than 2 hours or empty.I want a script to monitor a folder for new incoming files. if the folder has empty/incomplete file in last 2 hours then trigger something.
i can have a cron to check the folder for incoming new files
while [ 1 ]; do
if [ -e /dir_name/*.txt ];
echo "File is found"
exit 0
fi
sleep 60
but the above code would list all *.txt files when cron job is invoked.the folder is not empty.
in my folder i have files like
abc001.txt
abc002.txt
abc003.txt
------
------
But I want a script that checks my folder for incoming files.If the folder has empty/incomplete file in last 2 hours then trigger something.
You can also tweak your TZ variable. For me in the UK, TZ=GMT0BST so to go two hours back I could do this:-
ORIG_TZ=$TZ
TZ=GMT2BST
date '+%Y %m %d %H %M %S' | read year month day hours minutes seconds
TZ=$ORIG_TZ
touch -mt ${year}${month}${day}${hours}${minutes}.${seconds} /tmp/my_ref_file
ls -l /tmp/my_ref?file
find . -newer /tmp/my_ref_file -type f -name "pattern*match"
Does something like this help? Note the dot before the ${seconds} This tweak works for me on HPUX, AIX, Solaris, but not RHEL. Not sure what I'm doing wrong there, but for that, you can use the GNU date commands more like this:-
Sometimes it is good to sleep a second after creating a 'find -newer' marker file, so no more files are created/modified for that second. Computers are fast, and a second can be more than long enough.