Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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
Careful... histfile is unpredictable, if you have many open shells, it's no telling what exactly will get saved and in what order. Can anyone confirm that already open shells will not rewrite the history they loaded at initialization, when closed? – orion Jan 13 '15 at 17:02

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

Based on Michael and Chris' answers, I came up with the following. Add it to your ~/.bashrc file and then load it with either . ~/.bashrc or source ~/.bashrc.

    Deletes all lines from the history that match a search string, with a
    prompt. The history file is then reloaded into memory.

        hxf "rm -rf"
        hxf ^source

    - http://unix.stackexchange.com/questions/57924/how-to-delete-commands-in-history-matching-a-given-string
#The unalias prevents odd errors when calling". ~/.bashrc" (May result in
#"not found" errors. That's okay).
unalias hxf
hxf()  {
    read -r -p "About to delete all items from history that match \"$1\". Are you sure? [y/N] " response
    response=${response,,}    # tolower
    if [[ $response =~ ^(yes|y)$ ]]
        #Delete all matched items from the file, and duplicate it to a temp
        echo -e "grep -v \"$1\" \"$HISTFILE\" > /tmp/history"
        grep -v "$1" "$HISTFILE" > /tmp/history

        #Clear all items in the current sessions history (in memory). This
        #empties out $HISTFILE.
        echo "history -c"
        history -c

        #Overwrite the actual history file with the temp one.
        echo -e "mv /tmp/history \"$HISTFILE\""
        mv /tmp/history "$HISTFILE"

        #Now reload it.
        echo -e "history -r \"$HISTFILE\""
        history -r "$HISTFILE"     #Alternative: exec bash
        echo "Cancelled."
share|improve this answer

For those looking for a one-liner:

while history -d $(history | grep 'SEARCH_STRING_GOES_HERE'| head -n 1 | awk {'print $1'}) ; do :; history -w; done

So, if you wanted to eg. delete multiple lines with a password in it, simply replace "SEARCH_STRING_GOES_HERE" with the password. This will search your entire history for that search string and delete it.

2 things to be aware of

  • grep uses regex unless you supply -F as an argument
  • The command will show 1 error, once there are no more matches. Ignore it.
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.