The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get date and time for past 1 hour from current date spch2o Shell Programming and Scripting 5 10 Hours Ago 01:32 AM
Perl: Extracting date from file name and comparing with current date MKNENI Shell Programming and Scripting 4 03-26-2008 12:01 PM
calculate the date of next satureday of current date. rinku Shell Programming and Scripting 1 02-27-2008 04:40 PM
check the current date file in directory pallvi SUN Solaris 2 01-04-2008 01:57 PM
how to give current date in file name? simurg11 UNIX for Dummies Questions & Answers 3 05-10-2005 06:09 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-04-2007
Registered User
 

Join Date: May 2007
Posts: 1
File date vs Current date

I am a newbie in shell scripting, hope you can advice how can I compare the date/time of file extracted from 'll' and current system date/time.

I have done the following:

ll -rt > $FILE_AGE_LOG
FILE_DATETIME=`more $FILE_AGE_LOG | head -02 | cut -c 45-57`

It returns 'May 4 19:11'.

If I want to put in a condition to check if the file is more than 15 minutes old, something like the following.

if [ $FILE_DATETIME < (`date`-15) ]

Please kindly advice how can I do that?
Thanks.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-04-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Trexlim,
A true date calculation can be very involved as you have to consider leap years.
One way to solve your problem is:
1) One time only, create a "15_minutes_file".
2) Create a cron job that runs every 15 minutes.
2.a) Test if the file is older than 15_minutes_file -- "if file -ot 15_minutes_file"
2.a.I) Meets your criteria.
2.b) Update (touch) the "15_minutes_file" file with the current date.
See if this works for you.
Reply With Quote
  #3 (permalink)  
Old 05-04-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,460
try something like this:
Code:
#!/bin/ksh
# tdiff.sh 


tdiff()
{
perl -e '
   ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
   $atime,$mtime,$ctime,$blksize,$blocks) = stat("$ARGV[0]");
   $difference = time - $mtime;
   print $difference;
   ' "$1"
}

secs=$(tdiff "$1")
minutes=$secs/60
if [[ $minutes -le 15 ]] ; then
    echo "$1 is 15 minutes old or less"
else
    echo "$1 is older than 15 minutes"    
fi
Reply With Quote
  #4 (permalink)  
Old 05-04-2007
jgt jgt is offline
Registered User
 

Join Date: Apr 2007
Location: 44.21.48N 80.50.15W
Posts: 348
Following on with Shell Life

You could after touching the 'timestamp' file, do
'ls -ltr' and see what files are listed after the datestamp file.
Note that the granularity of the list may only be to the nearest minute.
Reply With Quote
  #5 (permalink)  
Old 05-04-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,223
get a file date/timestamp
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:06 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0