Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Other than viewing the history, is there a way to filter my history?

Say I want to search for a command that started with "ssh"?

share|improve this question

7 Answers 7

up vote 37 down vote accepted

Press Ctrl+R and type ssh. Ctrl+R will start search from most recent command to old one (reverse-search). If you have more than one command which starts with ssh, Press Ctrl+R again and again until you find the match.

share|improve this answer

If you just want to search your history, you can just use history | grep ssh, substituting ssh for whatever you want to search.

share|improve this answer
    
I use this method, because I link to see in list every command matching my filter. –  Pisu Nov 9 '11 at 14:40

Pressing Ctrl+R will start "reverse-i-search" mode, typing "ssh" would search your history for commands which contain "ssh".

share|improve this answer

I do a slight variation of the above, works well for me (if you're referring to your bash history

In my home folder I create a file named

.inputrc

Inside goes this

"\e[5~": history-search-backward
"\e[6~": history-search-forward

Note: the above doesn't seem to work anymore in 14.04 so this does the same thing -

"\e[A":history-search-backward
"\e[B":history-search-forward

Then typing however much of a previous command I wish & using the page up/page dn buttons searchs the history, always starting with page up

share|improve this answer
    
if you have root permisions, you can edit /etc/inputrc soo this will work for all users. –  Wolfy Nov 2 '11 at 16:14

Here's another method using classic commands (more likely to work across distros). Command history is stored in the file .bash_history in your home directory, so you can do this:

grep "ssh" ~/.bash_history

Don't forget the -i flag if you need case insensitive search.

share|improve this answer

I found the following function somewhere on the Internet and have used it to great effect. Put this in your ~/.bashrc:

hgrep () { 
    history | egrep --color=auto --recursive "$@" | egrep --color=auto --recursive -v "hgrep $@"
}

Now re-load your shell: exec bash. You now have a new command, which you can use like so:

hgrep ssh

It will show you a list of matching commands from your history. To run a command, type ! followed by the command number. Here's an example:

~:$ hgrep scp
  207  tn scp foreign-teachers __HOST__:unity.log __HOST__:compiz.log .
  421  tn scp scott-laptop __HOST__:Scott\ Severance.asc .
  422  tn scp scott-laptop __HOST__:'Scott\ Severance.asc' .
  468  tn scott-desktop scp -r Backgrounds/* __HOST__:Pictures/Backgrounds
  469  tn scott-laptop scp -r Backgrounds/* __HOST__:Pictures/Backgrounds
  470  scp -r Backgrounds/* 192.168.1.2:Pictures/Backgrounds
~:$ !207

I like this approach better than Ctrl+R because it allows much more flexible searches, and I can see multiple results at once.

share|improve this answer

If you use shell in Emacs (M-x shell) you can use M-r (it is equivalent to Ctrl+R in a terminal).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.