posted by steve | Category: Useful Items That I Forget | | 1 Comment ยป
Okay, so the date command in Linux. How boring is this? That’s what I thought until yesterday when I was playing around with it for a script.
By now, hopefully everyone knows the runquote trick to name a logfile. Like:
log.`date +%m%d%Y`
That works great to name a logfile based on the date when you’re moving them around or simply creating them.
But I did an `info coreutils date` yesterday and saw this tasty example:
$ date –date=”$(date +%Y-%m-15) -1 month” +’Last month was %B!’
Last month was June!
Nice.
One Response to “Neat things with the date command”
Leave a Reply
Categories
- Current Projects (35)
- Linux & Open Source (6)
- Microsoft & Closed Source (3)
- Random Rants (35)
- Reviews (4)
- Useful Items That I Forget (30)
- Useful Sites That Everyone Already Knows (2)
Archives
- February 2012
- January 2012
- December 2011
- November 2011
- July 2011
- June 2011
- February 2011
- September 2010
- December 2009
- November 2009
- September 2008
- August 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- March 2007
- December 2006
- August 2006
- July 2006
- December 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- March 2005
- February 2005
- December 2004
- October 2004
- September 2004
- July 2004
I usually do things like:
timestamp=`date ‘+%y-%m-%d.%H-%M-%S’`
Then I can create a series of directories logfiles, etc with the same timestamp. If you always call date ‘live’, you run the risk of having slightly different stamps on related files.
The other nice trick is to use the ‘-r’ option to get the mtime of a file. This allows you to name a logfile based on it’s last mod time rather tan the current time.
mv logfile logfile.`date -r %y-%m-%d.%H-%M-%S`