Display and Filter History Records Based on Date and Time in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

Executing theΒ history commandΒ without specifying any options will give you a clean record of previously executed commands with their event numbers.

$ history 10

Output:

Viewing the history list

As you can see, it does not display the date and time, so without them, you can’t even manage to filter the records.

In this article, you will learn how to add a date and time to history records, different examples, and how to filter the records based on the date and time.

Tutorial Details

DescriptionDisplay and Filter History Records Based on Date and Time
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteshistory
Internet RequiredNo

How to Include Date and Time in History Records

You need to modify the shell configuration file, like β€œ~/.bashrc” for Bash shell, with the β€œHISTTIMEFORMAT” variable, using your choice of text editor, either Vim or Nano, to display the date and time in history records.

$ vim ~/.bashrc

Output:

Shell configuration file

Then add the following line at the end of the configuration file to display the complete date and time in the history records.

export HISTTIMEFORMAT="%c "

Output:

Adding the date and time to the shell configuration file

Save and close the file, then reload the shell configuration changes using the source command.

$ source ~/.bashrc

Output:

Applying the shell configuration changes

Finally, run the history command to confirm the new output format.

$ history 5

Output:

Displaying the date and time with history records

More Examples with Different Formats

Instead of using the predefined date and timestamp β€œ%c” format, you can customize the output with multiple other formats to improve the level of detail in the history records.

The following is a list of all known date and time format codes:

CodesDescription
%dDay
%mMonth
%yYear
%HHour
%MMinutes
%SSeconds
%FFull date (Y-M-D format)
%TTime (H:M:S format)
%cComplete date and timestamp (D-M-Y H:M:S format)

The following are a few examples that you can follow to improve your skill in this format’s usage.

Displaying the Date and Time in the History Records

Use the β€œ%F %T => ” code to display the date in β€œY-M-D” format and the time in β€œH:M:S” format, and β€œ => ” used with spaces before and after to improve readability.

export HISTTIMEFORMAT="%F %T => "

Output:

Displaying the date and time in history records

Displaying the Custom Date and Time Format in History Records

This time, use the β€œ%d:%m:%y” code to display the date in β€œD:M:Y” and β€œ%H:%M” to display the time in β€œH:M” format.

export HISTTIMEFORMAT="%d:%m:%y %H:%M => "

Output:

Displaying the custom date and time format in history records

Displaying the Date and Time in a Fancy Way

The β€œDate: [%d-%m-%y]” code will show the date in β€œDate: [D-M-Y]” format, and β€œTime: [%T]” will show the time in β€œTime: [H:M:S]” format.

export HISTTIMEFORMAT="Date: [%d-%m-%y] / Time: [%T] => "

Output:

Displaying the date and time in a fancy way

Filtering the History Records Based on Date and Time

Once you include the date and time format codes in the shell configuration file, it will show you that all the commands were executed on the same day. Once you execute the new command on the next day, it will pick the new date and time.

Following are a few examples that are intended to work with β€œ%F %T” format code or something similar that will filter the command history based on different dates and times.

Listing the History Records for a Specific Date

Use the grep with history command to display the list of history records for a specific date.

For example, the following command will display the list of all commands executed on the β€œ2022-12-20” date.

$ history | grep 2022-12-20

Output:

Display the list of history records for specific date

Listing the History Records for Multiple Dates

The following command will display the list of all commands executed on the β€œ2022-12-19” and β€œ2022-12-20” dates.

$ history | grep -E "2022-12-19|2022-12-20"

Output:

Display the list of history records for multiple dates

Listing the History Records Based on a Range of Dates

The following command uses the awk command to display the range of commands executed between the β€œ2022-12-19” and β€œ2022-12-20” dates.

$ history | awk '{if($2>="2022-12-19" && $2<="2022-12-20") print $0}'

Output:

Displaying the range of commands executed between the specific dates

Listing the History Records Based on a Range of Time for a Specific Date

The following command will display the range of commands executed between β€œ14:27:47” and β€œ16:28:04” on β€œ2022-12-20β€œ.

$ history | awk '{if($2="2022-12-20" && $3>="14:27:47" && $3<="16:28:04") print $0}'

Output:

Display the range of commands executed between the specific time and date

Final Tips!

Using date and time in history records will help you a lot while searching for a specific command.

If you want to remove the date and time appearing in the history record, then remove the line added in your shell configuration file.

Lastly, if you have any questions or queries regarding this topic, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.