grep history (gp)

I wrote a little bash function to help me find past commands that I have run. It just greps history and comes back with unique entries. I am sure there is a better way to do it (that has been done already) but this is my way.

function gh ()
{
  old="bob"
  history |sed -e 's/^ *[0-9]* //' | grep $1 |sort | while read line; do
    if [ "$line" != "$old" ]; then
      echo $line
    fi 
    old=$line
  done
  }

after putting this in my .bashrc

I can now do things like
>gh cd
and it will list all of the directories into which i have changed

Comments

I think you can do something with ctrl-r to do some interactive history stuff. But I never do :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p><a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><img><blockquote>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.