13

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?

  • 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
17

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"
  • 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
8

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.

2

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.

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

    Examples
        hxf "rm -rf"
        hxf ^source

    See:
    - https://unix.stackexchange.com/questions/57924/how-to-delete-commands-in-history-matching-a-given-string
COMMENT
#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)$ ]]
    then
        #Delete all matched items from the file, and duplicate it to a temp
        #location.
        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
    else
        echo "Cancelled."
    fi
}
2

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.
  • I got an error (-bash: history: -d: option requires an argument), but it still worked? – paradroid Jan 15 at 16:13
  • It runs the delete command continuously, until it fails with an error (because there's is nothing more to delete). Just ignore the error. – thelogix Jan 17 at 14:53
1
cat "$HISTFILE" | grep -v "commandToDelete" >> "$HISTFILE" && exit

This worked for me. history -c && history -a didn't reload the history file properly for me. So instead I just exit my session right after rewriting the history file so the one in memory doesn't overwrite it.

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.