Checking the time of last file received?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking the time of last file received?
# 8  
Old 05-30-2008
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

Last edited by Dhruva; 06-02-2008 at 03:17 AM..
# 9  
Old 05-30-2008
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
# 10  
Old 05-30-2008
Just a basic solution
Code:
a=`find /Archive/Files/h/ -name *.dat -type f -ctime -1`
test -z $a && echo NOK #do something here...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find directory is getting files in every 10 mins, if not then when last time file received

Dears, I am looking for a script which will work as a watch directory. I ha directory which keep getting files in every 10 mins and some time delay. I want to monitor if the directory getting the files in every 10 mins if not captured the last received file time and calculate the delay. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

3. Shell Programming and Scripting

sh script for file received confirmation

I am new at scripting, (and this fourm- so hi guys!) and I was wondering is there something out there that I can use to do the following: I receive a file on an FTP server I would like a script to look at a specifc directory for a certain file and when it arrives it will send an email... (8 Replies)
Discussion started by: mamemlin
8 Replies

4. Shell Programming and Scripting

Script check for file, alert if not there, and continue checking until file arrives

All, Is there a way to keep checking for a file over and over again in the same script for an interval of time? Ie If { mail -user continue checking until file arrives file arrives tasks exit I don't want the script to run each time and email the user each time a file... (4 Replies)
Discussion started by: markdjones82
4 Replies

5. Programming

unexpected values received when writing and reading from file

In the code below i try to write and read from a file, but i get unexpected results, like after writing i cannot open the file, and when reading the file the value entered earlier is not shown bool creat_fragments(int nFragment) { int fd, rand_value; char frag_path, buf; for(int... (8 Replies)
Discussion started by: saman_glorious
8 Replies

6. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

7. Shell Programming and Scripting

how to grep the time of the mail received?

Hi all, My question is how can we grep the time of the mail which we receive in our inbox? like if i get a mail tonight at 10 i should be able to grep the time of it tomorrow morning or someother day in 24 hr format.... how can we do that? (2 Replies)
Discussion started by: smarty86
2 Replies

8. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

9. UNIX for Dummies Questions & Answers

checking time stamp

Hi, I am having a script in which I am again calling a script, but before calling that script I need to perform a time check (say 1 - 2 am i.e. I would be able to call that script if time is between 1:00 am and 2:00 am) but this time stamp needs to be configurable. can anybody suggest me how... (7 Replies)
Discussion started by: Manvar Khan
7 Replies

10. UNIX for Dummies Questions & Answers

Checking modified time of files

My problem is with the find command. After looking through the forum I've got - find . -mtime 2 -name "*" which gives me a list of all the files modified in the last 2 days. How do I change this to list files modified in the last 2 hours? Sorry if this question is already on the forum... (4 Replies)
Discussion started by: am97395331
4 Replies
Login or Register to Ask a Question