The history command is one of the essential utilities for the Linux administrator, usually used to track all the shell commands you execute in past.
When you execute the command, the shell captures it and then stores it in the separate file of the user’s home directory at ~/.bash_history
. When the history command is executed, it fetches the data from this file and displays it as an output with the command and ID on the terminal screen.
The history command does not display the date and time in the output, as shown below.
$ history
Below is the behaviour of the above command.
If you want to filter out commands executed by you on a specific date or time, then you have to export a HISTTIMEFORMAT
variable with the legal date and time format codes.
A reference list of all the legal date and time format codes:
%d | Day |
%m | Month |
%y | Year |
%Y | Full Year |
%T | Time |
%F | Date (yy/mm/dd) |
Legal date and time format codes
Suggestion for you: Complete guide on Date command usage in Linux
From the above legal codes, once you decide on your requirements for the history command output, choose the legal code and assign it to the HISTTIMEFORMAT
variable, and then export it to your current shell environment, as shown below.
$ export HISTTIMEFORMAT="%F %T "
Display the Full Date and Time in the History Command Output
If you want to output the full date in (yy/mm/dd), format along with the command execution time in (hh/mm/ss) format in the history command output, you can export the HISTTIMEFORMAT
global variable with the “%F” code to display the full date and the “%T” code to display the whole time in the format, execute the below command.
$ export HISTTIMEFORMAT="%F %T => "
After that, execute the history command, and you will get a similar output as shown below with the command execution date and time.
$ history
Below is the behaviour of the above command.
If you do not prefer to display output in the default full date format (yy/mm/dd) and display it in the custom format like (dd/mm/yy), use “%d” for a date, “%m” for a month, and “%y” for a year separated with color (:) as shown below.
$ export HISTTIMEFORMAT="%d:%m:%y %T "
Below is the behavior of the above command.
From the above example, I expect you have got the idea of how to use this date and time code in your favour.
Also Read: How to Set Environment Variables in Linux
Once you decide on the format, you can set the HISTTIMEFORMAT
variable in the ~/.bashrc
file to execute it on every terminal session without re-specifying the environmental variable, using the below code.
$ echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc
The above command appends ‘export HISTTIMEFORMAT="%F %T "
‘ in the user ~/.bashrc
file. If the file does not exist, a new file will be created and then execute source ~/.bashrc
to reload the configuration.
Filter the History Command Output based on the Selected Date
By now, you have understood the date and time format codes and their implementation with ~/.bashrc
. What if you want to output a command executed on a specific date?
To do so, you need to pipe the grep command with history and specify the target date as shown below.
$ history | grep "2022-06-06"
In the above code, the date format is in (yy/mm/dd), and below is the output of the above command.
Filter the History Command Output based on the Multiple Date
There will be situations where you are probably required to output command history for multiple dates simultaneously. As shown below, this task can be achieved by specifying all those dates separated with a vertical slash “|” within the grep command.
$ history | grep -E "2022-06-05|2022-06-06"
The above command displays the command history of dates 2022-06-05 and 2022-06-06, as shown below.
Filter the History Command Output based on the Range of Dates
For output, the history command based on a range of dates in the output can be achieved using the awk command. Below is the sample command to output command from the range of dates between 2022-06-04 and 2022-06-06, as shown below.
$ history | awk '{if($2>="2022-06-04" && $2<="2022-06-06") print $0}'
Below is the behaviour of the above command.
Filter the History Command Output based on the Range of Time
To find the command you have executed in a specific block of time in a day, you can specify the time range from start to end, as shown below.
$ history | awk '{if($2="2022-06-06" && $3>="23:27:47" && $3<="23:28:04") print $0}'
Below is the behaviour of the above command.
FAQ
It’s normal behaviour. The next day, when the new command is executed, it will pick the new date.
If you export the environment variable directly within the terminal, then restart the terminal. If you added the environment variables inside the ~/.bashrc
file, then remove them from there and execute source ~/.bashrc
to reload the configuration.
Suggestion for you: How to Filter log file entries based on date range
Hi, I am really glad I have found this information. Today bloggers publish just about gossips and web and this is actually irritating. A good site with exciting content, this is what I need. Thank you for keeping this site, I’ll be visiting it. Do you do newsletters? Can’t find it.
Thanks for the kind words.
You can subscribe to our newsletter from the blog section