Need to find the time difference between the file tmp_071508 cretaed and the current time (92 Views)
Reply
Valued Contributor
Manish Pandey
Posts: 104
Registered: ‎03-06-2007
Message 1 of 4 (92 Views)

Need to find the time difference between the file tmp_071508 cretaed and the current time

I has create a script, which will create the file with name tmp_($date +%H%M%S).I want to create the script to know the time difference, when the file was created and the current time.
"Plans Never Fails, People Fails To Plan"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 4 (92 Views)

Re: Need to find the time difference between the file tmp_071508 cretaed and the current time

Hi Manish:

# perl -le 'die unless -f $ARGV[0];print +(time-(stat($ARGV[0]))[9])," seconds ago"' file

# touch /tmp/me
# perl -le 'die unless -f $ARGV[0];print +(time-(stat($ARGV[0]))[9])," seconds ago"' /tmp/me
2 seconds ago

Regards!

...JRF...
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,585
Registered: ‎05-19-2003
Message 3 of 4 (92 Views)

Re: Need to find the time difference between the file tmp_071508 cretaed and the current time


The solution JRF proposes is probably fine.
But pleae realize that it relies on stat[9] = mtime = "last modify time in seconds since the epoch, " to be close to the creation time (which Unix does not record).

It does NOT use the time in the name.
So this only works fine for marker file which is created and not touched anymore.

A more exact, but much more tedious, solution would be to take the name apart into time component, stuff in time array and get the seconds since epoch.

For example:

#---- test.pl ------
$_=shift;
m/(\d+)(\d\d)(\d\d)$/;
$then = $3 + $2*60 + $1*3600;
@now=localtime;
$now = $now[0] + $now[1]*60 + $now[2]*3600;
print $now - $then, " Seconds\n";
#-------------------

Or a little cleaner still

#---- test.pl ------
use Time::Local;

$now=time;
$_=shift;
m/(\d+)(\d\d)(\d\d)$/;
@then=localtime($now);
@then[0..2]=($3,$2,$1);
$then=timelocal(@then);
print $now - $then, " Seconds\n";
#------------------------

$ cat > tmp_$(date +%H%M%S)
test
$ sleep 10
$ ls tmp_*
tmp_142711
$ perl test.pl tmp_142711
11 Seconds

Of course, the design of the file name will cause this only to work 'today'.
If you ever need to cross a day boundary, then you may want to add something like

$elapsed = $now - $then;
$elapsed +=86400 if $elapsed < 0;

hth,
Hein.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 4 (92 Views)

Re: Need to find the time difference between the file tmp_071508 cretaed and the current time

Hi (again) Manish:

We could also invoke the Date::Calc module to do:

# cat .fileage
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw( Delta_DHMS );
die "Usage: $0 file\n" unless @ARGV && -f $ARGV[0];
my $file = shift;
my ( $fsec, $fmin, $fhrs, $fday, $fmon, $fyrs )
= ( localtime( ( stat($file) )[9] ) )[ 0 .. 5 ];
my ( $csec, $cmin, $chrs, $cday, $cmon, $cyrs ) = (localtime)[ 0 .. 5 ];
$fmon++;
$fyrs += 1900;
$cmon++;
$cyrs += 1900;
my ( $Dd, $Dh, $Dm, $Ds ) = Delta_DHMS(
$fyrs, $fmon, $fday, $fhrs, $fmin, $fsec,
$cyrs, $cmon, $cday, $chrs, $cmin, $csec
);
printf "'%s' is %d day%s %d hour%s %d minute%s %d second%s old\n",
$file, $Dd, $Dd == 1 ? "" : "s", $Dh, $Dh == 1 ? "" : "s", $Dm,
$Dm == 1 ? "" : "s", $Ds, $Ds == 1 ? "" : "s";
1;


# ls -l /stand/vmunix
-rwxr-xr-x 1 root sys 18949752 Mar 3 15:16 /stand/vmunix
# ./fileage /stand/vmunix
'/stand/vmunix' is 133 days 2 hours 15 minutes 14 seconds old

Regards!

...JRF...

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation