304

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"?

N.N.
  • 18,219
Blankman
  • 8,275

11 Answers11

419

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.

Once you've found the match you can press Enter to execute the command or left / right cursor to just select the text of the command.

There is no default reverse option for Ctrl+R to invert the direction of the search but here you will find some suggestions about it.

151

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

ssmy
  • 1,851
19

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

doug
  • 17,026
  • 1
    if you have root permisions, you can edit /etc/inputrc soo this will work for all users. – Wolfy Nov 02 '11 at 16:14
  • 1
    @doug, hmm.. I've Ubuntu 15.04. This solution works but mess up Ctrl+LeftArrow / Ctrl+RightArrow keys. Issue looks like with creating .inputrc file and not with entries in it. – Ravi Jul 14 '16 at 11:12
  • Don't forget to rebind .inputrc or close the terminal – David Brossard Jun 13 '18 at 21:14
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.

13

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

N.N.
  • 18,219
Sergey
  • 43,665
6

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.

4

Put this in your ~/.bashrc :

alias hgrep='history | grep --color=auto '

example:

3

History is good but limited - I prefer to set up my bash environment so that I log all of the commands I have ever run, in addition to the directory in which they were run in. Then I run a command to list all of the commands I have run in the current directory, which I can pipe to grep etc - it's called 'dish' see:

https://github.com/wolfwoolford/dish

If you want to cut to the chase, just source this in your .bashrc (https://github.com/wolfwoolford/dish/blob/master/dishrc)

There's also a really useful command you get for free called 'dishg' - or dish global - which prints out every command ever run, regardless of directory. Obviously this one is only useful when used with grep and tail etc...

I've been using it for years and it's literally the first thing I install whenever I setup a new box. It logs the commands you run into text files in a hidden directory (~/.dish) .. I've never had an issue with disk space...

2

Wanted to write this as a comment to @ssmy but couldn't get the formatting in the comment to cooperate.

After searching through the history with history | grep ssh you can then execute the command you wish by typing its number prepended with an exclamation mark, e.g. !42.

Example:

$ history | grep ssh
  5 ssh me@someserver.net
 13 ssh me@someotherserver.net
 42 ssh me@thisone.com
$ !42
2

I like to grep my history instead of Ctrl+R backwards search. That's because you can only see one line with Ctrl+R. With grep you get more of an overall view of your last interaction with a command etc.

I have the following in my .bashrc . It lets you search the command history by typing hiss KEYWORD . Call it what you like. I like to hiss^^ . Don't forget to reload your .bashrc file with source ~/.bashrc

hiss () { history | grep "$@"; }
Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
1

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

N.N.
  • 18,219