The history command in Linux is a frequently used command to search history. You probably won’t use this command on a daily basis, but you will certainly require it to look into your work.
A. Description
The history command in Linux displays the previously executed commands and a number to imply an event number is also shown with this command. This command will help you recall any command you’ve used before but forgot about it. It will help you in your learning process and let you efficiently use bash commands.
B. Syntax
The history command in Linux has a really simple syntax. You can just type history and that’s it. You can also add a number after the command to show how many previously executed commands you want to see in the command line.
But on the man page, there is no syntax available for the history command.
Note: Generally the more you use history, the larger the history command will become. So, I recommend you pipe this command with the less command for an interactive view.
C. Options
There are not many options available for the history command in Linux. Here are some of the options that will come in handy for daily usage. In the man page of the history command, there are no options or syntaxes available.
man history
Output >
Useful Options
- -d EVENT_NUM (Deletes the instance with the event number of EVENT_NUM from history)
- -c (Deletes all the history)
Note: Deleting only one instance won’t remove the event number, the next commands in the list will take its place.
Practical Examples of the “history” Command in Linux
The history command has some simple uses and there are not that many varieties. Here, you will learn some of the useful examples of “history” command uses that will come in handy for you.
Example 1: Using the “history” Command to Display All Previously Executed Commands
You can use the history command to view all the previously executed commands. This is relatively easy and requires you just to type history. The general syntax is also just typing history in the command line. You can try the following steps to try it out.
Steps to Follow >
➊ At first open the Terminal in the Home directory.
➋ Type the following command in the command prompt:
history
➌ Now, press the ENTER key.
Note: At the end of the list you will also find the history command that you just used.
Output >
As you can see, it shows all the previously executed commands in the system.
Similar Readings
- The “man” Command in Linux [6 Practical Examples]
- The “cal” Command in Linux [7 Practical Examples]
- The “bc” Command in Linux [10+ Practical Examples]
Example 2: Viewing Only N Number of Previously Executed Commands Using the “history” Command
Sometimes you just want to view a number of previously executed commands, not all of them. To do that you just need to type a number after the history command and the command will then show the number of the last command. There’s another way of doing this, try piping the history command with the tail command and the same type of result will be shown. The general syntax is as follows.
history NUM
history|tail -NUM
You can go through the following steps to try this command.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Type the following command in the command line to try this.
history 5
history|tail -5
❸ Press the ENTER key.
Output >
As you can from the image below, using both of these commands shows similar results.
Example 3: Finding the Command Using the Event Number
Sometimes you may want to find your last executed command by just the event number. In that case, you can use the (!) symbol and write the event number.
!NUM
Let’s say you want to view the 555th command. You can follow the steps below to try it out.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Type the following command in the command line.
!555
❸ Press the ENTER key.
Output >
As you can see the command with event number 555 and the output is in the command line.
Example 4: Searching Through the History Piping the “grep” Command
If you want to search within the history, you can use the grep command with the history command. You can search for any keyphrase by piping the grep command with the history command.
Here is the general syntax of both methods.
history|grep KEYPHRASE
Now, let’s say you want to find the commands that used Desktop/ as a key phrase. Just follow the steps below to use it.
Steps to Follow >
❶ Open the Terminal in the Home directory
❷ Type the following command to find all the matches of Desktop/.
history|grep Desktop/
❸ Press the ENTER key.
Output >
In the command line, you will find all the possible matches of the commands involving Desktop/displayed.
Similar Readings
- The “cron” Command in Linux [3 Practical Examples]
- The “crontab” Command in Linux [10 Practical Examples]
- The “date” Command in Linux [8+ Practical Examples]
- The “ncal” Command in Linux [8 Practical Examples]
Example 5: Removing an Instance from History Using the “history” Command
If you want to remove an instance of the previously executed command from the history, you can use the -d option. You have provided the event number after the -d option.
history -d EVENT_NUM
Suppose, you want to remove the command with event number 624. You can follow the steps below.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Type the following command to delete a command event number 624.
history -d 624
❸ Press the ENTER key.
Output >
Here, you will find that the ls -l command is no longer available which was the command with an event number 624.
Example 6: Removing All History Using the “history” command
If you want to remove/delete all the history of the previously executed commands, you can use the -c option. You can follow the steps in the following.
Steps to Follow >
❶ Open the Terminal in the Home directory.
❷ Type the command below to delete all history.
history -c
❸ Press the ENTER key.
Output >
As you can see everything inside the history has been deleted.
Conclusion
The history command is often used to keep track of all the commands used in the past. But you can also keep your privacy by deleting some or all of the history. In this article, you see the commands available for the history command in depth. I hope it helps you in your learning and future endeavors with Linux.
Similar Readings
- The “neofetch” Command in Linux [12+ Practical Examples]
- The “tree” Command in Linux [14 Practical Examples]
- The “whatis” Command in Linux [12 Practical Examples]
- The “whereis” Command in Linux [10 Practical Examples]
- The “which” Command in Linux [3 Practical Examples]
- The “at” Command in Linux [7 Practical Examples]
- The “tee” Command in Linux [4 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners