The “history” Command in Linux [6 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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 >

man page of history command in linux

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.

history command in linux


Similar Readings


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.

last 5 commands ofhistory in linux

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.

finding and executing the 555th command in linux

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.

using grep command to search for Desktop


Similar Readings


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.

deleting an instance from history in linux

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.

Deleting all history

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

Rate this post
Md. Rafsan Zani

Hello, I’m Md. Rafsan Zani. I have recently completed my Undergraduate from the Bangladesh University of Engineering and Technology (BUET). Currently, I’m pursuing higher studies abroad. I’m really interested in computer science and would like to learn a lot about the wonderful world of computers. Currently, I’m working as a Linux Content Developer Executive and find Linux really interesting. I certainly would like to learn more about Linux and implement them in my future studies. Read Full Bio

Leave a Comment