Improving on history—the Linux history command, that is

Ready to make the Linux history command work harder to serve your needs? Let's look at some new tricks you might want to shove up your sleeve.

history bill stilwell
Credit: Bill Stilwell via Flickr (CC BY-SA 2.0)

The Linux history command allows users to repeat commands without retyping them and to look over a list of commands they've recently used, but that's just the obvious stuff. It is also highly configurable, allows you to pick and choose what you reuse (e.g., complete commands or portions of commands), and controls what commands are recorded.

In today's post, we're going to run through the basics and then explore some of the more interesting behaviors of the history command.

The basics of the Linux history command

Typing "history" and getting a list of previously entered commands is the command's most obvious use. Pressing the up arrow until you reach a command that you want to repeat and hitting enter to rerun it is next. And, as you probably know, you can also use the down arrow. In fact, you can scroll up and down your list of previously entered commands to review them or rerun them.

Since commands in your history display are numbered, you can also enter a command such as !11 to repeat a command—in the case, the one in the 11th line of the history display.

That's the easy stuff.

Configuring history

What you might not know is how many aspects of the history command's behavior you can control. You can determine how many commands are recorded (HISTSIZE), what file your commands are stored in (HISTFILE), whether sequentially repeated commands will show up in your command history once or as many times as they're entered (HISTCONTROL), and whether some commands are not recorded at all (HISTIGNORE). You can also enter a command (history -c) to empty your history buffer and select the time format that you'd like to see displayed.

The commands shown below do all of these things. These commands:

  • Set the size of the history buffer to 100 (many admins choose to save 500 or 1,000 commands)
  • Select the file to be used to store the commands you use (going with the default is generally best)
  • Prevent the recording of sequentially repeated commands
  • Remove previously repeated commands
  • Prevent the recording of commands that are preceded by blanks
  • Prevent the recording of specified commands
  • Set the time format to be used when history is viewed

Here are examples of the settings you might usee in your ~/.bashrc file:

HISTSIZE=100
HISTFILE=~/.history
HISTCONTROL=ignoredups
HISTCONTROL=erasedups
HISTCONTROL=ignorespace
HISTIGNORE="history:pwd:date:ls:ls *:man *"
HISTTIMEFORMAT="%h %d %H:%M:%S> "

The HISTIGNORE setting can be very helpful in restricting the commands that are remembered to just those that are important to tracking your activities and those of other users on your system. There's probably little value in recording that someone looked at a man page, listed files, displayed the date/time, or asked the system what directory they're sitting in. Knowing that they edited some particular file or issued a command that shut some particular service down is another matter.

While we can choose how time is displayed when we use the history command, that is not how it is stored. Instead, you'll see time stored as "epoch time" inside the file. The only issue for HISTTIMEFORMAT is how that time value is displayed when you use the history command.

#1497576143
vi .config
#1497576172
rm .config-

Picking and choosing what you repeat

Linux history does not restrict you to repeating entire commands. You can reuse portions of commands by using some special arguments to represent the portions of commands that you want to reuse.

For example, to reuse the last argument in the previously entered command, use ! (previous command) followed by :$ as shown in the example below.

$ echo one two three
one two three
$ echo !:$
echo three
three

To reuse the first argument, try !^.

$ echo !^
echo one
one

To reuse a particular argument, provide its position in the list of arguments.

$ vi !echo:2
vi two

And please note that in the last example above, we're plucking an argument not from the last command entered, but the last echo command entered. This works for any of the argument positions just discussed.

Wrap-up

I should probably end this post with some comment about being doomed to repeat history, but the history command provides too many useful options for anything so gloomy. I hope you've picked up a trick or two that makes your use of this command more rewarding.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Related:
Must read: 10 new UI features coming to Windows 10