Linux history allows you to show past commands executed in terminal session. However, it does not show the date & time of execution. Sometimes you may need to display commands with date & time in Linux. It helps you search for commands executed on a specific date & time. In this article we will look at how to show commands with date & time.
Linux History: Show Commands with Date & Time
Here are the steps to show commands with date & time. By default, history shows only command number and executed command.
Here is the default output of history.
$ history 1994 sudo systemctl status apache2 1995 ls 1996 cd /var/www/html/ 1997 ls 1998 cd tech-blog/ 1999 ls 2000 sudo vim .htaccess
The above history command does not display the date/time at which these commands were executed.
Also read : How to Limit Requests Per IP in Apache
In order to show commands with date & time, we will create an environment variable HISTTIMEFORMAT and add it to bash shell with the following command.
$ echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
In the above command, we have used certain format specifiers to describe the timestamp format for commands.
- %d – Day
- %m – Month
- %y – Year
- %T – Time
You can change them as per your requirement. Whenever you run history command, it looks for the HISTTIMEFORMAT variable to determine the date & time format of displayed commands.
Also read : How to Change XAMPP Apache port
Apply the changes using the following command.
$ . ~/.bash_profile OR $ source ~/.bash_profile
Now when you run the history command, it will show the commands with date & time.
$ history 1994 20/04/21 04:15:32 sudo systemctl status apache2 1995 20/04/21 04:15:32 ls 1996 20/04/21 04:15:32 cd /var/www/html/ 1997 20/04/21 04:15:32 ls 1998 20/04/21 04:15:32 cd tech-blog/ 1999 20/04/21 04:15:32 ls 2000 20/04/21 04:15:32 sudo vim .htaccess
This is useful because if you want to search for commands executed at a particular date & time, just pass the output of history command to grep command. Here is an example,
$ history | grep "20/04/21 04:23" 2002 20/04/21 04:23:28 echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile 2005 20/04/21 04:29:36 history | grep "20/04/21 04:23"
Please note: When you run above echo command followed by source command, all previous command’s date time will be set to today’s date and time. But the date and time of subsequent commands will be show correctly. Here is an example.
484 01/07/24 04:16:32 ps aux|grep nginx
485 01/07/24 04:16:32 sudo nginx -c /home/ubuntu/conf/nginx.conf
486 01/07/24 04:16:32 ps aux|grep nginx
487 01/07/24 04:16:32 exit
488 01/07/24 04:12:42 echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
489 01/07/24 04:13:44 source ~/.bash_profile
490 01/07/24 04:13:48 history
This is because history doesn’t keep track of date and time of commands unless you explicitly ask it to. SO it cannot assign a specific date and time for past commands. It can only set it for those commands that are run after exporting HISTTIMEFORMAT variable.
If you want to disable it, then open ~/.bash_profile file and remove the following line from it.
export HISTTIMEFORMAT="%d/%m/%y %T
Save and close the file. Run the following command to apply changes.
$ source ~/.bash_profile
Sometimes you may need to start a new session to see the changes, in case they are not applied to current session.
Also read : How to Use Linux History Command
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.
This example is not accurate: HISTTIMEFORMAT=”%d/%m/%y %T. This actually applies the set date (today) to ALL commands in history, even a certain command was run a month ago, it will still show todays date.
Thank you for your feedback. When you export HISTTIMEFORMAT to bash_profile the first time and run source command to apply changes, it will set the date and time of all past commands to today’s date and time. All subsequent commands will be recorded correctly. This is because history command does not save the date and time of commands unless you set HISTTIMEFORMAT variable. So it does not have info about past commands, only the ones that are run after setting HISTTIMEFORMAT. We have updated the post to mention it.