Home / Command Line Interface (CLI) / Add timestamp to history command output in Linux

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.

export HISTTIMEFORMAT="%F-%T "

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
root@kali:~#

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 a Reply

Your email address will not be published. Required fields are marked *