history – Display or manipulate the history list

The history command is used to display the list of previously executed commands in a Linux shell. The command can also be used to manipulate the history list, such as deleting specific commands or clearing the entire history.

Overview

To display the list of previously executed commands, simply type history in the terminal. This will display a numbered list of commands, with the most recent command being at the bottom of the list.

To execute a command from the history list, type an exclamation point (!) followed by the command number. For example, to execute the command at line 5 in the history list, type !5 and press enter.

To search for a command in the history list, type history | grep <search term>. This will display a list of commands that contain the search term.

To delete a specific command from the history list, use the -d option followed by the command number. For example, to delete the command at line 5 in the history list, type history -d 5.

To clear the entire history list, use the -c option. Type history -c and press enter.

Options

The following options are available for the history command:

Option Description
-c Clear the history list
-d Delete a specific command from the history list
-a Append new commands to the history list
-n Do not display the command number when displaying the history list
-r Read the history list from the history file

Troubleshooting tips

If the history list is not displaying any commands, it is possible that the history feature is disabled. To enable the history feature, add the following line to the .bashrc or .bash_profile file:

export HISTSIZE=10000

This will set the history size to 10,000 commands.

Notes

  • The history list is stored in a file called .bash_history in the user’s home directory.
  • The history list is not updated until the current shell session is closed. To update the history list immediately, use the history -a command.
  • The history list can be a security risk, as it can reveal sensitive information such as passwords or commands that were executed with elevated privileges. It is recommended to clear the history list regularly, or to disable the history feature altogether if security is a concern.