how to test if a file is more than one hour old in ksh

I am looking for a simple way to test if a file is more than one hour
old in ksh. I think 'find' command may be able to do that but I do not
know the detail. Any help? Thanks.
SamL [ Sa, 19 April 2008 04:40 ] [ ID #1945592 ]

Re: how to test if a file is more than one hour old in ksh

On 4/18/2008 9:40 PM, SamL wrote:
> I am looking for a simple way to test if a file is more than one hour
> old in ksh. I think 'find' command may be able to do that but I do not
> know the detail. Any help? Thanks.
>

If by "old" you mean how long has it been since it was created, you can't
(unless you save the information yourself) as UNIX doesn't store file creation
time. If you mean how long since it was last modified, what you probably are
looking for is:

find . -maxdepth 1 -ctime +1 -name file

Regards,

Ed
Ed Morton [ Sa, 19 April 2008 04:52 ] [ ID #1945593 ]

Re: how to test if a file is more than one hour old in ksh

Ed Morton wrote:
> On 4/18/2008 9:40 PM, SamL wrote:
>> I am looking for a simple way to test if a file is more than one hour
>> old in ksh. I think 'find' command may be able to do that but I do
>> not know the detail. Any help? Thanks.
>>
>
> If by "old" you mean how long has it been since it was created, you
> can't (unless you save the information yourself) as UNIX doesn't
> store file creation time. If you mean how long since it was last
> modified, what you probably are looking for is:
>
> find . -maxdepth 1 -ctime +1 -name file
But that's days, not hours.

Bye, Jojo
Joachim Schmitz [ Sa, 19 April 2008 11:49 ] [ ID #1945595 ]

Re: how to test if a file is more than one hour old in ksh

On Saturday 19 April 2008 11:49, Joachim Schmitz wrote:

>> If by "old" you mean how long has it been since it was created, you
>> can't (unless you save the information yourself) as UNIX doesn't
>> store file creation time. If you mean how long since it was last
>> modified, what you probably are looking for is:
>>
>> find . -maxdepth 1 -ctime +1 -name file
> But that's days, not hours.

Then I guess we need some math:

if [ $(( $(date +%s) - $(stat -c %Z file) )) -gt 3600 ]; then
....
fi

(or %Y instead of %Z if we want the modification time)

Or, with GNU find (adapted from Ed's):

find . -maxdepth 1 -cmin +60 -name file

(or -mmin if we want the modification time), and test if the command outputs
something.

All the above solutions need GNU tools. Not sure whether standard methods
exist for doing the same thing.

--
D.
Dave B [ Sa, 19 April 2008 12:18 ] [ ID #1945596 ]

Re: how to test if a file is more than one hour old in ksh

"Ed Morton" <morton [at] lsupcaemnt.com> wrote in message
news:48095E74.1040509 [at] lsupcaemnt.com...
> On 4/18/2008 9:40 PM, SamL wrote:
>> I am looking for a simple way to test if a file is more than one hour
>> old in ksh. I think 'find' command may be able to do that but I do not
>> know the detail. Any help? Thanks.
>>
>
> If by "old" you mean how long has it been since it was created, you can't
> (unless you save the information yourself) as UNIX doesn't store file
> creation
> time. If you mean how long since it was last modified, what you probably
> are
> looking for is:
>
> find . -maxdepth 1 -ctime +1 -name file

You demonstrate a common misconception about find - that the format is

find dir -switches ...

the true format is

find files ... -switches

You can have multiple files and the files can be of any type - remember, a
directory
is just a particular type of file

However, the the time values in find are days not hours.
You could always use touch to touch a file with a back date - that's kind of
kludgy.

touch -t CCYYMMDDhhmm /tmp/touchstone
if [[ $file -ot /tmp/touchstone ]]

You could use perl

if perl -e 'exit(!(-M "/path/to/file") > 1.0'
then
echo older
else
echo not older
fi



>
> Regards,
>
> Ed
>
Dan Mercer [ Sa, 19 April 2008 20:25 ] [ ID #1945599 ]

Re: how to test if a file is more than one hour old in ksh

For a given file:

[ $(($(date +%s)-$(date -r file +%s))) -ge 3600 ]&&echo old



SamL wrote:
> I am looking for a simple way to test if a file is more than one hour
> old in ksh. I think 'find' command may be able to do that but I do not
> know the detail. Any help? Thanks.
mop2 [ Sa, 19 April 2008 23:40 ] [ ID #1945605 ]

Re: how to test if a file is more than one hour old in ksh

On Apr 19, 4:40 pm, mop2 <mop2bky4mz5tyjwa8ersp7hrg5u... [at] gmail.com>
wrote:
> For a given file:
>
> [ $(($(date +%s)-$(date -r file +%s))) -ge 3600 ]&&echo old
>
> SamL wrote:
> > I am looking for a simple way to test if a file is more than one hour
> > old in ksh. I think 'find' command may be able to do that but I do not
> > know the detail. Any help? Thanks.

Depending where the files exist. My manual says 'lynx' will open
local files.
See if information in this script snippet will help.

LDPTIME=$(date -d "`lynx -dump http://www.tldp.org/timestamp.txt`" +
%s)
TIMESTAMP=$(date -d "`lynx -error_file=lynx_error -dump -
connect_timeout $url/timestamp.txt`" +%s)

if [ "$TIMESTAMP" == "" ] ; then
continue
fi
TIMEFRAME=$(($LDPTIME - $TIMESTAMP))
printf "Time frame is %s minus %s equal %s\n" "$LDPTIME"
"$TIMESTAMP" "$TIMEFRAME"
if [ "$TIMEFRAME" -gt 1209600 ] ; then
Dave Kelly [ So, 20 April 2008 01:01 ] [ ID #1946125 ]

Re: how to test if a file is more than one hour old in ksh

On Apr 19, 6:18 am, Dave B <da... [at] addr.invalid> wrote:
> On Saturday 19 April 2008 11:49, Joachim Schmitz wrote:
>
> >> If by "old" you mean how long has it been since it was created, you
> >> can't (unless you save the information yourself) as UNIX doesn't
> >> store file creation time. If you mean how long since it was last
> >> modified, what you probably are looking for is:
>
> >> find . -maxdepth 1 -ctime +1 -name file
> > But that's days, not hours.
>
> Then I guess we need some math:
>
> if [ $(( $(date +%s) - $(stat -c %Z file) )) -gt 3600 ]; then
> ...
> fi
>
> (or %Y instead of %Z if we want the modification time)
>
> Or, with GNU find (adapted from Ed's):
>
> find . -maxdepth 1 -cmin +60 -name file
>
> (or -mmin if we want the modification time), and test if the command outputs
> something.
>
> All the above solutions need GNU tools. Not sure whether standard methods
> exist for doing the same thing.
>
> --
> D.

I am using AIX and unfortunately do not have the stat command, so I
tried the find command approach.

It essentially works. Only one thing, it will return 0 even if there
is no files found satisfying the criteria. So

find . -maxdepth 1 -cmin +60 -name file >/tmp.$$
[[ -s /tmp.$$ ]] && echo "old"

Thanks to all those who replied.
SamL [ So, 20 April 2008 04:46 ] [ ID #1946129 ]

Re: how to test if a file is more than one hour old in ksh

2008-04-18, 19:40(-07), SamL:
> I am looking for a simple way to test if a file is more than one hour
> old in ksh. I think 'find' command may be able to do that but I do not
> know the detail. Any help? Thanks.


If you're implementation of ksh is zsh's, *(mh+1) expands to the
list of files that are more than 1 hour old.

--
Stéphane
Stephane CHAZELAS [ So, 20 April 2008 11:26 ] [ ID #1946132 ]

Re: how to test if a file is more than one hour old in ksh

On Saturday 19 April 2008 20:25, Dan Mercer wrote:

>> time. If you mean how long since it was last modified, what you probably
>> are looking for is:
>>
>> find . -maxdepth 1 -ctime +1 -name file
>
> You demonstrate a common misconception about find - that the format is
>
> find dir -switches ...
>
> the true format is
>
> find files ... -switches

Where did you read that? From the standard man page:

find [ -H | -L ] path ... [ operand_expression ... ]

--
D.
Dave B [ So, 20 April 2008 12:21 ] [ ID #1946133 ]

Re: how to test if a file is more than one hour old in ksh

On Sunday 20 April 2008 12:21, Dave B wrote:

>> the true format is
>>
>> find files ... -switches
>
> Where did you read that? From the standard man page:
>
> find [ -H | -L ] path ... [ operand_expression ... ]

Ah ok, now I see what you mean. I never looked at "find" that way. Thanks!

--
D.
Dave B [ So, 20 April 2008 14:24 ] [ ID #1946135 ]

Re: how to test if a file is more than one hour old in ksh

"Dave B" <daveb [at] addr.invalid> wrote in message
news:fuf5f5$pes$1 [at] registered.motzarella.org...
> On Saturday 19 April 2008 20:25, Dan Mercer wrote:
>
>>> time. If you mean how long since it was last modified, what you probably
>>> are looking for is:
>>>
>>> find . -maxdepth 1 -ctime +1 -name file
>>
>> You demonstrate a common misconception about find - that the format is
>>
>> find dir -switches ...
>>
>> the true format is
>>
>> find files ... -switches
>
> Where did you read that? From the standard man page:
>
> find [ -H | -L ] path ... [ operand_expression ... ]

find -H | -L are not standard find options - they may be Gnu find options,
but I've never seen them.
The synopsis for the standard find is

find path-name-list [expression]

Gnu find is a bit of a mess - if you're going to vary that far from the
original concept, maybe you
should reconsider renaming the program.

Dan Mercer

>
> --
> D.
>
Dan Mercer [ So, 20 April 2008 18:33 ] [ ID #1946142 ]

Re: how to test if a file is more than one hour old in ksh

On Sunday 20 April 2008 18:33, Dan Mercer wrote:

>> Where did you read that? From the standard man page:
>>
>> find [ -H | -L ] path ... [ operand_expression ... ]
>
> find -H | -L are not standard find options - they may be Gnu find options,
> but I've never seen them.
> The synopsis for the standard find is
>
> find path-name-list [expression]

Uhm, no. I copied/pasted that directly from the SUSv3 standard, so I suppose
that they *are* standard.

--
D.
Dave B [ So, 20 April 2008 18:40 ] [ ID #1946143 ]

Re: how to test if a file is more than one hour old in ksh

Post removed (X-No-Archive: yes)
Notifier Deamon [ So, 20 April 2008 19:24 ] [ ID #1946147 ]

Re: how to test if a file is more than one hour old in ksh

On 2008-04-20, SamL <slsamliu [at] gmail.com> wrote:
> On Apr 19, 6:18 am, Dave B <da... [at] addr.invalid> wrote:
>> On Saturday 19 April 2008 11:49, Joachim Schmitz wrote:
>>
>> >> If by "old" you mean how long has it been since it was created, you
>> >> can't (unless you save the information yourself) as UNIX doesn't
>> >> store file creation time. If you mean how long since it was last
>> >> modified, what you probably are looking for is:
>>
>> >> find . -maxdepth 1 -ctime +1 -name file
>> > But that's days, not hours.
>>
>> Then I guess we need some math:
>>
>> if [ $(( $(date +%s) - $(stat -c %Z file) )) -gt 3600 ]; then
>> ...
>> fi
>>
>> (or %Y instead of %Z if we want the modification time)
>>
>> Or, with GNU find (adapted from Ed's):
>>
>> find . -maxdepth 1 -cmin +60 -name file
>>
>> (or -mmin if we want the modification time), and test if the command outputs
>> something.
>>
>> All the above solutions need GNU tools. Not sure whether standard methods
>> exist for doing the same thing.
>>
>> --
>> D.
>
> I am using AIX and unfortunately do not have the stat command, so I
> tried the find command approach.
>
> It essentially works. Only one thing, it will return 0 even if there
> is no files found satisfying the criteria. So
>
> find . -maxdepth 1 -cmin +60 -name file >/tmp.$$
> [[ -s /tmp.$$ ]] && echo "old"
>
Look, Ma, no temp files!

[[ "$(find . -maxdepth 1 -cmin +60 -name file)" = "" ]] && echo "old"

--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities
Chris Mattern [ Mo, 21 April 2008 17:28 ] [ ID #1946486 ]

Re: how to test if a file is more than one hour old in ksh

In article <slrng0pcl9.gg3.syscjm [at] sumire.gwu.edu>,
Chris Mattern <matternc [at] comcast.net> wrote:
....
>Look, Ma, no temp files!
>
>[[ "$(find . -maxdepth 1 -cmin +60 -name file)" = "" ]] && echo "old"

Let's count the minutes until one of the standards jockeys comes along
and tells you that that requies GNU tools...
gazelle [ Mo, 21 April 2008 17:37 ] [ ID #1946487 ]

Re: how to test if a file is more than one hour old in ksh

2008-04-21, 15:37(+00), Kenny McCormack:
> In article <slrng0pcl9.gg3.syscjm [at] sumire.gwu.edu>,
> Chris Mattern <matternc [at] comcast.net> wrote:
> ...
>>Look, Ma, no temp files!
>>
>>[[ "$(find . -maxdepth 1 -cmin +60 -name file)" = "" ]] && echo "old"
>
> Let's count the minutes until one of the standards jockeys comes along
> and tells you that that requies GNU tools...

That requires GNU or FreeBSD tools.

And as you pointed out, it should have been

find file -prune -cmin +60 -print | grep -q .

Or you could have used

find file -chour +1 -exit 0 -o -exit 1

If you have the proper implementation/version of find.

--
Stéphane
Stephane CHAZELAS [ Mo, 21 April 2008 17:41 ] [ ID #1946488 ]
Linux » comp.unix.shell » how to test if a file is more than one hour old in ksh

Vorheriges Thema: access history from another shell session
Nächstes Thema: Where does the trailing '\n' come from?