How to View and Manage Linux Command History

The power of commands is simply unmatched when it comes to the Linux terminal. But sometimes you need to type lengthy commands multiple times to get the task done. Retyping such commands is not feasible and could be a painstaking process. Therefore, in this article, we will demonstrate how to view and manage the Linux command history.

How to View Command History in Linux

In Linux, we use the history command to view the list of all previously executed commands. It has a very simple syntax:

history <options>

Some of the options to pair with the history command are:

OptionsDescription
-cClears the command history for the current session
-wwrites the command history to a file
-rreloads the command history from the history file
-n <max_entries>Limits the output number of recent commands

Simply run the history command to see the list of all previously executed commands in Linux terminal:

Using the history command to view the Linux command history

In addition to viewing command history, you can also manage your command history and perform actions like modifying a previously executed command, reverse searching the command history, and even deleting the history altogether.

How to Modify a Previously Executed Linux Command

As discussed earlier, every command which you execute gets stored in the command history. To modify or reuse a command executed earlier, you can use either of these steps:-

Using command history expansion

The exclamation symbol ‘!’ expands bash history when paired with “event designators”. Some of the event designators you can use are –

  • !! – Inserts the last command executed into the shell prompt.
  • !<n> – Inserts the nth command executed into the shell prompt.
  • !<command> – refers to the most recent <command> executed.
  • !?<string> – refers to the most recent command containing <string>.
  • How to View and Manage Linux Command History
  • How to View and Manage Linux Command History
  • How to View and Manage Linux Command History
  • How to View and Manage Linux Command History

Sometimes scrolling through the entire list of thousands of commands to search for a particular command is not feasible. For this, you can use the reverse command search with “CTRL + r”. It will open a new prompt. Here type the command you want to search, and the complete command which was previously executed will be shown. Hit the enter key on the keyboard to execute the command.

reverse command search

How to Clear Command History in Linux

To clear the entire command history, use the -c flag with the history command.

history -c
clearing the entire Linux command history

How to Modify Command History Constraints in Linux

Based on some constraints, the bash command history gets saved accordingly in Linux. All such constraints are stored as system environment variables and can be modified much the same way you modify user-defined environment variables.

Limiting the number of commands stored

To limit the number of commands stored in the history, set the HISTSIZE environment variable with this syntax –

export HISTSIZE=<max_commands_to_store>

For example, to store only the most recent 1000 commands –

export HISTSIZE=1000
limiting the Linux command history size to view only 1000 records

Ignoring specific commands

To exclude a particular command from saving in the commands history, use the HISTIGNORE environment variable with this syntax –

export HISTIGNORE=<command1:command2:command3>

Here, the commands “command1”, “command2” and “command3” are set to be excluded from the bash command history. For example, to exclude the commands ls, cd, and exit –

export HISTIGNORE='ls:cd:exit'
excluding ls, cd and exit commands from the command history
#Tags
comment Comments 0
Leave a Reply