Need help? Chat now!
Search results for:
Table of Contents
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.
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.
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:
To run the last command that used a specific string, enter:
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.
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:
Let's start with the basic command and build up from there. To start, lets simply run:
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
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.
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.
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