Home BASH How To Enable Timestamp In Bash History In Linux
How To Enable Timestamp In Bash History In Linux

How To Enable Timestamp In Bash History In Linux

By sk
4087 Views

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
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 '

Here, the %F option is used to display the date in YYYY-MM-DD (Year-Month-Date) format. And the %T option is used to show the time in the format HH:MM:SS (Hour-Minute-Seconds) format.

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

Please note that this will only set the timestamps for new history entries after the HISTTIMEFORMAT environment variable is set for sessions.

You can also use 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 history command in dd/mm/year format e.g. 27/11/20 19:11:55.

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

$ nano ~/.bashrc

Add the following line at 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 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:

export HISTTIMEFORMAT='%F %T '

Or,

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

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

$ sudo source /etc/profile

For more details, refer the man pages:

$ man bash

You know now to show date and time in history command's output in Linux. As stated already, if you ever wanted to check when a command is executed, simply enable timestamp in bash history command as described above.

Are you using Fish or Zsh shell? Check the following tutorials to learn how to enable timestamp in history command:

Hope this helps.

Related read:

You May Also Like

2 comments

Marcos Alano November 28, 2020 - 4:36 am

There are this option with Zsh?

Reply
sk November 28, 2020 - 6:56 pm Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More