07 February 2009

bash history grep alias

In a previous post, I introduced many useful bash shell aliases that I use. There's one that I've made a notable improvement to that I'd like to share. If you spend a lot of time on the command-line and use bash, you should really try this out.

'bhg', for "bash history grep," was a pretty simple alias to search through your bash history file. Previously, I used it like this:
alias bhg='cat $HISTFILE | grep'

The deficiency I found with it is that I often keep a shell open for days (even weeks!) at a time. So, I may be searching for a command that has not been written to the history file. What I'd like, then, is one 'bhg' alias that will find any command from my current shell history or my history file. The bash 'history' built-in will give you all of the current shell's command history. We just need to strip out the line numbers -- easy enough with 'sed' -- and then con-'cat'-enate that with the history file and we're in business. So, this is what I use now to accomplish all that:
alias bhg="history | sed 's/^ *[0-9]* *//' | cat $HISTFILE - | grep "

As I mentioned before, if you find this useful, you can make it even more useful by increasing the history size variables in your ".bash_profile". Namely, 'HISTSIZE', which controls how many lines of current history will be available via the history command, and 'HISTFILESIZE', which controls how many lines of command history will be written to your history file. They both default to 500, I think, which I've found to be way too small for my purposes.

Labels:

4 Comments:

At 5/4/09 09:13, Blogger dericbytes said...

The following alias prints history numbers allowing you to run the command once you found it. i.e.

# run history command 301
!301

alias hg='history -a; history -n; history | grep'


history -a - write the current terminal's history to the history file

history -n - re-read the history from the history file


If your not bothered with history numbers and the order they were called you can use the sort and uniq program to filter out duplicate history commands

alias hgu='history -a; history -n; cat /home/dericbytes/.bash_history | sed '\''s/^ *//g'\'' | sed '\''s/ *$//g'\'' | sort | uniq | grep'

 
At 5/4/09 16:41, Blogger dericbytes said...

Check out my variation of bhg

- it runs history if no parameters.
- it writes its results to the history file allowing the history to be accessed in other shell before this shell has closed
- it removes duplicate entries from the history file while keeping its order
- it removes calls to history from the history file, because they are of no use to me.


http://dericbytes.blogspot.com/2009/04/grepping-history-and-removing-duplicate.html

 
At 8/4/09 23:43, Blogger kace said...

Nice. ... I sometimes do care about order. For example, I might want to see a series of commands, and so I'll call 'bhg' with the grep '-C <N>' option, which gives me N adjacent lines of context for the matching lines. I need sorting less often, so just roll with the one alias and then pipe it through sort and uniq as needed.

 
At 11/9/09 00:26, Blogger Jadu Saikia said...

Nice one. HISTTIMEFORMAT is also a good option with history.

http://unstableme.blogspot.com/2009/01/linux-command-line-history-with.html

 

Post a Comment

<< Home