Operating System - HP-UX
1752591 Members
3712 Online
108788 Solutions
New Discussion юеВ

Re: Create file with date minus number of seconds

 
LITWINCZUK
Advisor

Create file with date minus number of seconds

I use HPUX 11.23.
I need to create file with specified timestamp - like -1800 second backwards from now.

In linux:
touch -t `date \"+%Y%m%d%H%M\" -d@\`echo $((\\`date +%s\\`-1800))` file
will create file with time -1800 seconds. But date in hpux dont have -d@....

Any help?
8 REPLIES 8
Johnson Punniyalingam
Honored Contributor

Re: Create file with date minus number of seconds

Check below thread.

Hope this Helps .... :)

http://forums11.itrc.hp.com/service/forums/questionanswer.do?admit=109447626+1266398671869+28353475&threadId=1280246
Problems are common to all, but attitude makes the difference
Johnson Punniyalingam
Honored Contributor

Re: Create file with date minus number of seconds

If satisfies with Answers .. :)

Here is how to assign points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33
Problems are common to all, but attitude makes the difference
LITWINCZUK
Advisor

Re: Create file with date minus number of seconds

I read Your links. I know how to create file with touch.

But i need to create file like this:
touch -t aaaaaa file

where aaaaa is time minus -1800 seconds.

I neet to do this automatically.
James R. Ferguson
Acclaimed Contributor

Re: Create file with date minus number of seconds

Hi:

Perl gets you what you want:

# cat ./mycreate
#!/usr/bin/perl
use strict;
use warnings;
my $file = shift or die "filename expected\n";
my ( $fh, $at, $mt );
die "'$file' already exists\n" if -f $file;
open( $fh, ">", $file ) or die "$!\n";
close $fh;
$at = $mt = time();
utime( $at, $mt - 1800, $file );
1;

...This code prevents you from truncating an existing file. It creates a file 1,800 seconds old if one doesn't already exist.

Regards!

...JRF...
LITWINCZUK
Advisor

Re: Create file with date minus number of seconds

Thanks.

But there any method to do it without perl ?
WayneHP
Frequent Advisor

Re: Create file with date minus number of seconds

Why is "touch" a requirement?
James R. Ferguson
Acclaimed Contributor

Re: Create file with date minus number of seconds

Hi (again):

> But there any method to do it without perl ?

Why? Perl is a simple way to accomplish your goal. You could fiddle with a shell script and calculate 1800 seconds (30 minutes) less than the current date/time and build a string representation of the timestamp to use a the argument to 'touch'. To me, that's the hard way.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Create file with date minus number of seconds

>But there any method to do it without perl?

You could write a trivial C program to do it.
Or you could do complex date arithmetic in the shell that handles previous minutes, hours, days, months and years and handles leap years.

Or you could do: :-)
touch foo
sleep 1800

This works if you are going to have a cronjob that fires off every half hour. You do the touch the previous cycle.