To get the previous 5 minutes

Hi

I need to get the previous 5 minutes from the currnet time as given below. Please help.

Current time : date +%d-%m-%Y_%H-%M
output : 16-05-2012_15-05
I need the previous 5 minutes of the above output ,

Required output:

16-05-2012_15-04
16-05-2012_15-03
16-05-2012_15-02
16-05-2012_15-01
16-05-2012_15-00

One way using a perl one-liner:

$ perl -MPOSIX -e 'for ( 1 .. 5 ) { printf qq[%s\n], strftime q[%d-%m-%Y_%H-%M], localtime( time - $_ * 60 ) }'
16-05-2012_12-21                                                                                                                                                                                                                             
16-05-2012_12-20                                                                                                                                                                                                                             
16-05-2012_12-19                                                                                                                                                                                                                             
16-05-2012_12-18                                                                                                                                                                                                                             
16-05-2012_12-17

Please post what Operating System and version you are running and what Shell you prefer. We really need to know if you have the GNU date command.

Questions about date arithmetic are common on this forum and there is a FAQ.

It always helps to know what problem you are trying to solve when you have a problem with a solution.

Sorry not to mention those details. I am using Redhat linux and i required it on shell script(bash)

Try: bash + GNU date (slow):

for i in {1..5}; do
  date -d "$i minutes ago" +%d-%m-%Y_%H-%M
done

Or ksh (fast):

for i in {1..5}; do
  printf "%(%d-%m-%Y_%H-%M)T\n" "$i minutes ago"
done
2 Likes