I am trying to delete a specific entry in history. Clarification--by that I mean deleting all occurences containing a given command/or text. I used Aliteralmind's hxf script:


    hxf "rm -rf"

#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."

(from https://unix.stackexchange.com/a/178956/115826)

However after running the hxf script, history gives me: (for example)

401 254: 263: 287: 288: 293: cd ~
402 203: 243: 290: 309: 315: 332: 370: 377: 389: 400: ls
403 200: 207: 255: 263: 302: 325: 359: 378: 385: 393: 397: 399: nedit

How to modify the hxf() script to eliminate history displaying a list of indices for each occurrence of a command? After running hxf() I get the above. Prior to running it, history gives:

401 cd ~
402 ls
403 nedit

The list of entries gets quite long and unreadable quickly. I need for history output to appear as it did prior to running hxf with the only difference being the command(s) I deleted with hxf() now removed.

closed as unclear what you're asking by Gilles, chaos, jasonwryan, dhag, PersianGulf May 19 '15 at 14:54

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This is not a proper way to ask question. You should first explain what the whole problem statement is here itself rather than just giving the reference. Nobody is going to the reference and then read the question there and then the answer and then come back here to read your question. – shivams May 18 '15 at 18:11
  • Please frame your question properly or else it shall be voted for closing. – shivams May 18 '15 at 18:12
  • You are right. My apologies. – peinal May 18 '15 at 21:15
  • possible duplicate of How to remove a single line from history? – G-Man May 19 '15 at 3:35

If you are asking how to delete a certain command from the history:

history -d (number)

Get the number from running 'history'

  • Not exactly. How to delete all occurences of a certain command from the history is the goal. – peinal May 19 '15 at 12:38
  • The answer is still history -d (number). You want numerous instance? history | grep (your string) | awk '{print$1}' | while read i; do history -d $i; done – Baazigar May 19 '15 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.