Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 letter 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 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.

Thanks!

share|improve this question
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

3 Answers 3

up vote 6 down vote accepted

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"
share|improve this answer

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.

share|improve this answer

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 ?

share|improve this answer
    
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. –  Lemur Jul 12 '11 at 7:48

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.