Do you remember this command that solved your problem? Was it cat, less, more, wc or something else? When we are at the terminal, we can issue dozens of commands to solve a problem and in the background, our Linux operating system records these commands in a history file.
In this guide, we’ll look at different ways to find and reuse our order history. While you’re getting used to these commands, it’s important to check that you’re not unintentionally re-running a command that could cause problems. Take your time using these new techniques and check the details before hitting enter!
All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through the terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
The easiest way to browse your recent orders is to use the at the top and down arrow keys on your keyboard to scroll through previous commands. If you want to restart a command found, simply press the key Walk in key.
Viewing Your Command History in Linux
the the story The command, in its most basic use case, lists and annotates the last 1000 commands issued in the terminal emulator. Each command is associated with a number.
1. Run it the story order to see a list of the last 1000 orders. You will see that all historic orders listed are given a unique reference number.
2. Rerun the history command but limit the number of results to a specific number. This is useful if you know roughly when a command you are looking for was issued. You should only see the last 20 results listed.
Redo a previous command
Now we can use the story to view our previous orders, we may choose and reissue an order using the number assigned to the the story results.
1. Course story 20 To create a list of commands, choose a command to reissue, ensuring that the chosen command is safe to execute. Choose a simple command like Music CDs (1660) is a good sure example. Note that there is no space between the exclamation mark and the order number.
history 20 !1660
Improved Linux History Search Using Grep
By channeling the Release the story in grep we can search our order history returning results for a specified term or string. This is a great approach to finding a partially remembered command.
1. Search for a specific term using the story and grep. We used the example search term “silhouette” because we remembered that we had issued some commands to fix a problem with a silhouette vinyl cutter. Replace that search term with something appropriate for your machine.
history | grep silhouette
Using a Reverse Lookup in Linux Command History
Another handy approach to recovering previous Linux commands is to use the reverse lookup feature built into the terminal. To enter this mode, simply press ctrl and r. You can then enter a search term and use repeated presses on ctrl and r to go back in the list of previous commands containing this term. When you find a command you want to reissue, press Enter.
1. Hurry ctrl and r switch to reverse lookup modeyou should see the prompt read now (reverse search)`’:
2. Type a search term and you should see the last issued command containing that term. For example, we added the search term sudo to view previous commands issued with sudo privileges.
3. Repeat by pressing ctrl and r to browse other results.
4. Execute a previous command by pressing enter or exit reverse search by pressing Esc.
Quick reissue of previous Linux command
Often we will just want to re-run the last command we issued. We can achieve this simply by using the !! ordered.
1. Run it ls command to set this as an example to test.
2. Reissue the last command using !!. Note that the previous command is listed and executed.
Sometimes we may try to reuse a command that requires elevated privileges, such as editing a file outside of our home directory. To do this, we can precede the previous command with sudo. In the following example, we add the first ls command to restart with sudo.
Hide your commands from Linux history
There may come a time when you need to keep a command out of your history, and if this scenario occurs, all you have to do is precede the command with a single press of the spacebar.
For example, here are two ls commands, the second has a single space, hiding it from the history file.
With a little practice, all of the above approaches become quite instinctive to use and can make your terminal session more powerful and efficient. The ability to locate and reissue commands is extremely useful, especially when retrieving a rarely used command or a command that was difficult to create initially.