Take the 2-minute tour ×
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

10 Answers 10

up vote 49 down vote accepted

Just edit the file ~/.bash_history.

share|improve this answer
9  
If the HISTFILE environment variable is set, the history file isn't ~/.bash_history but rather ${HISTFILE}. –  Karolos Apr 3 '13 at 22:34
10  
Entries are written into the history file when you exit the shell. Therefore, after entering a command that you'd like to remove, you need to either log out and back in, or use 'history -d xxxx' to remove them from the current shell session. –  harmic Jan 24 '14 at 0:19
1  
but how would you delete the record of editing the history file? –  chiliNUT Jul 23 '14 at 19:54
1  
@chiliNUT: Just start your command (e.g. vim ~/.bash_history) with a space; commands starting with a space usually don't get logged (see $HISTCONTROL). –  cYrus Jul 23 '14 at 20:34
    
I don't think thats the case in my distribution, but in general that makes sense –  chiliNUT Jul 23 '14 at 22:29

You can achieve removal from the history file using the commandline in two steps:

  1. Typing history -d <line_number> deletes a specified line from the history in memory.
  2. Typing history -w writes the current in-memory history to the ~/.bash_history file.

The two steps together remove the line permanently from the in-memory history and from the .bash_history file as well.

share|improve this answer
2  
how to delete the last history -w from history? –  KrIsHnA Mar 5 at 9:01
    
@KrIsHnA edit ~/.bash_history manually –  Alexander Myshov May 23 at 6:51

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

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
2  
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 '13 at 18:11

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... –  Brock Hensley Apr 19 '13 at 0:09
    
This will delete the wrong history lines after deleting the fisrt!! –  mivk Dec 20 '13 at 14:51
    
But fortunately, it only prints the wrong delete commands. The following would print the correct delete commands: history | grep XYZ | grep -v grep | tac | awk '{print "history -d", $1}' –  mivk Dec 20 '13 at 15:17
history | sed -i 59d

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

share|improve this answer

First of all, if the command you're about to issue is sensitive, unsafe, or you just don't need it cluttering up your history, it is best/quickest to just prevent it from entering the history in the first place. Make sure that $HISTCONTROL contains ignorespace:

(bash)$ echo $HISTCONTROL
ignoredups:ignorespace

Then proceed any command you don't want in your history with a space:

(bash)$  sensitive-cmd-with --password 82cf7dfe
(bash)$  rm -r... # One-off recursive remove; mustn't be accidentally repeated!

If you accidentally put an unwanted command into history, providing that your bash session is still open, the command hasn't yet touched the disk. To delete the previous command in history, issue:

(bash)$  history -d $((HISTCMD-1))

Note the leading space; this command requires ignorespace, otherwise it'll just delete itself!

If you want to delete the last few commands, find the last and first history number:

(bash)$  history 5
  598  ok
  599  sensitive
  600  unsafe
  601  doesn\'t-work
  602  otherwise-unwanted

In this case 602 and 599. Then issue:

(bash)$  for i in {602..599}; do history -d $i; done

(Without ignorespace, it would be 603..599.)

If you don't want any history from your current session to hit the disk, exit using:

(bash)$ kill -9 $$

The approach so far is to not even let sensitive history items be written to disk for extra security, because in theory data deleted from non-volatile media can still be recovered.

If, however, the command(s) you wish to remove are from a previous session, they will have already been appended to the $HISTFILE on exit. Manipulating the history with the above commands will still only append the remaining new items to the $HISTFILE, on exit. To overwrite the $HISTFILE with the current session's view of the entire history, right now, issue:

(bash)$  history -w

Of course for history items already on disk, the alternative to editing the history with history -d commands then issuing history -w, is to edit the $HISTFILE with a text editor.

share|improve this answer
1  
”Manipulating the history with the above commands will still only append the remaining new items to the $HISTFILE, on exit.” Actually, this isn't exactly true. It seems to only append remaining new items that ‘stick out’ from the original history length. E.g. If I remove 5 original items and add 15 new, only the last 10 new are appended, whereas I'd expect all 15 new to be appended. I think this is a bug because I can't see how this is ever desirable functionality. –  James Haigh Jun 19 '13 at 15:59
1  
I guess bash takes note of the original length. On session close, it presumably appends items whose number is greater than this value. If this value was decremented every time an item is deleted whose number is less than or equal to this value, it would work as expected. ;-) –  James Haigh Jun 19 '13 at 16:06

Several techniques:

Prevent sensitive information from being stored in the history file

If you've entered some password on a command line, then realize that all commands are logged, you could either:

  1. Force exit the current session without saving history:

     kill -9 $$
    

    This will drop all current history.

  2. Type ↑ (up arrow) in the open bash session until the sensitive information is shown, then use line editing keystrokes like Ctrl+W to delete the sensitive info, and then ↓ (down arrow) until a new empty line is prompted, before typing Enter.

Delete sensitive information from the history file

If you realize that sensitive information is already stored, and you want to delete it, but not your entire history:

A simple sed command could do the job:

sed -e '/SeNsItIvE InFo/d' -i .bash_history

but, as you type this, you create another history line containing the search pattern (sensitive info) you are trying to delete. So you could:

sed -e "/$(head -n1)/d" -i .bash_history

This will run head -n1 with input from the terminal. It will appear that your terminal is hung (there won't be a prompt); just type the information that you want to delete from the file. This is a trick to let you enter (part of) a command without actually typing it into the command line, thus making it ineligible for inclusion in the history record. Then sed will use the text that you typed to search .bash_history and delete all lines containing the sensitive info. Note: if your sensitive information pattern contains slash(es), you must escape them with backslashes, or else change the sed command to use this syntax to specify a delimiter that does not appear in the pattern:

sed -e "\|$(head -n1)|d" -i .bash_history

Another way could be to delete only the sensitive info, but keep the commands that contain the information. For this, you could simply replace sensitive info with substitute text of your choice:

sed -e "s/$(head -n1)/Santa Claus/g" -i .bash_history.

Delete sensitive information from any file in a specific tree

Finally, to be sure that this won't stay in another forgotten file:

SENSITIVEINFO="$(head -n1)"
find . -type f -print0 | xargs -0 grep -l "$SENSITIVEINFO"

will list all concerned files.

find . -type f -print0 |
    xargs -0 grep -l "$SENSITIVEINFO" |
    tr \\n \\0 |
    xargs -0 sed -e "s/$SENSITIVEINFO/Santa Claus/g" -i

will replace all occurrences of sensitive info in all files in the directory tree rooted at .. Note: even though this command uses xargs -0, it will not handle files with newlines in their names.

share|improve this answer

Just try these,

$ history

this will display the id of the history and the command, e.g.

.
.
211 ls
212 javac Welcome.java
213 java welcome
.
.

use,

$ history -d 211

Here 211 is the id of the history. Now check this using

$ history

.
.
211 javac Welcome.java
212 java welcome
.
.

share|improve this answer

Locate the line you want to delete by pressing ↑ (up arrow) until it appears, then press Ctrl+U. That should remove the line.

If you use the history command, you can see the line has been substituted with an asterisk.

share|improve this answer
1  
Actually, I believe that you must cursor up to the line, press Ctrl+U, and then cursor up or down to another line (possibly the blank one at the bottom of the list).  Also, strictly speaking, the line hasn't been substituted with an asterisk.  Rather, the command has been erased, and the history number has been appended with a *. –  G-Man Apr 1 at 16:47
    
This becomes much more powerful with Ctrl+R (reverse incremental history search), then e.g. [End], Ctrl+U –  sehe Jun 15 at 12:13

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.