Touch the file to future time stamp

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

Asked By: Aravind


Use that command:

touch -t "$( printf '%(%Y%m%d%H%M.%S)Tn' '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.

Answered By: chaos

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.

For the GNU’s touch pilgrims stumbling upon this question:

You can also use the -d parameter of touch.

touch -d "now +30 minutes" $HOME/reference
Answered By: StaNov
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.