This article serves as a comprehensive guide to the Linux ‘history’ command for beginners. It covers the history of the command, its usage, common parameters, use cases, and tricky skills, providing an in-depth understanding for new users. The ‘history’ command is a powerful tool in Linux, and mastery of it can significantly improve command-line proficiency.
Instructions
This guide provides an in-depth look into the Linux ‘history’ command, including its history, usage, common parameters, common use cases, and tricks. It aims to provide beginners with a comprehensive understanding of the command to increase their proficiency in Linux command-line operations.
History
The ‘history’ command is a part of the GNU Readline library, which provides a set of features for facilitating the use of command-line programs. It was designed to keep track of the commands entered in a shell session, enabling users to easily recall, edit, and re-run previous commands.
When and why to use it
The ‘history’ command is primarily used to review the commands that have been previously entered in a shell session. This feature is especially useful when you need to recall complex commands or scripts that were run in the past. It also helps in troubleshooting by showing the exact commands that were executed.
How to use it
The ‘history’ command can be used simply by typing history
into the command line and hitting Enter. This will display a list of the commands that have been entered in the current shell session.
history
The commonly used parameters
-c
This parameter is used to clear the history list. When you typehistory -c
, all the entries in the history list will be deleted.
history -c
-d
This parameter is followed by a number and is used to delete the specific history entry at that position. For example,history -d 2
will delete the second entry in the history list.
history -d 2
Other supported parameters
-a
Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.-n
Read all history lines not already read from the history file.-r
Read the history file and append the contents to the history list.-w
Write the current history list to the history file.
Most common use cases
One of the most common use cases of the ‘history’ command is searching for a previously used command. You can use history
in combination with 'grep' to search for specific commands.
history | grep mkdir
The tricky skills
A less-known usage of the ‘history’ command is to execute a specific command from the history list by using an exclamation mark (!) followed by the number of the command in the history list. For example, !2
will re-run the second command in the history list.
!2
What needs to be noted
One point to be aware of when using the ‘history’ command is that it is session-based. This means that it only displays the history of the current session, and not the commands used in other sessions or terminals.
Conclusion
The ‘history’ command is a powerful tool that provides great convenience when working with the Linux command line. It helps users recall and re-run commands, saving time and effort in daily operations. Understanding and mastering the use of the ‘history’ command is beneficial for anyone who often interacts with the Linux command line.