Search The Hostwinds Guides Knowledge Base

History Command for Linux

Share This Article [TheChamp-Sharing]

If you have ever used a Linux Server, you know that there are many commands that are important to remember. With the wide range of commands that are available to you it can be hard to remember them all. For example, the one you ran not 2 minutes ago that 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 in order that they were entered in. There are additional flags that you can also pass into the history command as well, to help filter or better understand the results.

Shortcuts for History Command

Lets 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

Lets 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 short list 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 further filter the command. These can also be found in the history manual which you can access with:

man history

Example History Commands

Lets 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 were ran as the user that I was logged in as. Notice, while I was logged into root the commands ran as them are not shown until I return 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 lets 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 simply 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

Lets review how to use Word Designators. You would use Word Designators to select specific parts from the command. Using the $ I am able to reference the history.txt file from the event designator which references the command from before. To add a Word Designator, add a colon (:) after the Event Designator, then enter your Word Designator.

Example of History Command Word Designators

Lets go for something a bit more involved with the history command. Below, we will review what history is currently present on the server. What we will try to get a print out of all of the commands that have mentioned sudo. That way we know what commands our user is running as the root user.

To start, we have a display the current history, as well as review 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 command and simply left the !! command on its own; using the 2 different commands is just to show the flexibility of the Event Designators and Word Designators commands.

Related Articles