Last Updated : 15 Mar, 2025
The history command in Linux is essential for terminal users, enabling them to view, search, and manipulate previously executed commands. Now, mastering this Linux command allows for efficient commands of commands, automation of repetitive tasks, and troubleshooting without the need to retype lengthy commands.
In this article, we are going to discuss how to use history commands in Linux, command examples, and more.
What is thehistory
Command in Linux
The history command
in Linux displays a list of commands that were previously entered in the terminal. By default, it shows the last 1000 commands, but this can be configured. 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.
To view the command history, simply type:
Example
history
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 HistoryTo show the limited number of commands that executed previously as follows:
Example
history 5
Execute a Command by Event NumberNote: This command displays the last 5 commands from the command history.
Execute a command using its event number with the !
symbol.
Example
!1997
Print Command Before Execution HistoryNote: 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
!1997:p
Note: This will display the command associated with event number 1997 without executing it.
Search Command History withgrep
Combine the history
command with grep
for efficient searching.
Example
history | grep chpasswd
View the Most Recent Command HistoryNote: This command filters and displays only the commands containing the term "chpasswd."
To quickly view and rerun the most recent command, use:
Example
!!
Execute a Command without Storing HistoryNote: This will rerun the last executed command.
If a command needs to be executed without being stored in history, unset the HISTFILE
variable.
Example
echo $HISTFILE/home/user/.bash_history
Note: This ensures that the command won't be stored in the history file.
Execute a Command using a Part of the Command HistoryExecute a command using a part of its string.
Example
!command_starting_string
Remove a Specific Command from HistoryNote: This will execute the latest command starting with "command_starting_string."
Remove a specific command from history using the history -d
option.
Example
history -d 1996
Clear Entire Command HistoryNote: This command removes the command with event number 1996 from history.
Clear the entire command history using the history -c
option.
Example
history -c
View the Last 10 Commands HistoryNote: Be cautious, as this action irreversibly removes all commands from the history.
Use history | tail
to view the last 10 commands from the history:
Example
history | tail
ConclusionNote: This command efficiently displays the most recent commands in the terminal.
The history command in Linux
is an indispensable tool for efficient command-line operations. Whether you are looking to display, search, reuse, or clear your command history, this command offers a range of options to improve productivity. By mastering the history
command, you can streamline your workflow, avoid repetitive typing, and easily access previously used commands.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4