Adding Date And Time To Your Bash History

This is a quick but handy addon (RedHat/CentOS) to enhance your bash history (bash > 3.0).

It comes in handy particularly if there are multiple people maintaining a given server (so you can see when a command was performed) or even if you are the sole maintainer, but can't remember exactly when you did or changed something.

Edit your /etc/bashrc and append to the bottom:

export HISTTIMEFORMAT="%h/%d - %H:%M:%S "

From next login instead of:

574  tail -f /var/log/maillog
575  mailq | tail -15
576  tail -f /var/log/maillog
577  less /var/log/maillog

you get:

1002  Apr/30 - 11:46:16 grep duncan /var/log/maillog
1003  Apr/30 - 14:17:40 passwd jduncan
1004  Apr/30 - 14:18:02 smbpasswd jduncan
1005  Apr/30 - 14:50:28 history 10

Hope this helps someone;)

Share this page:

10 Comment(s)

Add comment

Comments

From: at: 2009-05-10 05:17:16

Excellent tip, thanks!

From: TeigneuX at: 2009-05-07 15:04:32

I prefer snoopy package which writes on syslog all commands Because the .bash_history can be erased by the system

From: at: 2009-05-07 10:20:11

It works also in my Ubuntu box. The file to modify is $HOME/.bashrc

From: Joao Baptista at: 2009-05-07 11:19:52

To make it global and permanent to all users, edit /etc/profile and append to the bottom.

 

From: thirupathy at: 2009-10-29 18:36:48

HI

For me this is not working in Redhat RHEL 3 version . Though i edited the file in /etc/profile

export HISTTIMEFORMAT="%h/%d - %H:%M:%S "

From: at: 2009-05-07 09:31:25

Yes, I think your tip could be a great help. Thank a lot for this :)

Keep up the great work!!!!

From: Ojee2 at: 2009-05-07 08:28:50

Very nice!

Works on Ubuntu/Debian too!

From: Anonymous at: 2009-05-26 11:00:02

Well thank you very much, just a little nifty thing I wasn't looking for but am glad to find!!!

 

 

From: Alain Ferriol at: 2010-07-21 09:13:33

With SuSE 11.2 you should consider modify /etc/bash.bashrc.local instead.

From: Anonymous at: 2012-09-22 23:52:25

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