Add timestamp to history command output in Linux

My last post was about adding timestamp to terminal in Linux. But isn’t it better to simply add timestamp to history command? This enables you to open your terminal anytime, run history command and find out when you ran which command, all without keeping terminal or putty windows open indefinitely. I guess it’s helpful in some cases. Here’s how to add timestamp to history command output in Linux:

Modify .bashrc file

We need to set HISTTIMEFORMAT environment variable. Add the following line to your .bashrc file.


Add timestamp to history command output in Linux - blackMORE Ops - 1

Save the file and open a new terminal, type in history and voila.

root@kali:~# history | tail -5
69 2016-10-11-21:33:48 vi .bashrc
70 2016-10-11-21:34:13 exit
71 2016-10-11-21:34:49 history | tail -5
72 2016-10-11-21:40:47 clear

Pretty neat. You can go further by adding it in the skel or default so that it affects all new accounts that are created. You can use config managers such as Puppet or Spacewalk or Chef to deploy these little snippet so that all machines in the managed network has timestamp in history. Either way, this is a nice handy snippet for any Linux Administrator to have.

Leave your solution or comment to help others.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from blackMORE Ops

Subscribe now to keep reading and get access to the full archive.

Continue reading

Privacy Policy on Cookies Usage

Some services used in this site uses cookies to tailor user experience or to show ads.