history Command in Linux | Explained

The “history” command displays the list of the commands that were ever typed unless run out from the history buffer size, cleared, or deleted the history. Each command has a corresponding event id displayed on the left of the command. Whenever the specific command from the list of previous commands needs to be executed, the “history” comes into play.

This post offers the detailed functionality of the history command in Linux. The content covered in this post is discussed below:

Let’s get started with the “history” command.

What is the history Command in Linux?

When the user runs a command on the terminal, these commands are temporarily saved into the buffer. To revisit those commands, Linux offers the “history” command. The working and usage of the “history” command are provided below:

Syntax:

$ history

The supported arguments or options of the “history” command can be obtained from the following command:

$ history --help

How to Use history Command in Linux?

Typically, the “history” command is also beneficial to list down the commands typed on the terminal from the last session, either by command number or by command name. This section describes some practical examples of using the “history” command in multiple ways.

Example 1: Display the Commands History

Simply run the “history” command in the terminal without any argument. It will display how many commands have run over the current Linux system:

$ history

The output shows that the current system has used a total of “174” commands.

Example 2: Shows the Limited History Commands

By default, the “history” command represents the list of all commands that are used by the current system. But if the user passes the number after the “history” command, then it will show the specified recently used commands in the output:

$ history 8

The above output displayed the top “8” recent commands of Linux.

Example 3: Execute the Command Using Event id

The event id is displayed on the left side of each command available in the history list. The user can execute, modify or recall the command again using its “event id”.Suppose the “history” list contains the following commands:

Run the “history” command with “!3” in the terminal. It will print and execute the command having event id “3”:

$ !3

The combination of “!” and the “p” option only prints the specified command, not execute it as shown in the below screenshot:

$ history !7:p

The output displays the command on the “7th” number in the history list.

Example 4: Clear the History List

The “-c” option of the “history” command clears the full history list. To perform this task, executes the “history” command with the “-c” option in the terminal(Ctrl+Alt+T):

$ history -c

The output verifies that the history list is completely cleared. Now again, run the history or any other command. At that time, the above list contained only one recently used command.

Example 5: Delete the Nth Command From History List

The “-d” option of the “history” command helps the user to remove the specific command from the history list instead of the whole list. For example, the “ls -l” command is on the “4th” index as shown below:

$ history

Type the “history” command with the “-d” option and the “event id ” of the “ls -al” command:

$ history -d 4

Again run the simple “history” command to verify the removal of “ls -al” command:

$ history

The output verifies that the “ls -al” has been removed from the list.

Example 6: List All Commands with Date and Timestamp

The “.bashrc” file stores the Bash shell settings. This file can change the output format of the “history” command by changing this file. To do so, first, open the “.bashrc” file in the default text editor “Nano” by following the command:

$ sudo nano .bashrc

Now, add the below line in the “.bashrc” file at the highlighted place for changing the output format:

export HISTTIMEFORMAT="%c "

Save the new changes using “Ctrl+S” and close it by pressing the shortcut key “Ctrl+X” and execute the file:

$ source .bashrc

Again run the “history” command and see the “history” command output in data and timestamps format:

$ history

That’s all from the “history” command.

Conclusion

The “history” command provides a list of all the typed and executed commands on the entire system. Any of the commands from the buffer (history) list can also be executed easily. In this post, the objective, usage, and working of the “history” command line utility are briefly described along with practical examples.