šŸ§ How to enable timestamp in Bash history on Linux


You can easily find a list of recently executed commands in Linux using the history command, right? But how do you know when a command is running? You can of course look in the logs. However, there is an easier way. You can simply include the timestamp in the Bash history on Linux to make it easier to find when a particular command was executed on Linux.

How to enable timestamp in Bash history on Linux

Bash keeps a history of commands 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 recently executed commands using the command history as shown below:

$ history

Output example:

šŸ§ How to enable timestamp in Bash history on Linux

As you can see in the above output, although the history command displays a list of previously executed commands, it does not show when those commands were executed.

To enable timestamping in Bash history on your Linux, you need to set the HISTTIMEFORMAT environment variable.

This variable is used to display the timestamp associated with each displayed record in history.

Run the following command to set the HISTTIMEFORMAT environment variable:

$ export HISTTIMEFORMAT='%F %T '

Here the% F parameter is used to display the date in YYYY-MM-DD (year-month-date) format.

And the% T option is used to display the time in HH: MM: SS (hours-minutes-seconds) format.

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

šŸ§ How to enable timestamp in Bash history on Linux

Excellent! Now you can easily find out when a certain command was executed on your Linux system.

If you want to display the timestamps for the most recent ā€œNā€ commands, for example 10, pipe the output of the history command to the tail command, for example:

$ history | tail -10

Note that this will only time-stamp new history entries after the HISTTIMEFORMAT environment variable has been set for sessions.

You can also customize the date format to your liking, as shown in the following command:

$ export HISTTIMEFORMAT='%d/%m/%y %T '

This environment variable shows the date and time in the command history in dd / mm / year format, for example. 11/27/20 7:11:55 PM.

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

$ nano ~/.bashrc

Add the following line to the end:

export HISTTIMEFORMAT='%F %T '

or

export HISTTIMEFORMAT='%d/%m/%y %T '

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

Run the following command for the changes to take effect immediately:

$ source ~/.bashrc

The system will only display the time stamp for the current user.

To enable the Bash history timestamp for all users on the system, edit the / etc / profile file:

$ sudo nano /etc/profile

and add there:

export HISTTIMEFORMAT='%F %T '

or:

export HISTTIMEFORMAT='%d/%m/%y %T '

Save and close the file.

For the changes to take effect, run:

$ sudo source /etc/profile

For more information, see the man pages:

$ man bash

Now you know how to display the date and time in the output of the history command on Linux.

Conclusion

As stated, if you’ve ever wanted to check when a command was executed on your system, just include the timestamp in the bash history command as described above.

Sidebar