How to enable timestamp in history command

In this post, we will describe how enable timestamp in history command.

History command in Linux & UNIX systems keep tracks all command which were executed by the users in the past. By default, history command keeps the records of last 1000 commands. This feature helps sysadmins to recall the command’s syntax if it is executed in the past and also helps in troubleshooting and audit purpose.

However, in history command output, timestamp is not enabled, so to enable it we must export environment variable HISTTIMEFORMAT.

When we run history command then following output will be displayed,

$ history

Histroy-Command-Output-Linux

As per above output, it is difficult to tell at what time these commands were executed.

Enabling Timestamp in history command

To enable timestamp in history command, export the following environment variable,

$  export HISTTIMEFORMAT="%F %T "

In above variable,

  • %F –> shows Date in the format ‘YYYY-M-D’ (Year-Month-Day)
  • %T –> shows Time in the format ‘HH:MM:S’ (Hour:Minute:Seconds)

Now, re-run the history command, this time we will see timestamp added in front of each command.

Timestamp-history-command-output-linux

To export HISTTIMEFOMRAT variable permanently, add the following entry in ~.bashrc file

$ vi ~/.bashrc
-----------
export HISTTIMEFORMAT="%F %T "
----------

Save & close the file

To make the above changes into the effect, source the .bashrc file,

$ source ~/.bashrc

Source-bashrc-file-histtimeformat-linux

That’s all from this post, I hope you have found it informative. Kindly do post your queries and feedback in below comments section.

Also Read: How To Use Variables in Bash Script (Simple Guide)

Leave a Comment

8 − 4 =