How to Check Linux Terminal Command History

The history command is a built-in shell command which is used to display previously executed commands in the terminal. Sometimes users want to revisit the past executed command, and this history command helps the users to go back in the command history.

Through the additional options of this command, you can reuse, modify, and recall the commands. Hence, this quick guide has everything about the ‘history‘ command in Linux and its various features.

1. Linux History Command

This command displays a list of previously executed commands. To execute the history command, simply type ‘history‘ in your terminal and press enter and you can see all the previously executed commands with their exact sequence as follows:

history
Linux History Command

The sequence number of the above commands is its history ID. You can use these history IDs to re-execute the commands in the terminal again. For this, add the ‘!‘ symbol with the history ID number of the command you want to re-execute. Here we re-execute the command number 9 as shown below:

command history ID pn Linux

2. Display only the Last 10 Used Commands

You can view the last 10 commands using the tail command with the history command.

history | tail
Display only Last 10 Used Commands

You can add a number with the history command to view only a limited number of commands:

history <number>
add a number with the history command

In the above result, the last four commands are displayed which were previously executed in the terminal.

3. Filter the Command History

Sometimes users want to find the specific command that they used in the past. So, you can also filter the command history by combining the history command with the grep command. For example, here we only search for the ‘cd’ command used in the terminal previously. To do so, run the following command:

history | grep 'cd'
Filter the Command History

As you can see from the above result, the command history only displays the ‘cd’ command with its exact running number or history ID.

4. Delete a Command from Linux History

The general syntax to remove any specific command from its history is as follows:

history -d <history ID>

For example, here we delete command 11 and run the following command:

Delete a Command from Linux History

In the above result first, you can see that the command at the 11th number has been deleted, and after deleting it the history ID numbers of all the subsequent commands have changed.

Conclusion

In this quick guide, we discussed the several features of the history command. Through this, you can search, view, and manipulate command history in Linux. Users can easily search and view the history of their command line using this valuable utility. Here using this command we display the previously used commands, filter the commands, execute the command through the history ID number, delete a specific command, and so on. Moreover, you can use the help command to explore this command more.

history --help
Get Linux History command help options

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.