Skip to main content

A sysadmin's favorite Linux history command line hack

When typing is necessary, it's better to do it efficiently.
Image
Command link hack
Image by Keshav Naidu from Pixabay

Like many people working with Linux machines, I prefer using the command line interface when possible and when it makes sense. In some cases, it is easier to find, inspect, and modify some configurations using your fingers because they just "know" the commands you need to type, and it's quicker than opening a GUI and searching for the sequence of menus that you need to click.

Sure, there are some cases when using the GUI is faster, and you're only doing that task once or twice, so who cares, right? But if it's something that you'll need to do multiple times, maybe with some variations, your sysadmin brain ponders, "Can I automate this?" In many cases, the answer will be, yes, but the effort to automate the task isn't worth it in other situations.

[ You might also like: Linux terminal trick: Hack the cd command to remember where you've been]

If I could just reuse the recent commands that I typed.

History

The simplest option when it comes to recovering the last commands you typed in the Linux console is to use history. In this example, you're working with the HTTP server, and you're repeatedly stopping the service, editing the httpd.conf file, and then restarting httpd to check results in the browser.

The following command will show you the last 10 commands used:

❯ history | tail -10
10057  systemctl stopt httpd.service
10058  systemctl stop httpd.service
10059  sudo systemctl stop httpd.service
10060  sudo systemctl start httpd.service
10061  systemctl status httpd.service
10062  history
10063  systemctl status httpd.service
10064  ps -ef|grep httpd
10065  man httpd.conf
10066  view /etc/httpd/conf/httpd.conf

From there, you can either copy and paste the line (if you need to edit the command or use it in another terminal) or type the exclamation mark followed by the sequence of the command in the history:

> !10061

This line recalls the command systemctl status httpd.service from the history, and then you just press the ENTER key (or edit the command before re-executing).

This is not bad, but you still need to type the sequence or copy+paste with the mouse. Not a huge deal, but you want to focus on your main task.

Some good alternatives

You could recall the previous commands from the history using the UP arrow key. Navigate through the list by using UP and DOWN keys.

This approach is OK, but what if you want to recover something you know you used 15 minutes ago? That will be in the history list for sure, but you may need to do some mining.

For example, you could use history, send the output to grep, and list only those commands that match your needs. In this example, I display all commands in history that include the http string:

> history | grep http
10544* systemctl status httpd.service
10545* systemctl stopt httpd.service
10546* systemctl stop httpd.service
10547* sudo systemctl stop httpd.service
10548* sudo systemctl start httpd.service
10549* systemctl status httpd.service
10551* systemctl status httpd.service
10552* ps -ef|grep httpd
10553* man httpd.conf
10554* view /etc/httpd/conf/httpd.conf
10561* systemctl status httpd.service

So from here, you can use the exclamation mark and sequence number again. For example: !10546

But you may think, "This is OK, but my brain-to-fingers connection is so quick that I do not want to be distracted. I know exactly the command I am looking for."

All right, there is a solution for that, too.

When your brain and fingers know what they're searching for

Continuing with this example, you want to recall commands related to httpd quickly.

The option for you is to use CTRL+R, which will prompt you for a substring. Use that to recall the last command that you typed that contains that string.

As you type the string from your command, the last command that used that string will be displayed. If the command is what you're looking for, press ENTER, and it will be executed.

If at this point you keep pressing CTRL+R, the second last command that contains that search string will be shown, which in this example is the view /etc/httpd/conf/httpd.conf command.

You can navigate backward (CTRL+R) and forward (CTRL+S).

[ Want to test your sysadmin skills? Take a skills assessment today. ] 

Wrap up

When you know part of the command you executed recently, you can use the shortcuts discussed here to recall it quickly. This can be especially useful if you used the command a long time ago and it is still in the shell's history, but your fingers "remember" it.

Obviously, there are more efficient ways to perform your daily tasks than typing: Creating aliases for commands that you use a lot, creating scripts, automating, etc. But when typing is necessary, it's better to do it efficiently.

Check out these related articles on Enable Sysadmin

Author’s photo

Roberto Nozaki

Roberto Nozaki [RHCSA/RHCE]  is a Senior Consultant at Red Hat Canada where he specializes in IT automation with Ansible. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, all available on demand.

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX