Add a timestamp to the history command [Quick Guide]

12:05 am November 4, 201610826

I. introduction

in this article, we will see how to add a timestamp to the famous command ‘history’ under Linux .

The command ‘history’ allows to find a history of the orders entered by the current user. In general, it is very convenient in many cases.

Here is an example of the output of the command ‘history’ :


order history without timestamp

here we see something that is often lacking: the lack of timestamp. It is unclear when an order for been entered, neither the day nor the hour. It is information that we could find good useful

II. Add the time and date in the history of the orders

handling for this addition is relatively simple. Simply change the way commands are sent to the history. For this, we will change the initialization file of the shell user with the export of the following environment variable:

  export   HISTTIMEFORMAT   =  " %   F   %   T: " 

therefore add this entry in the file “ .bashrc ” to your user home. «» /root/.bashrc‘ for the user root and ‘/home//.bashrc‘ for the user ‘ “.”

Following this addition, think to relaunch a new session so that the environment variable is taken into account. You will then see the timestamp of your orders by running again the command ‘history’ :


order history with timestamp

Note that run the command ‘ export… ‘. “in the current shell will also work but the change will not be kept after a termination of your session