Friday, October 2, 2009

How To Identify Date and Time of a Unix Command Execution

Question: How do I find out what time I executed a command previously on Unix OS? The history command doesn’t display timestamp?

Answer: Set HISTTIMEFORMAT as shown below. Please note that there is a space before the last single-quotes.

 To Display Date & Time:
$ export HISTTIMEFORMAT='%F %T ' 
After the above setting, history command will display timestamp in
front of all commands. You can also search the history as shown below. 
$ history | less
  351  2009-09-08 09:53:47 ls -lrt
  352  2009-09-08 09:53:49 cd ..
  353  2009-09-08 09:55:39 wget -mk -w 20 -P top5-1 -N
  354  2009-09-08 10:00:04 gk
  355  2009-09-08 10:00:07 cd Bash/Wget/
  356  2009-09-08 10:00:07 ls

To Display AM / PM:
$ export HISTTIMEFORMAT='%F %r '
$ history
  351  2009-09-08 09:53:47 PM ls -lrt
  352  2009-09-08 09:53:49 PM cd ..
 
To Display In DD-MM-YY time format:
$ export HISTTIMEFORMAT='%d-%b-%Y %r '
$ history
  351  08-Sep-2009 09:53:47 PM ls -lrt
  352  08-Sep-2009 09:53:49 PM cd ..


To Display Timezone:

$ export HISTTIMEFORMAT='%d-%b-%Y %r %Z'
$ history
  351  08-Sep-2009 09:53:47 PM PDT ls -lrt
  352  08-Sep-2009 09:53:49 PM PDT cd ..

1 comment:

  1. Nice, i was looking on how to add AM/PM to the timestamp. While searching i stumbled upon this.. "HISTTIMEFORMAT takes format string of strftime. Check out the strftime manual to choose and construct the timestamp that suit your taste." on another site. Here is a list of character's (%a, %A, %b, %B, %c, %C etc.) and what they can do to further modify your HISTTIMEFORMAT:

    https://www.kernel.org/doc/man-pages/online/pages/man3/strftime.3.html

    here are some more examples like the ones shown above:

    export HISTTIMEFORMAT="%a %h %d - %r "
    will display:
    457 Sat Sep 22 - 07:37:28 PM asdsa
    458 Sat Sep 22 - 07:37:29 PM history


    export HISTTIMEFORMAT="%A %h %d - %r "
    will display:
    459 Saturday Sep 22 - 07:39:53 PM sdfsad
    460 Saturday Sep 22 - 07:39:54 PM history

    export HISTTIMEFORMAT="%A %B %d - %r "
    will display:
    459 Saturday September 22 - 07:39:53 PM sdfsad
    460 Saturday September 22 - 07:39:54 PM history

    ReplyDelete

tag ur valuable ideas below