Americas

  • United States
sandra_henrystocker
Unix Dweeb

Selectively reusing commands on Linux

How-To
Feb 23, 20215 mins
LinuxUnix

Command history on Linux and Unix systems provides a lot more ways to rerun commands than you might know.

Multiple-exposure shot of a clock and calendar pages. [time / past / future / history / what's next]
Credit: STILLFX / Getty Images

The Linux command line allows your system to remember commands that you use—up to a limit set by your HISTSIZE variable. Since each user’s HISTSIZE is generally set to 1000, that means Linux can remember the last 1000 commands you entered and make it easy to reuse them without retyping them.

Unless you configure your history settings differently, all of your commands will be remembered. You can put some restrictions on that behavior if you like. For example, you might not want to save every command you use to read a man page or every time you type pwd to ask where you’re currently sitting in the file system. How to restrict the commands that are remembered and which are not is covered here.

The easiest way to reuse a previously entered command is to type “!!” on the command line. This repeats whatever command you last used. Alternatively, you can press the up-arrow key on your keyboard as many times as needed to return to some command in your history buffer that you want to run again or the down-arrow key if you backed up too far. Press the enter key when the command you want to reuse is displayed with your command prompt.

If the command that you want to reuse is one you entered 500 commands earlier, on the other hand, backing up to it in order to avoid retyping it is probably only a good idea if the command is one you really don’t want to type again or you don’t remember.

There is a better approach, though. It involves using the command history | more to start displaying all of the commands in your command history, pressing the enter key until you see the command you want to reuse in the commands listed, and then entering Ctrl+C to stop the history command. Then type ! followed by the number to the left of the command you want to reuse (e.g., !123).

If you know that the command is one you used recently, you can avoid much of the tedium by using a command such as history | tail -20 to view only recently entered commands and find your the one you want to reuse much more quickly.

Here are some additional ways that you can take advantage of what Linux remembers for you.

Reuse a command by typing a portion of it

One easy way to reuse a previously entered command (one that’s still on your command history) is to type the beginning of the command. If the bottom of your history buffers looks like this, you could rerun the ps command that’s used to count system processes simply by typing just !p.

$ history | tail -7
 1002  21/02/21 18:24:25 alias
 1003  21/02/21 18:25:37 history | more
 1004  21/02/21 18:33:45 ps -ef | grep systemd | wc -l
 1005  21/02/21 18:33:54 ls
 1006  21/02/21 18:34:16 echo “What’s next?”

You can also rerun a command by entering a string that was included anywhere within it. For example, you could rerun the ps command shown in the listing above by typing !?sys? The question marks act as string delimiters.

$ !?sys?
ps -ef | grep systemd | wc -l
5

You could rerun the command shown in the listing above by typing !1004 but this would be more trouble if you’re not looking at a listing of recent commands.

Run previous commands with changes

After the ps command shown above, you could count kworker processes instead of systemd processes by typing ^systemd^kworker^. This replaces one process name with the other and runs the altered command. As you can see in the commands below, this string substitution allows you to reuse commands when they differ only a little.

$ ps -ef | grep systemd | awk ‘{ print $2 }’ | wc -l
5
$ ^systemd^smbd^
ps -ef | grep smbd | awk ‘{ print $2 }’ | wc -l
5
$ ^smbd^kworker^
ps -ef | grep kworker | awk ‘{ print $2 }’ | wc -l
13

The string substitution is also useful if you mistype a command or file name.

$ sudo ls -l /var/log/samba/corse
ls: cannot access ‘/var/log/samba/corse’: No such file or directory
$ ^se^es^
sudo ls -l /var/log/samba/cores
total 8
drwx———. 2 root root 4096 Feb 16 10:50 nmbd
drwx———. 2 root root 4096 Feb 16 10:50 smbd

Reach back into history

You can also reuse commands with a character string that asks, for example, to rerun the command you entered some number of commands earlier. Entering !-11 would rerun the command you typed 11 commands earlier. In the output below, the !-3 reruns the first of the three earlier commands displayed.

$ ps -ef | wc -l
132
$ who
shs      pts/0        2021-02-21 18:19 (192.168.0.2)
$ date
Sun 21 Feb 2021 06:59:09 PM EST
$ !-3
ps -ef | wc -l
133

Reuse command arguments

Another thing you can do with your command history is reuse arguments that you provided to various commands. For example, the character sequence !:1 represents the first argument provided to the most recently run command, !:2 the second, !:3 the third and so on. !:$ represents the final argument. In this example, the arguments are reversed in the second echo command.

$ echo be the light
be the light
$ echo !:3 !:2 !:1
echo light the be
light the be
$ echo !:3 !:$
echo light light
light light

If you want to run a series of commands using the same argument, you could do something like this:

$ echo nemo
nemo
$ id !:1
id nemo
uid=1001(nemo) gid=1001(nemo) groups=1001(nemo),16(fish),27(sudo)
$ df -k /home/!:$
df -k /home/nemo
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sdb1      446885824 83472864 340642736  20% /home

Of course, if the argument was a long and complicated string, it might actually save you some time and trouble to use this technique. Please remember this is just an example!

Wrap-Up

Simple history command tricks can often save you a lot of trouble by allowing you to reuse rather than retype previously entered commands. Remember, however, that using strings to identify commands will recall only the most recent use of that string and that you can only rerun commands in this way if they are being saved in your history buffer.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author