Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Create file with date minus number of seconds

Highlighted
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
Honored Contributor 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
Honored Contributor 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
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.
Acclaimed Contributor 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...
Advisor

Re: Create file with date minus number of seconds

Thanks.

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

Re: Create file with date minus number of seconds

Why is "touch" a requirement?
Acclaimed Contributor 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...
Acclaimed Contributor 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.