You can easily find the list of recently executed commands in Linux using the 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 a timestamp in the Bash history in Linux, making it easier to find when a specific command was executed.
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:
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-D
D (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:
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:
- Enable Timestamp For History Command In Fish Shell
- Enable Timestamp For History Command In Zsh In Linux
Hope this helps.
2 comments
There are this option with Zsh?
Check this guide for Zsh -> https://ostechnix.com/enable-timestamp-for-history-command-in-zsh-in-linux/