LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-05-2011, 09:19 AM   #1
sergani
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora, CentOS, OpenSuse, Oracle Enterprise Linux, MacOSX
Posts: 27

Rep: Reputation: 0
How to use date command for last minute


Hi all,

I'm currently writing a new script for Cacti (a graphing tool) that basically counts some lines in /var/log/httpd/access_log on a CentOS 5.5 machine.

If I do this
Code:
LOG=/var/log/httpd/access_log
LINES=`grep $(date "+%d/%b/%Y:%R") $LOG | grep "GET " | wc -l`
printf "$LINES"
I'll get a number, which is the number of lines.

The problem is that
Code:
date "+%d/%b/%Y:%R"
produces
Code:
05/Oct/2011:15:17
which basically is the current date to the minute.

Is it possible to use date to print the date, to the last (not current) minute? I could do it using some math and introducing variables, but is it possible through the date command itself, or any simple workaround?

Thanks all...
//M
 
Old 10-05-2011, 09:33 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
The 'man date' command will provide much useful information. For your application:
Code:
date -d '1 minute ago' "+%d/%b/%Y:%R"
 
1 members found this post helpful.
Old 10-05-2011, 09:36 AM   #3
sergani
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora, CentOS, OpenSuse, Oracle Enterprise Linux, MacOSX
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by macemoneta View Post
The 'man date' command will provide much useful information. For your application:
Code:
date -d '1 minute ago' "+%d/%b/%Y:%R"
Awesome!

Actually i searched the man page, but couldn't find anything, not even now after your answer

Thanks a lot...
//M
 
Old 10-05-2011, 09:48 AM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Reading documentation is a skill worth taking the time to learn.

From 'man date':
Quote:
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.
From 'info date', pointed to by the above:
Quote:
28.6 Relative items in date strings
===================================

"Relative items" adjust a date (or the current date if none) forward or
backward. The effects of relative items accumulate. Here are some
examples:

1 year
1 year ago
3 years
2 days

The unit of time displacement may be selected by the string `year'
or `month' for moving by whole years or months. These are fuzzy units,
as years and months are not all of equal duration. More precise units
are `fortnight' which is worth 14 days, `week' worth 7 days, `day'
worth 24 hours, `hour' worth 60 minutes, `minute' or `min' worth 60
seconds, and `second' or `sec' worth one second. An `s' suffix on
these units is accepted and ignored.

The unit of time may be preceded by a multiplier, given as an
optionally signed number. Unsigned numbers are taken as positively
signed. No number at all implies 1 for a multiplier. Following a
relative item by the string `ago' is equivalent to preceding the unit
by a multiplier with value -1.
 
Old 10-05-2011, 09:51 AM   #5
sergani
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora, CentOS, OpenSuse, Oracle Enterprise Linux, MacOSX
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by macemoneta View Post
Reading documentation is a skill worth taking the time to learn.
Not really interested ... but thanks for the advice anyway

//M
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i get cron to run a command in 5 minute intervals baronobeefdip Linux - Software 6 09-23-2011 01:10 AM
using date command sumeet inani Linux - Newbie 9 05-06-2010 05:40 AM
date command miran_shahverdyan Linux - General 4 10-14-2009 06:59 AM
date command aetucker1 Linux - General 3 01-21-2005 12:57 PM
X-Chat script to enter a command every minute eam Programming 2 08-30-2004 03:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration