Bash Tip #1 March 3, 2008

Filed under: Bash Tips, Linux, Tips — thepet (Follow on twitter) @ 9:30 pm
bash-tip-1

Reverse Incremental Searching

Bash Tip #1: Reverse Incremental Searching. Quickly jumping through your bash history is super easy to do if you know about reverse incremental searching. I was shocked when I found out this existed with being a Bash lover for so long and not knowing of it.

To try it out:

  1. Open a bash prompt on a server that you have a good size history on.
  2. Press ctrl-r.
  3. Type what you want to search for.

As an example, say you run a web server and periodically you login and update the awstats data. You know, on days when you know traffic is up and you don’t want to wait for the next cron’d awstats update. And since you have it cron’d you haven’t bothered with setting up a script to do these periodic updates, so you’re lazy and just type in the awstats command by hand. Here’s what I do:

  1. SSH to the web server.
  2. Press ctrl-r.
  3. Type: update.
  4. Bash will then show you the last command you entered with the word “update” in it.
  5. If it’s not the right command, just press ctrl-r until you find the right command.
  6. Once you’re on the right command, just press enter.

It’s kind of like having mini scripts saved in your history. You just have to know what to reverse search for in order to bring it up the fastest.

Another example: You just compiled php on a server the other day and it turns out the developers needed gd support compiled in. Since you know the ./configure line is still somewhere in your history:

  1. SSH to the server with php installed on it.
  2. cd to to the php src dir.
  3. Press ctrl-r.
  4. Type this (or as much as you need to for configure lines to show): ./config
  5. If it’s the wrong ./configure line then press ctrl-r until you find the right line.
  6. Once you find the right line press ctrl-e. This should jump you to the end of that line and pop you out of reverse incremental search mode.
  7. Add –with-gd and hit enter.

Once you get hooked you feel uncomfortable in other shells that lack it.

 

1 Comment for this post

 
Alex Says:

Excellent tip! I’ve always thought that should be possible, but I never knew how. Thanks!

Leave a Reply