To get the previous 5 minutes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To get the previous 5 minutes
# 1  
Old 05-16-2012
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
# 2  
Old 05-16-2012
One way using a perl one-liner:
Code:
$ 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

# 3  
Old 05-16-2012
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.
# 4  
Old 05-17-2012
Sorry not to mention those details. I am using Redhat linux and i required it on shell script(bash)
# 5  
Old 05-17-2012
Try: bash + GNU date (slow):
Code:
for i in {1..5}; do
  date -d "$i minutes ago" +%d-%m-%Y_%H-%M
done

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

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file creation Time minutes and if file older then 5 minutes execute some stuff

Hello all, Info: System RedHat 7.5 I need to create a script that based on the creation time, if the file is older then 5 minutes then execute some stuff, if not exit. I thought to get the creation time and minutes like this. CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print... (3 Replies)
Discussion started by: charli1
3 Replies

2. Shell Programming and Scripting

Grep a log file for the last 5 minutes of contents every 5 minutes

Hi all, System Ubuntu 16.04.3 LTS i have the following log INFO 2019-02-07 15:13:31,099 module.py:700] default: "POST /join/8550614e-3e94-4fa5-9ab2-135eefa69c1b HTTP/1.0" 500 2042 INFO 2019-02-07 15:13:31,569 module.py:700] default: "POST /join/6cb9c452-dcb1-45f3-bcca-e33f5d450105... (15 Replies)
Discussion started by: charli1
15 Replies

3. UNIX for Beginners Questions & Answers

How to convert days hours minutes seconds to minutes?

Hi, please help with below time conversion to minutes. one column values: 2 minutes 16 seconds 420 msec 43 seconds 750 msec 0 days 3 hours 29 minutes 58 seconds 480 msec 11 seconds 150 msec I need output in minutes(total elapsed time in minutes) (2 Replies)
Discussion started by: ramu.badugula
2 Replies

4. UNIX for Dummies Questions & Answers

Minus 5 minutes

Hi, I need to subtract 5 minutes from the date. Example $date +"%Y-%m-%d-%H.%M.%S" displays 2014-06-26-06.06.38 I want to show it as 2014-06-26-06.01.38 (5 mins are subtracted from date) Any help would be appreciated. I am currently on AIX version 6.1 -Vrushank (10 Replies)
Discussion started by: vrupatel
10 Replies

5. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

7. Shell Programming and Scripting

Determine previous time in minutes

I have several logs with where the time stamp in the logs are "YYYYMMDDHHMM". I would like to check the last line in each file to make sure the entry is less than 5 minutes old. My timezone is EST5EDT so the following will work for 1 hour. But I need something easy for 5 minutes ago.... (5 Replies)
Discussion started by: oldman2
5 Replies

8. Shell Programming and Scripting

ls every 5 minutes

I have to check for a directory content: every time it is filled with a file, I have to launch a bash script. So: how to do an ls every 5 minutes (avoiding cron), telling the script to start if the file has appeared in the dir? Have you got better ideas? Thanks in advance! (5 Replies)
Discussion started by: canduc17
5 Replies

9. Shell Programming and Scripting

Convert minutes to hours, minutes, seconds

How would you convert lets say a 1000 minutes to hours, minutes, seconds (1 Reply)
Discussion started by: Vozx
1 Replies
Login or Register to Ask a Question