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 need to delete all commands in my history matching a string. I've tried:

$ history | grep searchstring | cut -d" " -f2 | history -d
-bash: history: -d: option requires an argument

$ history | grep searchstring | cut -d" " -f2 | xargs history -d
xargs: history: No such file or directory

$ temparg() { while read i; do "$@" "$i"; done }
$ history | grep searchstring | cut -d" " -f2 | temparg history -d
(no error, but nothing is deleted)

What is the right way to do this?

share|improve this question
While there are working answers below, I still wonder why it doesn't work with history -d X. I came across this question because I had just done history | grep search_str | sort -nr | awk '{print $1}' | while read i; do history -d $i; done. No error, but nothing deleted. Anybody can explain why? –  mivk Jun 4 '13 at 17:47

2 Answers 2

The history command just operates on your history file, $HISTFILE (typically ~/.history or ~/.bash_history). It'll be much easier if you just remove the lines from that file, which can be done many ways. grep is one way, but you have to be careful not to overwrite the file while still reading it:

$ grep -v searchstring "$HISTFILE" > /tmp/history
$ mv /tmp/history "$HISTFILE"

Another way is with sed:

$ sed -i '/searchstring/d' "$HISTFILE"
share|improve this answer
Good answer; $HISTFILE could contain spaces, probably worth quoting it. –  Chris Down Dec 7 '12 at 20:50

Michael Mrozek's answer will work if you don't care about removing commands from the current session. If you do, you should write to the history file before doing the operations in his post by doing history -a.

Also, after you have removed the entries that you want from your history file, you can reload it by issuing history -c, then history -r.

share|improve this answer

Your Answer


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.