Beginners Guide for History Command in Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

Each command executed in your Linux terminal is taken as an event and associated with an event number (or index number) later found in the β€œ~/.bash_history” file.

Now, you can use a program like a β€œcat” to read the record of this file, but Linux provides a separate β€œhistory” command that gives you more features and functionality.

It involved features like viewing history records, removing single or complete records, executing a specific command from the history using its event number, and many more.

In this article, you will learn how to configure history records, history command usages, and a few more tips that will help you in your Linux journey.

Tutorial Details

DescriptionHistory
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteshistory
Internet RequiredNo

Syntax of the History Command

The history command takes only one argument for specifying the option (and it is optional).

$ history [OPTION]

View the History Record

Get to your terminal and execute the following command without any options to display the list of all executed commands in the past.

$ history

Output:

Viewing the history record

Or, if you are interested in the five most recent history records, specify the number of records you wish to list in integer format using the history command.

$ history 5

Output:

Viewing the recent five history records

Once you close the terminal, the records for the current session will be reflected in the history file.

Displaying Records with Date and Timestamps

Currently, the history command only shows the event number (or index number) along with the user-executed commands; however, you can also display the date and time along with these records.

For that, you need to configure your shell configuration file, like β€œ~/.bashrc” for Bash shell, using your choice of text editor, either Vim or Nano.

$ vim ~/.bashrc

Now, add the following record at the end of the configuration file.

export HISTTIMEFORMAT="%c "

Output:

Modifying the shell configuration file for the history command

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

$ source ~/.bashrc

Lastly, execute the history command to verify the changes.

$ history

Output:

Displaying the date and timestamp with history records

Configuring the History Command

The shell configuration file (β€œ~/.bashrcβ€œ) has two variables that control the history buffer.

  • HISTSIZE: It’s responsible for showing the defined number of results when the history command is executed.
  • HISTFILESIZE: It’s responsible for recording a certain number of user executed commands in the history file.
History setting in the shell configuration file

For example, if β€œHISTSIZE=1000” is specified, then when you execute the history command, it will only list the 1000 records.

And if β€œHISTFILESIZE=2000” is set, then only 2000 user-executed commands will be stored in the history file, and once it reaches the edge point, it will start removing and adding one record.

For example, if you want to only list ten results when the history command is executed, replace β€œHISTSIZE=1000” with β€œHISTSIZE=10β€œ, as shown.

Modifying the "HISTSIZE" variable

Save and close the file, restart the terminal session, and execute the history command to view the changes.

$ history

Output:

Showing the 10 entries in history

Executing Command Using Event Number

Every executed command is stored with an event number in the history list, and this event number can be used to reuse the assigned command with the (β€œ!β€œ) exclamation mark.

$ !298

Output:

Reusing the previously executed command

Or, use the β€œ:p” after the event number to view the command before executing it, so the wrong command does not get executed.

$ !298:p

Output:

Viewing the record before executing

Also, you can add a dash (β€œ-β€œ) before the index number to start counting from the end of the list.

$ !-2

The above command will execute the second last command from the history list.

Executing the second last execute command

Or, use the double exclamation mark (β€œ!!β€œ) to repeat the last executed command.

$ !!

Output:

Re-executing the last executed command

I personally use the double exclamation mark when I forget to add β€œsudo” in front of any command that requires it, for example, the β€œsudo apt update” command.

$ sudo !!

Output:

Adding sudo before the command using a double exclamation mark

Searching for Command by String

Adding a string in front of the exclamation mark will execute the most recently executed command that starts with the specified string.

Recently, the β€œsudo apt update” command was executed, so if β€œsudo” is specified as a string with an exclamation mark, then it reruns the same command.

$ !sudo

Output:

Executing the latest recent command with the specified string

Listing the Matching Commands

You can combine (pipe) the history command with theΒ grep commandΒ to get the list of matching commands that contain the specified string.

For example, the following command will display a list of all commands that contain the β€œsudo” string.

$ history | grep sudo

Output:

Displaying the list of commands with specific string

Replacing the String from a Recently Executed Command

You can use the following syntax to replace the string from a recently executed command:

$ ^[OLD_STRING]^[NEW_STRING]^

For example, the last executed command in my system is β€œsudo apt updateβ€œ, and I want to replace β€œsudo” with its minimalist variant β€œdoas” using the following command:

πŸ“
If the string occurs multiple times in the same command, then only the first occurrence will be replaced with the specified string and the rest will remain the same.
$ ^sudo^doas^

Output:

Replacing the string from the recently executed command

Executing Command Without Recording in History

There are multiple ways to prevent an executed command from appearing in the history record; the most straightforward is using the set command, which will temporarily disable the command recording.

πŸ“
It will come in handy when you intend to execute a command that asks for sensitive information like a username, email, or password.
$ set +o history

Once you’re done with the command execution, you can re-enable it using the following command:

$ set -o history

Deleting the Single or Complete History Record

You can use the β€œ-d” flag with a specific event number (or index number) to remove a specific record from the history list.

$ history -d 299

The above command will remove the command with event number β€œ299” from the history list.

Removing a specific command from the history list

Or, you can use the β€œ-c” flag to wipe out the complete record from the history list.

$ history -c

Output:

Clearing the complete history record

That was the last example of this command.

This article is enough for a beginner or professional to understand the concept of history and history records in Linux.

However, if something that needs to be added is not included, please mention it in the comment section.

Till then, sayonara!

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.