File existence

Hope someone can help me on this

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

Hi,

You can check a file exists and is greater than zero length using;

if [ -s "/path/to/filename" ]

Regards

Dave

Hi,

I want to check the file existence for last 2 hrs ie.current time-n hrs

hi dave,how can i check that the file existence with time .if file not received for more than 2 hrs ie current time - 2 hrs.

Test a certain file if older than 2 hours:

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.

OS:HP-UX

What about adapting MadeInGermany's fine proposal to fit your needs?

By mistake I had -mtime (days) instead of -mmin (minutes) in my post.
Now corrected.

---------- Post updated at 06:39 AM ---------- Previous update was at 06:30 AM ----------

The HP-UX find does not have -mmin.
For simply testing if files exist you better use a file_exists function.

That won't work if more than one file matches your pattern.

You can create a file with a timestamp of your choosing and then use the find command to select files that are newer than it.

Would that help?

Robin

Yes, use "touch: to make a mark file and "find ... -newer mark. Now, you may need a utility to give date-time 2 hours ago. I wrote a utility much like GNU date, one solution, but tailored to do any to any +- any time: tm2tm, whose source in in here: http://www.unix.com/shell-programming-and-scripting/146216-date-difference-between-freebsd-linux.html

You can get GNU date here: HP-UX Porting and Archiving Centre | sh_utils-2.0

You can get GNU find here: HP-UX Porting and Archiving Centre | findutils-4.4.2

Thanks for the reply .I couldnt use -mmin along with find in HP-UX

find: bad option -mmin

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:-

date --date='2 hours ago' '+%Y %m %d %H %M %S'

I hope that this helps,
Robin

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.