Hostwinds Tutorials

Search results for:


Table of Contents


Shortcuts for History Command
History Command Event Designators
History Command Word Designators
Example History Commands
Using Event Designators
Using Word Designators

How to Use the History Command in Linux

Tags: Linux 

Shortcuts for History Command
History Command Event Designators
History Command Word Designators
Example History Commands
Using Event Designators
Using Word Designators

If you have ever used a Linux Server, you know that many commands are important to remember. With the wide range of available commands, it can be hard to remember them all. For example, the one you ran not 2 minutes ago is not on the screen anymore. There are a few ways to find those commands once they are no longer visible, such as using the Up Arrow Key to cycle through them one at a time. However, you can also use the History command.

The history command lists the previous commands used on the server for the current user. You get the list in chronological order. So they should be so that they were entered in. There are additional flags that you can also pass into the history command to help filter or better understand the results.

Shortcuts for History Command

Let's go over some of the simple shortcuts you can use to get started with. The history command allows you to run previously used commands on your account. There are three parts to the command. Firstly is the Event Designator. Secondly, after is the Word Designator. Lastly, would be the Modifier.

History Command Event Designators

Let's start with the basics. To run the last command you used, enter:

!!

You can also run a command that was much earlier in the history list. To do so, enter the following, where n is the line you wish to run:

![n]

To run the last command that used a specific string, enter:

![string]

History Command Word Designators

You can further filter down the commands that you use using Word Designators, added to the Event Designators above. Here is a shortlist for some of the Word Designators. For the full list, check out the man page for the history command.

  • 0 (Zero) – returns the zeroth word, which is typically the command
  • n – returns the nth word of the selected command
  • ^ – returns the first argument for the command
  • $ – returns the last word, usually an argument
  • * – returns all words except for the zeroth word of the command

Additionally, you can also add Modifiers to filter the command further. These can also be found in the history manual, which you can access with:

man history

Example History Commands

Let's start with the basic command and build up from there. To start, lets simply run:

history

Example of History Command output.

Here you can see the commands that ran as the user that I was logged in as. Notice that while I was logged into root, the commands ran as they are not shown until I returned to the user.

The History command, of course, can also be used with piping, either sending information to a file or displaying filtered text to the chat window.

history | grep sudo

History Command with Piping

Using Event Designators

Basic commands were mentioned above but let's review them in a bit more action instead. Due to my history list, which is noted above. If I want to check for updates, rather than typing out the full command, I can type ! 1 or ! 4

Example for History Command Event Designators

However, the Event Designator can also be combined with another command. Since I ran the history command earlier, as the 11th value, I can also check its manual page by running the following.

man !11

Using Word Designators

Let's review how to use Word Designators. You would use Word Designators to select specific parts from the command. Using the $, I can reference the history.txt file from the event designator, which references the previous command. Add a Word Designator, add a colon (:) after the Event Designator, then enter your Word Designator.

Example of History Command Word Designators

Let's go for something a bit more involved with the history command. Below, we will review what history is currently present on the server and try to get a printout of all of the commands mentioned sudo. That way, we know what commands our user is running as the root user.

To start, we have a display of the current history and a review of the list of the commands that have used sudo. Using these lists, we come up with the following command:

!13 !!:* > !cat:$

Running command "!13 !!:* > !cat:$" with History Command Event Designators

Using the command above, which only uses the history's Event Designators and Word Designators.

  • !13 – This grabs the word history from line 13.
  • !!:* – This pulls the last command used and omits the first word
  • ! cat:$ – This grabs the last command to use cat and uses the last word.

While I could have just completely not used the ! 13 commands and left the !! Command on its own; using the 2 different commands shows the flexibility of the Event Designators and Word Designators commands.

Written by Brianna Flores  /  March 9, 2020