Checking the time of last file received?

Hi,

Is there a way of looking at the last file received and checking whether it is more than 24 hours old?

E.g.
/Archive/Files/h/h0012345.dat

I need to look in a directory, get the date of the processed file and make sure it wasn't more than a day ago. If it is more than 1 day ago I need to highlight that the mechanism that is sending the files might have gone down.

Any help would be greatly appreciated.
Thanks
Ant.

Read man find

Thanks.

I've had a look and can see that -mtime, -atime etc can be used to find files for n number of days.

Do you know:

  • How to relate this to only the last file received in that directory?
  • If I put '-mtime 1' when does the 1 start from? i.e. is it the a day from the execution time or calendar day?

Thanks again
Ant.

1 or +1 is over one day, -1 is less that a day :slight_smile:
You can send the result to sort and head or tail.

U can use this as hint.

ls -lrt | tail -1 | awk '{print $6 $7 "-"$8}'

Thanks :b:

On the risk of being cheeky (infact it is cheeky) how would I link this with the mtime/atime command? :o

Cheers
Ant.

Still struggling to link these two commands together?

:confused:

Below is the script.There are steps which could have clubbed with other steps but I kept them separate for clarity.See if this script helps you.

###############################################################################
#Usage : scriptname.sh directoryname
#Purpose: this script takes last modified file from directory.Directory is passed
# as an argument while calling script and report if file is older or newer than last 24 hrs
#logic: step1 list last modified file
# step2 Take last modified time of the file by parts
# step3 convert last modified time of file and system time into seconds
# step4 take diff of two times and check for 24 hrs.
###############################################################################

#############################################
#month_to_num function takes Month name as
#argument and return index value of month
#############################################
month_to_num() {
if [ -n "$1" ]
then
t_month=$1
case $t_month in

Jan) return 01;;
Feb) return 02;;
Mar) return 03;;
Apr) return 04;;
May) return 05;;
Jun) return 06;;
Jul) return 07;;
Aug) return 08;;
Sep) return 09;;
Oct) return 10;;
Nov) return 11;;
Dec) return 12;;
*) return 0;;

esac
fi
}
#############################################

####################################
modf_time=`ls -ltr "$1" | tail -1`
##################################

#BElow commands take Hr min Yr month day individually
#retuned by long listing of last file

time_month=`echo $modf_time | awk '{print $6}'`
time_day=`echo $modf_time | awk '{print $7}'`
time_hrmin=`echo $modf_time | awk '{print $8}'`
time_hr=`echo $time_hrmin | cut -d ':' -f1`
time_min=`echo $time_hrmin | cut -d ':' -f2`
filename=`echo $modf_time | awk '{print $9}'`

#assuming system time year and file modified year are same
century=`date '+%Y'`

#converts month Name to numeric number
month_to_num $time_month
v_month_index=$?
echo "month index $v_month_index"

filedatetemp="${century}-${v_month_index}-${time_day} ${time_hr}:${time_min}"

#converts file time to seconds
filedate=`date --utc --date "$filedatetemp" +%s`

#converts system time to seconds
sysdate=`date '+%Y-%m-%d %H:%M' | date '+%s'`

diffSec=$((sysdate-filedate))

if [ $diffSec -le 0 ]
then abs=-1
else abs=1
fi

x=$((diffSec/abs))
# 24 hr in secs =24*60*60 sec
if [ $x -le 86400 ]
then
echo "File $filename is newer than 24 hrs"
else
echo "File $filename is older than 24 hrs"
fi

Hi and thanks for the script.
When I try to run the script it produces the following error:

month index 5
date: -- illegal option
Usage: date [-u] [+format]
date [-u] [-t [[CC]YYMMDDhhmm[.SS] | MMDDhhmm[YY] ]
./UNIX_SCRIPT[72]: %s: syntax error

Another question is you have mentioned that the file must be passed as an argument? At the time of doing this check I won't know the latest file and would need to check in the current direcotry fot it.

Does this script do that? (The script is way above my knowledeg of Unix)

Thanks

Just a basic solution

a=`find /Archive/Files/h/ -name *.dat -type f -ctime -1`
test -z $a && echo NOK #do something here...