Rate this page del.icio.us  Digg slashdot StumbleUpon

Tips and Tricks: Easy and safe bash history searches

by the editorial team

Contributed by Zak Brown

Often the command you need is in your history, how do you find it?

One simple method is to run the history command and pipe it through grep.

$ history | grep cat
110  cat /tmp/foo

You can then run the command by typing ! and the history line number:

$ !110
cat /tmp/foo

Another method is to use the built in history search feature. In this case you
type !? and then the search term. This searches back into the history
for the last instance of the search term and executes it:

$ !?cat
cat /tmp/foo

However, this might be dangerous, what if the last instance of your search term
did something unexpected that would damage the system?

$ !?foo
cat /tmp/foo > /dev/hda

Ouch! A better solution is to search for the term and append it to your history
without executing. This time we close the search with ?:p

$ !?foo?:p

This will echo the command and place it as the last item in your history without executing. If it is in fact the command you wish to run, just hit the UP ARROW and ENTER

The information provided in this article is for your information only. The origin of this information may be internal or external to Red Hat. While Red Hat attempts to verify the validity of this information before it is posted, Red Hat makes no express or implied claims to its validity.

16 responses to “Tips and Tricks: Easy and safe bash history searches”

  1. Corba the Geek says:

    What about good old Ctrl-R?

  2. Joshua Gimer says:

    I also find the following two shortcuts usefull:

    !! - will execute the last executed command:

    cat /etc/host
    !!s - will run cat /etc/hosts

    !$ - will place the last argument on the last executed command in the current position.

    cat /etc/hosts
    cat /dev/null > !$ - will run cat /dev/null > /etc/hosts

  3. Alex says:

    I much prefer to use reverse-i-searches. Hit ^r, type in a string and it will automatically complete it using the latest matching line in your history. Hitting ^r again will search backwards to the next matching line.

  4. Yann says:

    hey…. I haven’t used these history features since the early nineties.

    CTRL-R (or shift-CTRL-R to go backwards) is your greatest friend.

    oh, and thanks RedHat for the awesome software and efforts you guys and gals have made… here’s to 2008, keep it up.

  5. Alex says:

    Whoops, I guess Corba beat me to it.

  6. Yann says:

    he he… me too

  7. Cathe Rine says:

    Credit card has revolutionized the way shopping was done. This is basically money in plastic form. Importance of this card depends upon its use. Youngsters of today use this card as a status symbol where as a family member use them for their convenience. In whatever form you are using this card you cannot ignore the importance of it. And why should you ignore this if some one is giving loan on this basis.To find cash advance loans, payday loans, cash advance payday loans, cash till payday visit http://www.cashadvancepaydayloans.org.uk

  8. TS Urban says:

    Or use zsh :)

    !text

    Will complete the history match starting with text before you have to hit enter.

    Otherwise, like others, I find the search mechanism useful, and also an alias:

    hg=’history | grep’

    because really sometimes you just want to search.

  9. Tom says:

    I didn’t know about that R thing. That’s cool. Quick question:

    If I type in a search string and then continue pressing R to go through the choices, if I accidentally go past the one I want, can I change directions and come back to it without restarting the whole search?

  10. Tom says:

    I mean CTRL-R. I used angle brackets and I guess it was taken as code.

  11. rhuser says:

    Ctl-s is the incremental forward search, which will allow you to go in the other direction if you pass your command in the Ctl-r search (hit ctl-s twice).

    Also, Ctl-s may be grabbed by the terminal, I had to “stty -ixon” to test this.

  12. Yann says:

    As mentioned in my comment above…

    use ctrl-r to search forwards and use shift-ctrl-r to go backwards

    It’s habit really… my colleague on the next desk insists on using the arrow keys, but it works for him.

  13. Gerhardus Geldenhuis says:

    One of the best books you can read to improve your sysadmin knowledge is OReillys Bash Guide. There is a treasure trove of bash shortcuts available. After reading I realized the little I actually make use off.

  14. Zak Brown says:

    Yep, CTRL-R is a great way to do the same thing. I personally don’t like it because it has a captive interface.

    zsh is indeed a great shell with great features. I started using it because of it’s great tab completion features. I’ve since gone back to using bash with the bash-completion package installed.

  15. Raja says:

    I love CTRL-R, solves all problems :D

  16. Felipe says:

    I like to use the CTRL-X-E to edit multiple lines…

    but dont forget to set your preferred editor:
    #export EDITOR=vim

Leave a reply