109

I am using Linux Mint 17.1 Rebecca for about 2 days and accidentally typed my password into the terminal which is now displayed in the history list of commands I have previously typed.

I want to clear the terminal history completely. I have tried using the following commands in the terminal which I thought would clear the history forever but they do not:

history -c
reset
tput reset

The above commands "will" clear the history from the terminal but when I exit and bring up a new one all my previous history is still there and can all be listed again using the - history command and also by pressing the UP arrow on my keyboard. I do not want this to happen until I have totally cleared my history, then I want to continue using it.

How can I clear my terminal history completely - forever and start fresh?

Please Note: I do not want to exit the terminal without saving history just clear it forever in this one instance.

  • 1
    sed -i "s/$your_pass//g" ~/.bash_history... – jasonwryan May 13 '15 at 23:55
  • 3
    @jasonwryan That alone wouldn't solve the problem since the sed command would end up in the shell history. – Gilles 'SO- stop being evil' May 14 '15 at 0:11
  • 1
    I tried using the code from @jasonwryan but I got: sed: -e expression #1, char 0: no previous regular expression which I think I know why and that lead me to come up with this from a search and some messing around: cat /dev/null > ~/.bash_history && history -c && exit – Shambhala May 14 '15 at 0:15
  • 1
    @Gilles: you are right: there is supposed to be a space in front of the command... – jasonwryan May 14 '15 at 0:28
156

reset or tput reset only does things to the terminal. The history is entirely managed by the shell, which remains unaffected.

history -c clears your history in the current shell. That's enough (but overkill) if you've just typed your password and haven't exited that shell or saved its history explicitly.

When you exit bash, the history is saved to the history file, which by default is .bash_history in your home directory. More precisely, the history created during the current session is appended to the file; entries that are already present are unaffected. To overwrite the history file with the current shell's history, run history -w.

Instead of removing all your history entries, you can open .bash_history in an editor and remove the lines you don't want to keep. You can also do that inside bash, less conveniently, by using history to display all the entries, then history -d to delete the entries you don't want, and finally history -w to save.

Note that if you have multiple running bash instances that have read the password, each of them might save it again. Before definitively purging the password from the history file, make sure that it is purged from all running shell instances.

Note that even after you've edited the history file, it's possible that your password is still present somewhere on the disk from an earlier version of the file. It can't be retrieved through the filesystem anymore, but it might still be possible (but probably not easy) to find it by accessing the disk directly. If you use this password elsewhere and your disk gets stolen (or someone gets access to the disk), this could be a problem.

| improve this answer | |
  • 1
    I like your answer and will accept as you told me quite a few things I did not know that are very useful. I will also change my password! – Shambhala May 14 '15 at 0:21
  • 1
    First type history to check the content of the history log and the line numbers you want to delete, supposed you want to delete the line number 1000 in the history log, then history -d 1000 – Claude COULOMBE Mar 6 '19 at 20:37
  • If I modify the .bash_history file directly, the history command still shows commands as they were before modification, until I log out/log in. – Burrito Mar 8 '19 at 15:44
  • Even after removing it from your .bash_history file, log out from all your bash sessions and then back in to double check that none of your sessions are caching any part of your history which might bring that line back into existence. Also, check your .viminfo file which might have cached any search you did for your password using vim. – jrw32982 supports Monica Mar 11 at 20:15
44

I have tried history -c but the history comes back once we exit and reopens.This helped me.

cat /dev/null > ~/.bash_history && history -c && exit

This clears the history saved in the history file as well as the history in the current session (so that it's not saved to file when bash exits). It then exits the shell. The next shell session will have no history.

| improve this answer | |
  • I was working on a pod that uses sh not bash and the history file was in .ash_history. I just edited it with vi. – Nagev Mar 12 at 17:10
20

Instead of removing all your history entries, type these commands in your terminal:

  1. history -c (for delete history)
  2. history -w (save history)
| improve this answer | |
  • 1
    This won't be enough with zsh if inc_append_history or share_history option is set. – saga May 17 '17 at 8:33
7

Because there is a security issue here, you should consider securely deleting the history file.

$ shred -u ~/.bash_history && touch ~/.bash_history
| improve this answer | |
5

As a neophyte faced with the same problem I resorted to the rather the rather brutal expedient of deleting and recreating the history file:-

rm .bash_history
touch .bash_history

It worked for me without giving rise to any unpleasant side effects that I'm aware of.

| improve this answer | |
4

Type: history -c; rm ~/.bash_history

history -c clears your current session's history, rm ~/.bash_history clears your long term history, so history -c; rm ~/.bash_history clears both.

| improve this answer | |
  • explanation can will help the OP understand better. – Mongrel Dec 15 '16 at 7:54
0

I use these two commands together and it perfectly clears everything:

>~/.bash_history;history -cw;
| improve this answer | |

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