/>
X
Tech
Why you can trust ZDNET : ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission. Our process

'ZDNET Recommends': What exactly does it mean?

ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.

When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.

ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.

Close

Can't remember the Linux command you ran earlier? Let history repeat itself

If you use the Linux command line quite a bit, you might find yourself lost in what to type at some point. The history command is there to give your mind a jog.
Written by Jack Wallen, Contributing Writer
Reviewed by Min Shin
Woman using a laptop in an office
Tony Anderson/Getty Images

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: 8 things you can do with Linux that you can't do with MacOS or Windows

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. Thankfully, with Linux, there are a number of ways to locate the command. 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. 

Also: The best Linux laptops you can buy

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.

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 history command

1. Open a terminal window

First, open your default terminal window or log into your Linux server.

2. Run the history command

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:

history

You can then scroll through the output to find the command you need.

3. Change the number of commands to retain

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

4. Ignore duplicates

You can also configure history to not print duplicate commands. For that, issue the command:

export HISTCONTROL=ignoredups

5. Limit the number of outputs

Let's say you only want to view the last 10 commands. For that, use history like so:

history 10

Or:

history 25

Or:

history 100

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: Ubuntu Lunar Lobster could be the surprise hit of 2023

You can filter out only commands that included nano by piping the history output through grep, like so:

history | grep nano

The output will only include only nano commands.

The results of a grep search through history.

Piping the output of history through grep.

Screenshot by Jack Wallen/ZDNET

And that's how Linux can help remind you of the command you're looking for, without having to spend too much time or energy recalling what you did two or three days ago in the CLI. 

Editorial standards