How to Display Command History in Linux | history Command
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 the history
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.
How to Display Command history in Linux
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 History
To show the limited number of commands that executed previously as follows:
Example
history 5

Note: This command displays the last 5 commands from the command history.
Execute a Command by Event Number
Execute a command using its event number with the !
symbol.
Example
!1997


Note: This will rerun the command with event number 1997.
Print Command Before Execution History
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 with grep
Combine the history
command with grep
for efficient searching.
Example
history | grep chpasswd

Note: This command filters and displays only the commands containing the term "chpasswd."
View the Most Recent Command History
To quickly view and rerun the most recent command, use:
Example
!!

Note: This will rerun the last executed command.
Execute a Command without Storing History
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 History
Execute a command using a part of its string.
Example
!command_starting_string

Note: This will execute the latest command starting with "command_starting_string."
Remove a Specific Command from History
Remove a specific command from history using the history -d
option.
Example
history -d 1996

Note: This command removes the command with event number 1996 from history.
Clear Entire Command History
Clear the entire command history using the history -c
option.
Example
history -c

Note: Be cautious, as this action irreversibly removes all commands from the history.
View the Last 10 Commands History
Use history | tail
to view the last 10 commands from the history:
Example
history | tail
Note: This command efficiently displays the most recent commands in the terminal.
Conclusion
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.