History repeats itself. At least as far as the Linux Command Line Interface (CLI) is concerned. While modern Linux distributions don’t necessarily require users to open a terminal window (which is a good thing), if you ever decide to use Linux as your server operating system, or just want more flexibility and power, the command line will be your friend.
I’ve been using Linux for a very long time, so the terminal window has become second nature to me. That’s why I run a lot of commands. Sure, I could do whatever I need with the GUI, but sometimes the terminal is just faster (for me).
However, there are days when I catch myself staring out the terminal window, scratching my head, and trying to remember the command I ran the day before. Fortunately, under Linux there are several ways to find it. I usually use two methods. The first is to use the up and down arrow keys to scroll through the most recently executed commands. Although I use this method most of the time, sometimes I have to go back so far into the past that it’s not the most efficient use of my time.
According to the history command’s man page (which can be viewed with the man history command), “the GNU history library is able to keep track of these lines, associate arbitrary data with each line, and use information from previous lines to compose new lines.”
A better description would be that the “history” command prints a line-by-line history of the commands you have previously run. By default, 1000 orders are saved. You can even test it with the command:
The output should be simple:
How to use the history command
1. Open a terminal window
First, open a default terminal window or connect to a Linux server.
2. Run the history command
The history command is very easy to use. By default, it reads the ~/.bash_history file and displays its contents in the terminal. To display it, run the command:
You can then view the output to find the command you need.
3. Change the number of orders to keep
Let’s assume, however, that 1000 orders are too many for you to sift through. You can change the number of records using the export command.
Let’s say, for example, that you want to reduce the history limit to 500. The commands to do this would be:
4. Ignore duplicates
You can also customize the history so that you don’t print duplicate orders. To do this, run the following command
export HISTCONTROL=ignored duplicates
5. Limit the number of exits
Let’s say you only want to see the last 10 orders. To do this, use history like this:
Did you understand.
When you find the command you want, highlight it, press [Ctrl]+[Shift]+[C] copy command and enable [Ctrl]+[Shift]+[V] to paste it back into the terminal.
Make it easier to search with grep
Let’s say you ran a certain command multiple times with different options. For example, you used the nano editor for a configuration file, but you can’t remember the location of that file.
You can filter out only those commands that include nano by piping the history output through grep, like this:
history | grep nano
The output will only contain nano commands.
Output pipeline history via grep. Screenshot by Jack Wallen/ZDNET.
Here’s how Linux can remind you of the command you’re looking for without having to spend too much time or energy remembering what you did two or three days ago in the API.