Open In App

How to Display Command History in Linux | history Command

The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the various features of the history command, enabling users to streamline their workflow and save time by efficiently accessing and reusing commands.

Understanding the history Command:

The history command in Linux provides a chronological list of previously executed commands, along with corresponding command numbers. This feature allows users to recall, reuse, and modify commands without having to retype them. The command history is stored in a file, typically ~/.bash_history for the Bash shell.

How to Display Command history in Linux

To view the command history, simply type:


Here, the number(termed as event number) preceded before each command depends on the system. You may get different numbers while executing on your own system.

Display a Limited Number of Commands History

To show the limited number of commands that executed previously as follows:

history 5 

This command displays the last 5 commands from the command history.

Execute a Command by Event Number

Execute a command using its event number with the ! symbol. For instance:


This will rerun the command with event number 1997.

To print a command before executing it to avoid errors, use the :p option after the event number. Example:


This will display the command associated with event number 1997 without executing it.

Search Command History with grep

Combine the history command with grep for efficient searching. Example:


history | grep chpasswd 

This command filters and displays only the commands containing the term “chpasswd.”

View the Most Recent Command History

To quickly view and rerun the most recent command, use:


This will rerun the last executed command.

Execute a Command without Storing History

If a command needs to be executed without being stored in history, unset the HISTFILE variable. Example:

This ensures that the command won’t be stored in the history file.

Execute a Command using a Part of the Command History

Execute a command using a part of its string. Example


This will execute the latest command starting with “command_starting_string.”

Remove a Specific Command from History

Remove a specific command from history using the history -d option. Example:

history -d 1996 

This command removes the command with event number 1996 from history.

Clear Entire Command History

Clear the entire command history using the history -c option. Example

history -c

Be cautious, as this action irreversibly removes all commands from the history.

View the Last 10 Commands History

Use history | tail to view the last 10 commands from the history:

history | tail

This command efficiently displays the most recent commands in the terminal.


In this article we discussed the history command which is crucial for efficient command-line navigation in Linux. This guide has explored various features, allowing users to view, search, and manipulate command history. From displaying a limited number of commands, executing by event number, and searching with grep, to advanced functionalities like executing without storing, removing specific commands, and clearing the entire history, users can enhance their command-line efficiency. This skill empowers users to save time, avoid errors, and confidently navigate the Linux command line.

Article Tags :