Adding Date And Time To Your Bash History

Want to support HowtoForge? Become a subscriber!
 
Submitted by linuxscooter (Contact Author) (Forums) on Thu, 2009-05-07 11:11. :: Linux

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;)


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Sun, 2012-09-23 00:52.

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


Submitted by Alain Ferriol (not registered) on Wed, 2010-07-21 10:13.
With SuSE 11.2 you should consider modify /etc/bash.bashrc.local instead.
Submitted by Anonymous (not registered) on Tue, 2009-05-26 12:00.

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

 

 

Submitted by jon (registered user) on Sun, 2009-05-10 06:17.
Excellent tip, thanks!
Submitted by TeigneuX (not registered) on Thu, 2009-05-07 16:04.
I prefer snoopy package which writes on syslog all commands Because the .bash_history can be erased by the system
Submitted by Joao Baptista (not registered) on Thu, 2009-05-07 12:19.

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

 

Submitted by thirupathy (not registered) on Thu, 2009-10-29 19:36.

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 "
Submitted by ginocic (registered user) on Thu, 2009-05-07 11:20.
It works also in my Ubuntu box. The file to modify is $HOME/.bashrc
Submitted by ginocic (registered user) on Thu, 2009-05-07 10:31.

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

Keep up the great work!!!!

Submitted by Ojee2 (not registered) on Thu, 2009-05-07 09:28.

Very nice!

Works on Ubuntu/Debian too!