- Stop plugging these 7 devices into extension cords - even if they sound like a good idea
- I changed these 6 Samsung TV settings to give the picture quality an instant boost
- I tested a 9,000,000mAh battery pack from eBay that cost $10 - here's my verdict
- The 3 most Windows-like Linux distros to try because change is hard
- This 'unlimited battery' GPS tracker is an integral part of my hikes - and it's on sale
How to use the Linux history command – and what it can do for you
History does repeat itself — at least with regards to the Linux command line interface (CLI). Although modern Linux distributions don’t require users to ever open a terminal window (which is a good thing), if you ever decide to adopt Linux as a server operating system or just want to get even more flexibility and power out of the OS, the command line will be your friend.
Also: The first 5 Linux commands every new user should learn
I’ve been using Linux for a very long time, so the terminal window is second nature to me. Because of that, I run a lot of commands. Sure, I could do everything I need with a GUI, but sometimes the terminal is just faster (for me).
However, there are days when I find myself staring at the terminal window, scratching my head, and trying to remember the command I ran the previous day (or even hour). Thankfully, there are a number of ways to locate the command in question. I tend to use two methods. The first is to use the up and down arrow keys to scroll through the last few commands I ran. Although that method is my go-to, there are times when I have to scroll so far back in my history that it’s not exactly the most efficient use of my time.
There’s another way to view those previously run commands. With a nod to irony, the method of viewing previously run commands is yet another command… history.
Also: The best Linux laptops you can buy
According to the history man page (which can be viewed with the command man history), “The GNU History library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones.”
A better description would be the history command prints out a line-by-line history of the commands you’ve previously run. By default, 1000 commands will be retained. You can even verify this with the command:
echo $HISTSIZE
The output should read simply:
1000
How to use the Linux history command
What you’ll need: The only thing you’ll need for this is a running instance of Linux. And because the history command is found on all Linux distributions, it doesn’t matter which one you choose.
First, open your default terminal window or log into your Linux server.
The history command is very simple to use. By default, the command reads the file ~/.bash_history and prints the contents in the terminal. To view this, issue the command:
You can then scroll through the output to find the command you need.
Let’s say, however, that 1000 commands is too many for you to have to scan through. You can change the number of entries using the export command.
Also: How to install Linux on an old laptop
Say, for instance, you want to change the history limit to 500. The commands for that would be:
export HISTSIZE=500 export HISTFILESIZE=500
You can also configure history to not print duplicate commands. For that, issue the command:
export HISTCONTROL=ignoredups
Let’s say you only want to view the last 10 commands. For that, use history like so:
Or:
Or:
You get the idea.
When you find the command you’re looking for, highlight it, hit [Ctrl]+[Shift]+[C] to copy the command, and [Ctrl]+[Shift]+[V] to paste it back into the terminal.
Simplify the search with grep
Say you know you’ve run a particular command many times with different options. For example, you’ve used the nano editor on a configuration file but you can’t remember where the file was.
Also: Why don’t more people use desktop Linux? I have a theory you might not like
You can filter out only commands that included nano by piping the history output through grep, like so:
The output will only include nano commands.
And that’s how Linux can help remind you of the command you’re looking for, without you having to spend too much time or energy recalling what you did two or three days ago in the CLI.