Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I remove a certain line from history's database?

share|improve this question
5  
Please don't write "Thanks" comments all over the place. Upvote answers you find helpful and accept the ones that helped you most. This is the way to say thanks here. – slhck Jan 31 '12 at 16:33

6 Answers

up vote 12 down vote accepted

Just edit the file ~/.bash_history.

share|improve this answer
1  
If the HISTFILE environment variable is set, the history file isn't ~/.bash_history but rather ${HISTFILE}. – Karolos Apr 3 at 22:34
history | sed -i 59d

59 is the line number. Cannot be anything sweeter than this :)

share|improve this answer

If you need to remove several lines at the same time I normally use this:

history | grep <string> | cut -d ' ' -f 3 | awk '{print "history -d " $1}'

If you need to remove the last command you can use:

history -d $((HISTCMD-2))
share|improve this answer
This looked like it worked but history | grep <string> still shows all the lines it claimed to delete... – dirt Apr 19 at 0:09

If you need to remove a range of lines from history, the following bash function could save you some time:

function histdel() {
    [ $# -eq 1 ] && history -d "$1" && history -w
    if [ $# -eq 2 -a "$1" -le "$2" ]; then
        for n in `seq "$2" "$1"`; do
            history -d "$n"
        done
        history -w
    fi
}

Function should be typically added to $HOME/.bashrc. To use the function imediatelly, you will need to have the file read again by your running shell (. $HOME/.bashrc). Then to delete e.g. commands 200-210 from history:

$ histdel 200 210

(Note: This question is among the top search results if you search for deleting a range of commands from bash history. So, while the above is more than what the question asks, it could be useful for some readers.)

share|improve this answer
1  
Alternative forms for the for statement: for n in $(seq "$2" "$1") is stylistically preferred by some, and, if seq doesn’t work, try for ((n="$2"; n>="$1"; n--)). – Scott Mar 13 at 18:11

To prevent a command from being added to the history in the first place, make sure that the environment variable HISTCONTROL contains among its colon-separated values the value ignorespace, for example (add e.g. to .bashrc):

$ export HISTCONTROL=ignorespace

This will prevent any command with a leading space from being added to the history. You can then clear the history completely by running

$  history -c -w
  ^-- additional space character
share|improve this answer
Wow, that is amazing. Never have tried/heard of this option – w0rldart Feb 1 '12 at 23:25
+1 for the good tip – Felipe Alvarez Dec 10 '12 at 4:51

Typing "history -d xxx" will delete a specified line. You then have to type "history -w" to make them permanent. It also removes them from the .bash_history file. The surest fire way is cYrus's answer.

share|improve this answer

Your Answer

 
discard

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.