370

I want to clear all previous commands from the history of my server. I used history -c and it seems all things are cleared but when I ssh to the server, all the commands are still there.

How can I clear them permanently?

Zanna
  • 70,465
hd.
  • 3,867
  • 5
  • 19
  • 10

9 Answers9

495

The file ~/.bash_history holds the history.

To clear the bash history completely on the server, open terminal and type

cat /dev/null > ~/.bash_history

Other alternate way is to link ~/.bash_history to /dev/null

However,

One annoying side-effect is that the history entries has a copy in the memory and it will flush back to the file when you log out.

To workaround this, use the following command (worked for me):

cat /dev/null > ~/.bash_history && history -c && exit
devav2
  • 36,312
  • 18
    you can also put above command in .bashrc & .bash_logout . what it mean when you login u will have clear history & when you logout out your history will be cleared – Qasim Sep 23 '12 at 06:06
  • 30
    Why do you need /dev/null there? Wouldn't > ~/.bash_hstory be enough? – configurator Jan 10 '13 at 04:36
  • 4
    yes it would be enough! – fromnaboo May 18 '13 at 15:40
  • 1
    It seems that there exists several copies. One is for root and the others for other users. I can't find the other user's copies. – Stallman Jun 24 '14 at 10:52
  • 3
    I tried to do it on a raspberry box while connected via SSH.

    I added the above command ( cat /dev/null > ~/.bash_history && history -c && exit ) to both .bashrc & .bash_logout as suggested by @Qasim .

    Now as soon as I connect via SSH the remote host closes the connection (after printing the motd) . Help :/

    – Advanced Feb 13 '15 at 17:07
  • 18
    Goes to show what happens when you blindly do things without reading properly and without understanding the processing and data flow. It also shows the pitfalls of bad communication. What devav2 did was run it one time, and what Qasim should have written is to remove the exit command before adding it in login script. – Sri Sep 02 '15 at 04:26
  • 1
    Also works on Mac OS X El Capitan (tested on version 10.11.2), but you have to add that following line to your ~/.bash_profile: export SHELL_SESSION_HISTORY=0, then do a source ~/.bash_profile and to finish shut down and restart your Terminal app. If you want to understand what this export command does, then you should definitely check this following link: http://superuser.com/questions/950403/bash-history-not-preserved-between-terminal-sessions-on-mac – King-Wizard Jan 05 '16 at 00:58
  • In my case the ~/.bash_profile file only held very old command history. I had to use the history command to assess the recently issued commands and history -d NNN to delete a specific command holding sensitive information. – Paulo Carvalho May 25 '18 at 12:23
  • you can also use vim, vim ~/.bash_history to erase only the lines you really want to get rid of from the history and then invoke history -c && exit to make it persistent – Rizan Zaky Sep 28 '18 at 13:17
  • It worked. But I had to write (cat /dev/null > ~/.bash_history', new line,history -c') to both ~/.bashrc and ~/.bash_logout for my normal user and ~/.bashrc for root. I like Ubuntu; I don't like this. – YoungCoder5 May 17 '20 at 18:52
  • 1
    This solution did not work for me. What worked was the shorter solution with just issuing history -cw as mentioned in callpraths' answer. – Daniel K. Aug 12 '20 at 09:40
  • Neither cat /dev/null > ~/.bash_history && history -c && exit nor history -c worked for me, it says fc: event nor found: -c. What's going on? – snek programmer Sep 20 '21 at 23:01
210

What to do:

In every open bash shell (you may have multiple terminals open):

history -c
history -w

Why: As noted above, history -c empties the file ~/.bash_history. It is important to note that bash shell does not immediately flush history to the bash_history file. So, it is important to (1) flush the history to the file, and (2) clear the history, in all terminals. That's what the commands above do.

Reference: http://www.giannistsakiris.com/index.php/2007/09/13/how-to-clear-bash-history-and-what-to-watch-out-for/

Ajedi32
  • 165
  • 1
  • 10
callpraths
  • 2,209
  • 3
    Actually, the other way around worked for me. – kristianp Feb 01 '14 at 00:43
  • 42
    In many cases, you can combine these two commands together: history -cw – Elliot Apr 24 '14 at 22:52
  • 2
    For some reason this does not work on Ubuntu 14.04, probably others. It should but it doesn't. If you issue the command "history -cw" you can confirm with the up arrow that the history isn't there anymore, but if you start another terminal window (in Unity desktop) with shift + click on the terminal icon (I have it pinned in the launcher) the commands history are back, no matters how many times you do "history -cw". "cat /dev/null > ~/.bash_history" is the only way that worked for me. – Hatoru Hansou Dec 04 '14 at 07:20
  • While I'm not Ubantu, works fine for my bash shell in OSX. – SMBiggs Jan 02 '15 at 19:54
  • 4
    It's history -c and then history -w, you first clear the history then write the changes. This is easily confirmed by closing the terminal and opening it again, with -w first the commands are there again, with -w last history is effectively cleared. – Hatoru Hansou Mar 25 '15 at 07:27
  • It doesn't clear ~/.bash_history, it clears whatever the history file is at that moment ($HISTFILE). – Carlo Wood Oct 04 '17 at 14:54
  • The accepted answer did not work for me. This is much shorter and worked. – Daniel K. Aug 12 '20 at 09:41
  • The link is dead and currently points to a potentially dangarous scam. Wayback link to be used instead: https://web.archive.org/web/20180403200605/http://www.giannistsakiris.com/2007/09/13/how-to-clear-bash-history-and-what-to-watch-out-for/ (There are too many pending edits and I can’t edit) – Yushin Washio Jun 25 '23 at 05:57
92

execute the following commands to clear history forever

history -c && history -w

good luck!

Eric Carvalho
  • 54,385
Edwin Jose
  • 921
  • 6
  • 2
19

There's another much simpler one: running history -c on the terminal prompt and gone are all entries in the bash_history file.

Aditya
  • 13,416
  • No, it is not. Log out, log in, arrow-up gives me everything. You need the "history -w" too. – stolsvik Nov 17 '13 at 19:31
  • 1
    If you read the OP's question you would know that using history -c is the exact method that led to this question. – J.Money Sep 22 '15 at 18:41
12

Clear the current shell's history:

history -c

When you log out, your current shell's history is appended to ~/.bash_history, which is a cache of previous shells' histories, to a maximum number (see HISTFILESIZE in "man bash").

If you want to remove the history altogether, then you essentially have to empty out ~/.bash_history which many of the above entries have suggested. Such as:

history -c && history -w

This clears the current shell's history and then forces the current shell's history (empty) to overwrite ~/.bash_history....or to be more accurate, it forces it to overwrite HISTFILE (which defaults to ~/.bash_history).

Hope this helps.

Greg Fenton
  • 221
  • 2
  • 3
  • 4
    You answer is accurate, but the question was two years old, had accepted answers, and included the same answer you have given. – Charles Green Jul 10 '14 at 17:47
10

Another way to do this is deleting the ~/.bash_history file by using rm ~/.bash_history command. When you login another time, the .bash_history file will be automatically created.

Anwar
  • 76,649
  • 4
    At least on some OSes ~/.bash_history is written to disk when you log out - Not sure about *buntus. – d-_-b Jun 12 '13 at 23:41
8
rm ~/.bash_history; history -c; logout

Now log back in and witness that your arrow-up doesn't give you anything.

Radu Rădeanu
  • 169,590
stolsvik
  • 385
0

If you want the history not to be saved in the first place, you should add this to your ~/.profile:

unset HISTFILE

That's it.
All new invocations of bash (if you re-login) will not log anything. After that you can delete the old ~/.bash_history file as well if you want.

0

Try this one

edit your .profile and add the line below at the end of the file

rm -f .bash_history

this way, every time you login, it will delete your .bash_history file automatically for you. Adding the -r recursive remove option seems dangerous and not needed.