Introduction to Linux History Command
In the Linux ecosystem, the history command is used to get the list of old command that have triggered by earlier in the same terminal. With the help of the history command, we are able to reuse the older command instead of typing the whole command. The history feature is not available in all the shell environment. It is available in bash, Korn. The history feature is not available in the Bourne shell. Whenever we are triggering any command on the Linux shell. The executed or non-executed command having their own event number with the help of the same event number. We are able to evoke the older command.
Syntax of History Command:
- history: We can use the history keyword in the syntax or command. It will take the argument as an option. It will provide the end result with the list of older commands or provide the list of specific commands only.
- OPTION: We can provide the different flags as options that are compatible with the history command.
How Linux History Command works?
In the Linux environment, history is not a command. The end-user is considering this is a command. But with the help of built-in keyword, we are able to fetch out the older command with the help of history shell file. All the history command is saved in the “.bash_history” file. In the same file, we are able to fetch all the historical commands. When any user will log in in the shell terminal. By default, the “.bash_history” file will create and store all the Linux commands in the “.bash_history” file. We can also edit and do the modification in the same file.
While working in the shell terminal, we are triggering the multiple commands. All the
triggered commands (consider correct execution or non-execution command) will store in the “.bash_history” file. If we need to perform some operation on history command. In backed, the “.bash_history” file is only in use.
Below are the lists of options available in the History Command:
|Sr. No||File Name||Description|
|1||-c||It will clear the history list. It will delete all the entries of the “.bash_history” file.|
|2||-d||It will delete the history entry at the position offset.|
|3||-a||It will help to append the new history lines to the “.bash_history” file.|
|4||-n||It will read the history lines but not already read from the “.bash_history” file into the current history list.|
|5||-r||It is helping to read the contents of the “.bash_history” file and use it as the current history of the terminal.|
|6||-w||It will write the current history to the “.bash_history” file and help to overwrite the current history file contents.|
|7||-s||It will store the arguments in the “.bash_history” file as a single entry.|
Examples of Linux History Command
Given below are the examples of Linux History Command:
It is very simple and common History command in a Linux environment. It will print all the history command of the working terminal.
- As per the above command, we are able to print all the history command in the terminal window. We are having a total 1002 command in history.
History Command – Print Limited Command
In the history command, we are able to print the number of commands that are executed recently.
- As per the above command, we are able to print the history command but only the last 10 commands executed in the shell window.
History Command – Execute the Command with Event Number
In history command, we are having the functionality to execute the history command with a command event number. We do not need to write the complete command.
- In history command, we are getting the information of command as we all as the event id associated with the command. If I need to execute the “ip add sh” command without typing it. I can do that, with the help of event id associated with it i.e. “20”.
History Command – Only Print the Command with Event ID
In the history command, we are only able to print the command before execution. To do that, we need to use the “p” option with the history command event id.
- If we need to print only the command without execution of it. We need to use the “p” option with it. So it will not execute the command but only print the command.
History Command -With Grep command
In history command, we are having the functionality to use the history command with the combination of different commands like grep, tail, less, more, etc.
history | grep -i "which"
- In the history command, we are able to find the specific command from the number of history command. We are finding “which” keyword from the entire history command.
History command – get most resent command
In history command, we are able to get and executed the most recent command on the terminal.
- As per the above command, we are able to execute the most recent command.
History Command – With “-d” Option
In the History command, we can delete the specific history command with the help of associated event id.
history -d 1012
- As per the above command, we are deleting the specific history command record from the history file. We are deleting the “1012” event id command.
History Command – With “-c” Option
We are able to delete all the historical command of the current login terminal.
- We can delete all the history command in a single shot. We need to use the “-c” option with the history command.
We have seen the uncut concept of “Linux History command” with the proper example, explanation and command with different outputs. The history command is useful to find all the historical command was executed in the terminal window. The old or history command is store in the “.bash_history” file. We are having the functionality to use the different options with the history command.
This is a guide to Linux History Command. Here we discuss how linux history command works along with examples respectively. You may also have a look at the following articles to learn more –