The history
command in Linux allows users to view their command line history, making it easy to recall and reuse past commands. Whether you’re a seasoned Linux user or just starting out, understanding how to navigate and utilize the command history can greatly enhance your productivity. In this guide, we will explore the various features and functionalities of the history
command.
Viewing Command History
To display the entire command history, simply enter the following command:
history
Executing this command will present you with a numbered list of all the commands you have previously run, as shown below:
Repeating Commands from History
The history
command also allows you to repeat a specific command from your history. By using the syntax !<command number>
, you can easily rerun a previously executed command. For example, if you wanted to repeat the command with the number 121 from the above history, you would type !121
.
Searching Command History
In addition to displaying the entire command history, you can also search for specific commands using the grep
command. This is particularly useful when you want to find a command you ran earlier but can’t remember the exact details. To search for a command, use the following syntax:
history | grep <search pattern>
For example, if you want to find any command that includes the word “docker”, you would enter:
history | grep docker
This will display a filtered list of commands that match your search pattern, as shown below:
Clearing Command History
To clear your command history, simply run the command history -c
. This will remove all the stored commands from your history, allowing you to start with a clean slate.
Note: The
history
command is available on Linux, macOS, WSL (Windows Subsystem for Linux), and any UNIX environment. It provides a unified way to access your command history across different platforms.
By mastering the history
command, you can streamline your workflow and take advantage of past commands without the need to remember or retype them. Whether you want to repeat a command, search for a specific command, or clear your history, the history
command offers a powerful tool for managing your command line experience.
Tags: Linux commands, history command, command line, command history, grep, UNIX