Episode #32 - CLI Monday: history

Loading the player...

About Episode - Duration: 8 minutes, Published: 2014-08-11

In this episode, we are going to cover command history. If you spend any amounts of time at the command prompt, being able to recall complex or obscure command options from the history, should result in a productivity boost.

Download: mp4 or webm

Get notified about future content via the mailing list, follow @jweissig_ on Twitter for episode updates, or use the RSS feed.

Links, Code, and Transcript


In this episode, we are going to cover command history. If you spend any amounts of time at the command prompt, being able to recall complex or obscure command options from the history, should result in a productivity boost.

So, what is command history? Well, the shell we use to run commands has built in functionality to track executed commands on a per user basis. I should mention that we will be focusing on bash here, since it is the default shell for most distributions use, however most shells have similar functionality.

The command history can also be a great resource to review recent activity, say for example, how did you grow that filesystem again, and what options did you use? Ideally, you would have a recipe written up on a wiki some place, but the command history work wonders in a pinch, because lets be honest, it can be hard to remember options and switches for tools we use infrequently.

Lets go ahead and look at how the command history works at a basic level. Say for example, in your day to day work you are plugging away at the command prompt, running commands like ls with some options, ping with some arguments, uptime, and finally ps with some options.

In the background the shell is keeping track of these commands. Lets look at a couple methods for extracting data from the history. First, you can run the history command, and as you can see it outputs a listing of commands we have run.

history

But the command history is also saved to disk, by default it is located in each users home directory, in a file called .bash_history, and by displaying the contents of this file you can see it is consistent with the history command.

cat ~/.bash_history

Well, that is the command history in a nutshell, pretty simple right. But, lets dig a little deeper, by checking out the configuration files, along with common usage patterns for extracting bits from the history.

There is actually an amazing online bash manual and I highly recommend checking it out. If you spend a significant amount of time at the command prompt, there are some really interesting tips in here, that will likely save you tons of time. There is actually a chapter dedicated to using the command history and it covers what I am about to show you in great detail. You can find links to both these pages in the episode notes below.

Lets jump back to the command prompt for a minute. Before we dive in, I just wanted to mention that I am going to be broadcasting my keystrokes onto the screen. They will appear up here, in top right hand corner via this black box, as you can see, I am typing testing 123. The follow examples will use some arrow and control keys, so I just want to make sure this comes across in the video.

Okay, lets get on with the demo. So, I already showed you a couple ways to review the command history, via the history command and the bash_history file. But, there is actually a much easier way though, and it is probably the most common method, that is to simply scroll through the history using arrow keys. As you can see, as I am pressing the up and down arrow keys we are rolling through the command history. This is extremely handy for rerunning commands in the same session while working on a task.

But what about commands you ran many months ago? Well, for that type of thing you can search the history by pressing control+r, like this. You can see the prompt changed to a reverse search prompt, lets test it out by typing a command we ran in the past. Notice how we typed hist and the prompt automatically guessed what we were looking for. Now you can simply hit enter here, and it will run the command. But you can also drop to the prompt with the command and modify it as needed, lets try that, by running control+r again. So, lets type his, and you can see it guessed the correct command again, but you can hit the right arrow key and it will drop you to a prompt, say if you wanted to add some switched to the command or something. I should also mention, that while you are at the search prompt, you can continually hit control+r to cycle through potential matches.

I am going to be honest with you here, in that I rarely use control+r, simply because I feel it offers a limited window into the command history. I often find myself running the history command and greping the output, something like this. Just that you get a larger view right away vs a single line search result that you have to cycle through.

history | grep ping

What is interesting, is that while doing research for this episode, I ran across software that helps address this issue. It is a piece of software called bash suggestion box, over on github, linked to in the episode notes below. This seems to be a sweet spot between using the control+r search feature and getting lots of output at once. I have only briefly played around with this software but it looks good so far.

In the history output you will notice we have numbers associated with each command. Well, you can something called event designators to reference a line in the history and do something with it. Lets take item number two for example, we can use exclamation mark or bang as it is commonly called, and then enter the line number. When we hit enter, the shell knows that we want to run command number 2 from the history since we used this special event designator.

!2

You can also bang bang, this mean we want to run the previous command.

!!

This comes in handy if you want to do something like this, sudo bang bang, in that we take the previous command and run it as root. I do not want to go to crazy about this as the rabbit hole goes pretty deep, but if you are interested in event designators check out the manual page, linked to in the episode notes below.

sudo !!

I just want to chat about how to change the default behaviour of the command history via configuration files before we finish off this episode. As you can probably guess by now, the manual is a great place to find the configuration options. But lets cover where the configuration settings live and how to change them.

So, we are sitting here in our home directory. I already chatted about here the history log file lives, but where does the configuration live? Well, you can modify that via the bashrc file. Lets have a look in there. This block here talks about how to configure the command history. These first two values, control the amount of history saved in memory and on disk, these are the defaults, but I highly recommend changing histfilesize to something large, like 20 thousand for example. This next one is something I just added, this allows you to timestamp history events, so that you can know when something was run. Finally, you can add additional bits here, for example the histignore option, this allows you to ignore various common commands, things like ls and cd for example. These would be commands that you run often, but do not want to clutter up your history file. Lets just exit out of here and let me show you the updated history output with our new timestamp option set.

So, at this point you should know what the command history is, along with how to use the arrow keys to navigate recently run commands, how to search the history using control+r. Finally, you should be able to find and modify the default behaviour via configuration tweaks if needed.