File date vs Current date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File date vs Current date
# 1  
Old 05-04-2007
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.
# 2  
Old 05-04-2007
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.
# 3  
Old 05-04-2007
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

# 4  
Old 05-04-2007
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.
# 5  
Old 05-04-2007
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

3. UNIX for Beginners Questions & Answers

“sed” replace date in text file with current date

We want to call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ Code: line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to write a... (3 Replies)
Discussion started by: pradeepp
3 Replies

4. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

5. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

6. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

7. Shell Programming and Scripting

Subtract a file's modification date with current date

SunOS -s 5.10 Generic_147440-04 sun4u sparc SUNW,SPARC-Enterprise Hi, In a folder, there are files. I have a script which reads the current date and subtract the modification date of each file. How do I achieve this? Regards, Joe (2 Replies)
Discussion started by: roshanbi
2 Replies

8. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

9. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question