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:
Some of the options to pair with the history command are:
Options Description -c Clears the command history for the current session -w writes the command history to a file -r reloads 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:
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>.
Using Reverse command search
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.
How to Clear Command History in Linux
To clear the entire command history, use the -c flag with the history command.
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 –
For example, to store only the most recent 1000 commands –
Ignoring specific commands
To exclude a particular command from saving in the commands history, use the HISTIGNORE environment variable with this syntax –
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 –