Linux: how to master the history command?

Linux - how to master the history command

Under Linux, the history command is used to view the history of commands previously entered in the Terminal, as its name suggests. With this command, you can track your previous actions from the command line. After reading this article, the history command will hold no secrets for you!

On a Linux machine, the history command is an excellent way of viewing previously executed commands. Every command executed in Terminal will be logged in this history and a number will be associated with each command. So, the history is numbered, as we'll see where each command has an ID, so to speak. This number will be indispensable for recalling a previously executed command, as we can call it up by its number to easily replay it.

1. Getting started with the history command under Linux

To launch the history command, simply type the magic word "history" in the terminal of your Linux machine, be it Debian, Ubuntu, Rocky Linux, CentoOS, etc.

history

Here's an example, on my Debian machine. You can see the entry number in the history on the left. On the right, the command executed. Here, you can see a lot of cURL commands, as these are the tests I carried out when writing the previous article on cURL.

Linux - Command history

The numbering system used by the Linux terminal interface represents the oldest command at the top of the list and the most recent command at the bottom. As you can see, the "history" command I've just entered has been given the ID number 100. 100 commands in the history is quite a lot... But it could be a lot more on a very active Terminal...!

If you want to see the last 10 commands executed, i.e. the 10 most recent commands :

history 10

This is one way of filtering the command's output. Another way is to use the "history" command with another command called "tail". Run the command below, it will give the same result.

history | tail -n 10

2. Replay a command from the Linux history

Let's start having fun... And let history repeat itself by replaying a previously executed command. Let's say we want to replay the command with ID "99", we'll use this syntax:

!99

You can see that it works like a charm:

Replay a command from the Linux history

This method is very useful for recalling a command executed a long time ago: it's much more productive than scrolling through the history with the directional arrow...

And if you want to replay the last command executed, simply type 2 exclamation marks (without spaces).

!!

As you can see, the previously executed command has been relaunched! If you're used to using the up arrow key, here's a new way to replace it (and impress your colleagues). More efficient, in my opinion.

To re-execute the previous command with "root" rights using "sudo", it's very simple: just add the prefix :

sudo !

To go a step further, you can execute your penultimate command with the following command:

!-2

In fact, we start from the end of the history (most recent command) and work backwards in the history, in this case to execute the second command starting from the most recent command. I've used the penultimate command as an example, but you could set a different value. But in this case, you might as well target the command with its ID.

3. Browse history with keywords

Suppose you've executed a command in your terminal, but you haven't completely memorized it. You want to find it again... And you know it's the last "curl" command you executed... To use this command again, you can call up the history by entering this keyword:

!curl

The most recent curl command in the history will be executed again on the Linux machine! The history command in this form also supports sudo if required:

sudo !curl

4. Interactive search in Linux history

Now, suppose you've executed several commands that are similar, like me with all my curl commands in the history. With interactive search in Terminal, it's now possible to search for the right command. To launch the interactive search, execute the key combination "Ctrl+R". Your Linux terminal should look like the one below.

When I enter "curl -I", the search engine shows me the last command I executed in this form.

order history and interactive search

You can enter as many keywords as you like, and the search result will be updated in real time. Press the Enter key to execute the command shown on the right (after ":").

To modify the command before executing it, use the left and right arrows on the keyboard. The selected command is displayed in the terminal, and you can make any necessary changes.

5. Delete Linux command history

So far, we've seen how to view the history, replay a command, etc... However, you may be looking for a way to purge the history entirely or delete a specific command. If there's an unencrypted password in a command in the history, that's a bit tricky.... The -d parameter of the history command will be your ally..

Example to delete the order with ID 119 :

history -d 119

If you run the history command, you'll see that it's no longer in the list. It's out of sight, out of mind!

Another example... To delete history entries between ID 110 and ID 115:

history -d 110 115

But that's not all: you can delete the last X commands entered in Terminal from the history. For example, to delete the last 10 commands (the hyphen is important, otherwise it doesn't have the same effect!) :

history -d -10

Finally, you can purge the entire history with the command below:

history -c

6. Conclusion

When you're feeling nostalgic about your work, you can have fun re-reading your Linux history.... All joking aside, after reading this tutorial, you'll be familiar with history command essentials under Linux !

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *