Calculate age of a file | calculate time difference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate age of a file | calculate time difference
# 1  
Old 02-11-2011
Calculate age of a file | calculate time difference

Hello,

I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes...

I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes.

To do so, I have created two variables:
Code:
CURRTIME=`date +"%H%M"`
FILETIME=`ls -l $APSYS_DAT/currency.txt | awk '{print $8}' | awk -F: '{print $1 $2}'`

Now, I could calculate: echo $CURRTIME - $FILETIME | bc

But it doesnt calculate it as time... 1305-1255 gives me 50 and not 10 minutes.

How can I calculate with time?

Thanks for your help!

Best regards

Rolf

Last edited by Franklin52; 02-14-2011 at 05:06 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-11-2011
Hammer & Screwdriver I answered a similar question once before

This uses the stat command, which I am not sure is available in your shell. It works by converting everything to epoch time, and in this case is checking for files within 3600 seconds (60 minutes). But, perhaps playing with these commands might get you started.

https://www.unix.com/unix-dummies-que...e-60-mins.html
# 3  
Old 02-11-2011
Hello Joeyg,

Thanks for the hint to use epoch time.

It was a little bit tricky to find the current epoch time on solaris, as
date +%s doesn't exist on solaris.

But the following line gives me the current epoch time:
Code:
truss date 2>&1 | grep time | awk '{print $3}'

Now I just need to find out, how I can print the epoch time, when the file was created.

Unfortunately, the stat command, you used in your script, doesn't exist on solaris.

Any ideas, how I could find the epoch time of the file creation?

Thanks and best regards

Rolf

Last edited by Franklin52; 02-14-2011 at 05:06 AM.. Reason: Please use code tags
# 4  
Old 02-11-2011
I seem to remember using the stat command, under bash, while on a Solaris machine at an earlier job. Hmmm....
Going to have to think a bit.

Perhaps others can offer a suggestion?
# 5  
Old 02-11-2011
Will you be running your monitoring script from "cron"? If so, how frequently and during what hours and on what days of the week?


It is easy to write a script to run from cron which maintains a reference file with a timestamp 20 mins old but remember that if run once a minute such a cron would run 1440 times a day. If you really don't need to be that accurate, could you check every 10 mins or more?
# 6  
Old 02-11-2011
Quote:
Originally Posted by worm
Now I just need to find out, how I can print the epoch time, when the file was created.

Any ideas, how I could find the epoch time of the file creation?
I don't believe *nix systems store 'creation' time. Only changed, modified, and accessed.
# 7  
Old 02-11-2011
AlphaLexman is correct. The "last modified" timestamp is the most useful and is the one used by "ls -la" and "find -mtime" etc..
The "-ctime" timestamp is the timestamp when the inode was last changed. Some backup software changes this timestamp to mark the file "backed up".

Earlier I was trying to avoid the epoch date arithmetic or writing a "C" or "perl" (or whatever) program.

There are many techniques to find files which are less than 20 minutes old even if you don't have the GNU version of "find" - which offers this sort of search as standard.

Let's find out what you are trying to do in more detail.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate time difference between two lines

i grepped the time stamp in a file as given below now i need to calculate time difference file data: 18:29:10 22:15:50 (5 Replies)
Discussion started by: vivekn
5 Replies

2. Shell Programming and Scripting

Calculate time difference

I have time in a file in HH:MM:SS format as it contents(its not the file creation time). i need this to be converted to epoch time or time since 1970. The time is written into that file by a script, which i cannot modify. Im using AIX machine $ cat abc.txt 10:29:34 (2 Replies)
Discussion started by: gpk_newbie
2 Replies

3. Shell Programming and Scripting

Calculate time stamp difference

Hi All, I am new to shell scripting.I have to write a shell script for the problem statement: "A file is updated continously. If it is not updated for a day then an error message needs to pop up." So the script needs to read the last modified time of that file and current system time .If... (4 Replies)
Discussion started by: bharath.phatak
4 Replies

4. Shell Programming and Scripting

Calculate the Time stamp difference

hi, I have a log file which gives time stamps hh:mm:ss.sssss format in which hh- hours , mm -minutes ss.sssss - seconds.microseconds I need to calculate the time diff between sent time stamp and received time stamp .... could any one please help me.. i am tryin to write a script but... (2 Replies)
Discussion started by: firestar
2 Replies

5. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

6. Shell Programming and Scripting

Formatting output so I can calculate time difference between two stamps

I know there have been a million questions regarding calculating time stamps, and with enough googling, I think I'm almost there (I'm going to use the changing the times into seconds and subtracting solution). My problem is that I'm not sure how to format my log file to get the info I need. Below... (0 Replies)
Discussion started by: DeCoTwc
0 Replies

7. Shell Programming and Scripting

Calculate the time difference between a local file and a remote file.

I m stuck with a issue. I need to calculate the time difference between two files.. one on the local machine and one on the remote machine using a script. Can any one suggest the way this can be achevied Thanks, manohar (1 Reply)
Discussion started by: meetmano143
1 Replies

8. Shell Programming and Scripting

How to calculate the time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies

10. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies
Login or Register to Ask a Question