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:

Suggested articles

11 Comment(s)

Add comment

Comments

By:

Excellent tip, thanks!

By: TeigneuX

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

By:

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

By: Joao Baptista

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

 

By: thirupathy

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 "

By:

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

Keep up the great work!!!!

By: Ojee2

Very nice!

Works on Ubuntu/Debian too!

By: Anonymous

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

 

 

By: Alain Ferriol

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

By: Anonymous

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


By: lxvi

Why put in a timestamp that isn't sortable??? Just use "%F %T" and be done with it!