Can’t remember the Linux command you just ran? Here’s how to repeat a command


The story repeats itself. At least as far as the Linux command line interface (CLI) is concerned. Although modern Linux distributions don’t necessarily require users to open a terminal window (which is a good thing), if you ever decide to adopt Linux as your server operating system or just want to get more 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 is second nature to me. That’s why I run a lot of commands. Sure, I could do whatever I need with a GUI, but sometimes 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 day before. Fortunately, under Linux, there are several ways to find it. I tend to use two methods. The first is to use the up and down arrow keys to scroll through the last commands I executed. While this method is the one I use most often, sometimes I have to go back so far in my history that it’s not exactly the most efficient use of my time.

According to the man page for the “history” command (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 to use the information of the previous lines to compose new ones”.

A better description would be that the “history” command prints a line-by-line history of the commands you have previously run. By default, 1,000 orders are kept. You can even check it using the command:

echo $HISTSIZE

The output should be simple:

1000

How to use the history command

1. Open a terminal window

First, open your default terminal window or connect to your Linux server.

2. Run the history command

The history command is very simple to use. By default, it reads the ~/.bash_history file and displays its contents in the terminal. To display it, run the command:

history

You can then browse the output to find the command you need.

3. Change the number of orders to keep

Suppose, however, that 1000 orders are too many for you to sift through. You can change the number of entries using the export command.

Suppose, for example, that you want to reduce the history limit to 500. The commands to do this would be:

export HISTSIZE=500

export HISTFILESIZE=500

4. Ignore Duplicates

You can also configure the history to not print duplicate orders. To do this, run the following command

export HISTCONTROL=ignoredups

5. Limit the number of exits

Suppose you only want to see the last 10 orders. To do this, use the history as follows:

history 10

Or :

history 25

Gold :

history 100

You understood.

When you find the command you’re looking for, highlight it, press [Ctrl]+[Shift]+[C] to copy the command and on [Ctrl]+[Shift]+[V] to paste it back into the terminal.

Simplify search with grep

Let’s say you have run a particular 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 commands that include nano by passing the history output through grep, like so:

history | grep nano

The output will only contain nano commands.

Piping history output through grep. Screenshot by Jack Wallen/ZDNET.

This is how Linux can remind you of the command you’re looking for, without you having to spend too much time or energy remembering what you did two or three days ago in the programming interface.

Source: ZDNet.com





Source link -97