Visit Our UNIX and Linux User Community


Take action only if a file is X hours (or seconds) old


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Take action only if a file is X hours (or seconds) old
# 1  
Old 09-08-2009
Take action only if a file is X hours (or seconds) old

shell: #!/bin/ash

I searched and found a few relevant posts (here and here - both by porter, on the same day (?)) however both are just a do while loop, I need to check a file date and compare it to the current time.

I would like it to say if file 'test' is more than 12 hours old than "right now" to echo back "file is stale" else echo "file is ok". I was thinking I could get the "unix time" of the file and just subtract them, but honestly I have no idea how to get the unix time of a file (much less in a script)

concept:
Code:
touch magic

$timediff = creation time of file magic - creation time of file test (in seconds)

if $timediff > 43200 (seconds) then echo "file is stale" else "file is ok"

I'm sure there is a better/easier way to do this... Thanks all!
# 2  
Old 09-08-2009
I'm not an ash coder. However
Code:
# the last time a file was changed in epoch seconds
perl -e '@arr=stat $ARGV[0]; print "$arr[9]\n" '  filename
# now in epoch seconds
perl -e ' $now=time; print "$now\n" '
# therefore: now - filetime == age of file in seconds
perl -e '@arr=stat $ARGV[0];  $diff = time - $arr[9];  print "$diff\n" '  filename

There are less readable more compact ways to write the code, but this works for starters.

If you have GNU date on your system something similar is possible.
# 3  
Old 09-08-2009
I have gnu ls (coreutils) 5.2.1 and this works for me:

Code:
ls -l --time-style='+%s' magic | awk '{print $6}'

current time is:
Code:
date +%s

and math comparison as necessary.
# 4  
Old 09-08-2009
jim mcnamara & peterro, thank you for responding.

jim: I would like to avoid perl if possible... (this is running on a router with openwrt - low powered)

peterro: I'm using BusyBox v1.13.4, which does not recognize the --time-style for ls. Obviously, the
Code:
date +%s

works fine.

Any other suggestions?

---------- Post updated at 10:03 PM ---------- Previous update was at 06:57 PM ----------

Looking around in some unix books I found
Code:
find -printf %A_@

but unfortunately, -printf isn't recognized in the shell I'm using...

---------- Post updated at 10:39 PM ---------- Previous update was at 10:03 PM ----------

Quote:
Originally Posted by jim mcnamara
...If you have GNU date on your system something similar is possible.
Thank you so much for saying this...

Code:
date +%s -r test

Gives me exactly what I need. I wonder why GNU date is included, but ls isn't?

Oh well. Thanks for the feedback!!

Previous Thread | Next Thread
Test Your Knowledge in Computers #286
Difficulty: Easy
AIX Version 1 was introduced in 1986 for the IBM RT PC workstation, was based on HP-UX.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare file name and take action

Have some files in /tmp/dir abc.zip 123.zip 345.zip and if name matches to 345.zip then take action My code.... am i doing something wrong ? Please advise. #!/bin/bash set -x cd /tmp/dir for i in *.* do if ] then (4 Replies)
Discussion started by: abhaydas
4 Replies

2. UNIX for Beginners Questions & Answers

How to convert days hours minutes seconds to minutes?

Hi, please help with below time conversion to minutes. one column values: 2 minutes 16 seconds 420 msec 43 seconds 750 msec 0 days 3 hours 29 minutes 58 seconds 480 msec 11 seconds 150 msec I need output in minutes(total elapsed time in minutes) (2 Replies)
Discussion started by: ramu.badugula
2 Replies

3. Shell Programming and Scripting

Add or Subtract the hours,minutes or seconds in the the time variable

Hello All, I am working on script where I need to add hours,minutes or seconds in the time.Time is not the current but it could be future time.I thought I can store that time in variable and add hours.minutes or second but I am not able to add that in the time that is stores in a variable. Time... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

4. Shell Programming and Scripting

How to split numeric value into hours:minutes:seconds

I have a problem. I am working on a Call Detail Report system. Come to find out the phone switch does not report in seconds. It is a 5 digit field that reports h:mm:ss The problem is I have 1-5 digit numbers Ie 1 = 1 second and should be reported as 0:00:01 22 should be 0:00:22 321 should be... (5 Replies)
Discussion started by: truecall
5 Replies

5. Shell Programming and Scripting

Opening file and executing an action

I want the script to read the directory I am running the script from and print the contents of any file that has GX in it's title. This is the code needed. But how do I combine it? #!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); foreach $item (@list){... (4 Replies)
Discussion started by: DemonixX
4 Replies

6. Shell Programming and Scripting

Take action if a particular file appears in a directory

This is my task - pls help Write a script that will run every 5 min and check if a particular file has appeared in a particular directory. Once it appears then rename the file and move it a bkp directory and run another script. (3 Replies)
Discussion started by: mrudula009
3 Replies

7. Shell Programming and Scripting

Perform action file name written to the pipe

Hello, I have a script that monitors files uploaded via ftp. After a successful upload, the file name is written to the pipe. There is another program that reads this pipe and allows automatically run any program or script ( say test.sh ) to process the newly uploaded file. cat test.sh... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies

8. Shell Programming and Scripting

Difference in day-hours-minutes-seconds format

Hi experts, I am reading two log files and passing dates as output to a txt file. Code is given below: echo "Start Time:" >> Report.txt cat start.log | while read LINE1 do echo $DATE1 >> Report.txt done echo "End Time:" >> Report.txt cat end.log | while read LINE2 ... (7 Replies)
Discussion started by: Sreejith_VK
7 Replies

9. Shell Programming and Scripting

Clean file in single action

What one finds challenging another finds simple... (HPUX B.11.11) I have a text file named something like 12345.dst that could look like this: DOG CAT NONE TEST CAT What I want to end up with is 12345.dst looking like this: CAT DOG TEST removing "NONE" should it be there and... (1 Reply)
Discussion started by: djp
1 Replies

10. Shell Programming and Scripting

Convert minutes to hours, minutes, seconds

How would you convert lets say a 1000 minutes to hours, minutes, seconds (1 Reply)
Discussion started by: Vozx
1 Replies

Featured Tech Videos