Create file with date minus number of seconds (678 Views)
Reply
Advisor
LITWINCZUK
Posts: 22
Registered: ‎02-17-2010
Message 1 of 9 (678 Views)

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?
Please use plain text.
Honored Contributor
Johnson Punniyalingam
Posts: 2,246
Registered: ‎09-22-2008
Message 2 of 9 (678 Views)

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&...
Problems are common to all, but attitude makes the difference
Please use plain text.
Honored Contributor
Johnson Punniyalingam
Posts: 2,246
Registered: ‎09-22-2008
Message 3 of 9 (678 Views)

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
Please use plain text.
Advisor
LITWINCZUK
Posts: 22
Registered: ‎02-17-2010
Message 4 of 9 (678 Views)

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.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (678 Views)

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...
Please use plain text.
Advisor
LITWINCZUK
Posts: 22
Registered: ‎02-17-2010
Message 6 of 9 (678 Views)

Re: Create file with date minus number of seconds

Thanks.

But there any method to do it without perl ?
Please use plain text.
Frequent Advisor
WayneHP
Posts: 54
Registered: ‎02-09-2007
Message 7 of 9 (678 Views)

Re: Create file with date minus number of seconds

Why is "touch" a requirement?
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 9 (678 Views)

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...
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,958
Registered: ‎03-06-2006
Message 9 of 9 (678 Views)

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.
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