Neat things with the date command

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 thought on “Neat things with the date command

  1. Stephen Samuel

    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`

Comments are closed.