12

I was wondering how to search commands in history without knowing the first few letters of the command?

When searching using Ctrl-R in bash, one has to give the first few letters of the command. What if I only know some characters in the middle, or some at the beginning and some in the middle?

For example, to search cat myfile, I only know there is at in it, or c at the beginning and my in the middle somewhere. keywords have to be continuously positioned. For example, in cat myfile, I would like to search for bothc and my, but Ctrl+R will not allow to specify both simultaneously.

  • 2
    I use Ctrl-R all the time and it searches the middle text automatically. Nothing fancy, but in your example just typing my would be sufficient. RHEL4 and Cygwin confirmed. – Aaron D. Marasco Jul 12 '11 at 1:46
  • @Tim: you didn't actually try that, right? Because it does search for letters in the middle of a command. – alex Jul 12 '11 at 7:26
  • @alex: I actually wanted to say keywords that are not continously positioned. For example, in cat myfile, I would like to search for bothc and my, but Ctrl+R will not allow to specify both simultaneously. – Tim Jul 13 '11 at 16:16
12

If you are just looking to find the line (to jog your memory) you could just grep for the part of the command you remember:

history | grep "substring"
  • 2
    How is it that I've been using *nix systems for 5+ years and didn't know about the history command until now? Thanks! – GMA Jul 4 '17 at 13:00
6

Bash only has a simple string search, as far as I can see.

Consider switching to zsh, which has a history wildcard search. history-incremental-pattern-search-backward and history-incremental-pattern-search-forward aren't bound to keys by default, but you can bind them:

bindkey '^X^R' history-incremental-pattern-search-backward
bindkey '^X^S' history-incremental-pattern-search-forward

Then type Ctrl+X Ctrl+R ^c*my.

0

Apropos(1) should also return a man for the subject if the man contains that word. Whatis(1) returns only absolute matches.

For reference: Apropos == man -k Whatis == man -f

These two should be a great help, along with wildcards * and ?

  • The question is about searching through the command history in the terminal... (not searching the man pages)... but on a completely seperate note, I did find your answer useful, so +1 :) – Peter.O Jul 12 '11 at 7:37
  • ....Ah lovely, my bad on that one mate. – baweaver Jul 12 '11 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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