2

I have created the file which is 30 minutes old by using following command

TZ=ZZZ0 touch -t "$(TZ=ZZZ0:30 date +%Y%m%d%H%M.%S)" $HOME/reference

Similarly how can I create a file 30 minutes of future time.

Ex: at 5:00 PM I want to touch the file to be at 5:30 PM

1

Simply add a minus sign:

TZ=ZZZ0 touch -t "$(TZ=ZZZ-0:30 date +%Y%m%d%H%M.%S)" $HOME/reference

Or invert your timezone trick:

TZ=ZZZ0:30 touch -t "$(TZ=ZZZ0 date +%Y%m%d%H%M.%S)" $HOME/reference

If you need to go beyond the range of timezone offsets, standard shell utilities notoriously lack a way to manipulate date. Various people have done the job of implementing date calculations in Bourne shell, so one option is to download one of these (a few are listed in Date arithmetic in Unix shell scripts). Another option is to install GNU coreutils, specifically its date command which lets you typeset dates such as now + 30 minutes. You can also install a more sophisticated scripting environment such as Perl and use its date manipulation libraries.

0

Use that command:

touch -t "$( printf '%(%Y%m%d%H%M.%S)T\n' 'now +30 minutes' )" file

With -t of touch you can specify a date. And wih ksh's printf it is also possible to format a date. with the %(date-format)T, see the manual page of ksh.

  • I have tried this, i got an error date: 0551-402 Invalid character in date/time specification. Usage: date [-u] [+"Field Descriptors"] – Aravind Feb 11 '15 at 13:12
  • date: 0551-402 Invalid character in date/time specification. Usage: date [-u] [+"Field Descriptors"] touch: 0652-141 The specified time format is:[[[CC]YY]MMDDhhmm[.SS]]. – Aravind Feb 11 '15 at 13:17
  • @Aravind sry, it's AIX.. I changed my answer^^ – chaos Feb 11 '15 at 13:36
  • I think AIX's printf doesn't have this format. The manual doesn't distinguish between /usr/bin/printf and the ksh93 builtin though, so it's possible that some options are missing. – Gilles Feb 11 '15 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.