How to Enable Timestamp in Linux History Command Output

If you’re a command-line enthusiast, you probably use the history command regularly to recall past commands. It’s a handy feature that allows you to review your actions, streamline your workflow, and even troubleshoot if needed. However, there’s a way to supercharge your history logs by enabling timestamps. By default, history command stores last one thousand commands in their output.

In this blog post, we’ll walk you through the process of enabling timestamps in the history command, giving you a more comprehensive and organized view of your command-line journey.

Why Timestamps Matter:

By default, the history command in Linux & Unix only displays the list of commands with line numbers, making it challenging to determine when you executed each command. Enabling timestamps in your history logs can significantly enhance your productivity and help you gain insights into how you spend your time on the terminal. Timestamps offer a clearer perspective on your workflow, assisting you in tracking when specific tasks were performed, and identifying potential bottlenecks.

Enabling Timestamp in History Command

Sample Output of History Command,

# history

History-command-output-linux

As we can see in above history command output what commands were executed but issue is here that we do not have timestamp in output. So, we can’t say anything about the timings of command execution.

So, to enable timestamp in history command output, we must configure “HISTTIMEFORMATshell variable.

Execute following command to configure this variable

# export HISTTIMEFORMAT="%F %T "

Where,

  • %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 execute the history command again and verify whether we can see date and time in front of each command,

# history

TimeStamp-History-Command-Linux

That’s it we have successfully enable timestamp in history command. To make “HISTTIMEFORMAT” variable persistent across the reboot, configure it permanently in ~.bashrc file , append the following code at end of file

# vi ~/.bashrc
…………
export HISTTIMEFORMAT="%F %T "
…………

Save and exit the file.

To make changes of bashrc file into the effect immediately, execute beneath command,

# source  ~/.bashrc

In case if you wish to remove timestamp from history command then remove the line which contains “export HISTTIMEFORMAT=”%F %T ” from ~/.bashrc file

If you want to enable timestamp in history command for all local users too, then define the variable HISTTIMEFORMAT in /etc/profile file instead of root user’s ~/.bashrc file.

 # vi /etc/profile
……………
export HISTTIMEFORMAT="%F %T "
…………

Save and exit the file. To make above changes into the effect , source it.

# source  /etc/profile

Now run history command via local user and see whether date and time is visible in front of each command.

[root@mail ~]# su - linuxtechi
[linuxtechi@mail ~]$ history
    1  2019-10-28 01:07:46 exit
    2  2019-10-28 01:07:46 exit
    3  2019-10-28 01:07:46 sudo vi /etc/fstab
    4  2019-10-28 01:07:46 vi /etc/ntp.conf
    5  2019-10-28 01:07:46 exit
    6  2019-10-28 01:07:46 echo $JAVA_HOME
    7  2019-10-28 01:07:46 exit
………

That’s all from this article. Please do share it among your technical friends and share your queries and feedback in below comments section.

5 thoughts on “How to Enable Timestamp in Linux History Command Output”

  1. yes, the timestamps on this are just ‘when they’re output to the command prompt. it’s not valid timestamp data

    Reply
  2. You will have to wait guys. It will show same time old history command because for those time was not taken. You give some command after interval of few mins/seconds, you will see correct time.

    Reply
  3. I see my code is always giving current time when I execute with HISTTIMEFORMAT. Could you please assist
    #!/bin/bash
    HISTFILE=~/.bash_history
    set -o history
    HISTTIMEFORMAT=”%d/%m/%y %T” history | grep -v grep | | grep “\/test\.sh -f \input.xml”| tail -n1

    Reply

Leave a Reply to Gyanendra Cancel reply