8 C
Thursday, January 28, 2021

How To Enable Timestamp In Bash History In Linux

Must read

You can easily find the list of recently executed commands in Linux using history command, right? Yes. But how do you know the time at which the command was executed? Of course, you can search in the log files. However, there is an easier way. You can simply enable timestamp in Bash history in Linux, so it is easier to find when was a specific command is executed in Linux.

Enable Timestamp In Bash History In Linux

Bash maintains a history of commands that have been entered in the Terminal. This list of commands is saved in a file called .bash_history in our HOME directory. Most Linux distributions remember the last 1000 commands by default. We can retrieve the recently executed commands using history command ash shown below:

$ history

Sample output:

history command output without timestamp

As you see in the above output, even though the history command displays the list of previously executed commands, it didn’t show when those commands have been executed.

To enable timestamp in Bash history in Linux, you need to set the HISTTIMEFORMAT environment variable. This variable is used to print the timestamp associated with each displayed history entry.

Run the following command to set the HISTTIMEFORMAT env variable:

$ export HISTTIMEFORMAT='%F %T '

Now, run the history command again, and you will see the timestamp before each command:

Enable timestamp in Bash history
Enable timestamp in Bash history

Perfect! Now you can easily find when a specific command is executed in your Linux system.

If you want to display the timestamps for the last “N” commands, for example 10, pipe the history command output to tail command like below:

$ history | tail -10

To make the HISTTIMEFORMAT env variable persistent across system reboots, edit the ~/.bashrc file:

$ nano ~/.bashrc

Add the following line at the end:


Press CTRL+O to save the file and press CTRL+X to exit.

Run the following command to take effect the changes immediately:

$ source ~/.bashrc

This will only display timestamp for the current user. To enable Bash history timestamp for all system users, edit /etc/profile file:

$ sudo nano /etc/profile

and add this line:


Save and close the file. To take effect the changes, run:

$ sudo source /etc/profile

For more details, refer the man pages:

$ man history

Hope this helps.

Related read:

Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

Latest article