Skip to content
Related Articles

Related Articles

Improve Article
Setting Date and Time For Each Command in Bash History
  • Last Updated : 07 Mar, 2021

Prerequisites: History command in Linux with Examples

In bash, all history of commands executed by the user are stored in /home/user/.bash_history. This file is store by default in 1000 commands, but we can increase or decrease it. By using the history command the system administrator can view the commands executed by the respective user. Then history command shows the output with the command number and command executed sequentially. Here is an example of a history command

history

This command gives a complete history of the currently logged-in user.

Set Date and Time for Each Command You Execute in Bash History

But in output, we can see that we can see history with command numbers. But we have to get output such that the history command shows the output with the date and time of executed command. We can do it by setting the HISTTIMEFORMAT variable. We can do it in two-way temporary and permanent.



Temporary history command with date & time:

To set the HISTTIMEFORMAT variable temporary we have to export it. Use the following command to export the HISTTIMEFORMAT variable.

export HISTTIMEFORMAT='%F %T'

In the above command %F is show the full date in %Y-%m-%d  (year – month – date) format and %T is shown full time in %H:%M:%S (hour:minute:seconds) format. To know more about the date command use man command like

man date 

 history command in Linux with Examples

Then for the current terminal session, we can see the output of the history command with the date and time like:

Set Date and Time for Each Command You Execute in Bash History

Permanent history command with date & time:

To set the HISTTIMEFORMAT variable permanent we have to export it in ~/.bashrc file. For that open the ~/.bashrc file in your favourite editor.

vim ~/.bashrc

And now add the following line into the ~/.bashrc file

export HISTTIMEFORMAT='%F %T'

Set Date and Time for Each Command You Execute in Bash History

After this save & exit from file to see the changes you can exit from the current terminal session and open another terminal or run the following command:

 source ~/.bashrc

This will show the permanent output of the history command with date & time in the bash shell.

My Personal Notes arrow_drop_up
Recommended Articles
Page :