360

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?

9 Answers 9

484

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
12
  • 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, 2012 at 6:06
  • 30
    Why do you need /dev/null there? Wouldn't > ~/.bash_hstory be enough? Jan 10, 2013 at 4:36
  • 4
    yes it would be enough!
    – fromnaboo
    May 18, 2013 at 15:40
  • 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, 2015 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 2, 2015 at 4:26
203

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/

8
  • 3
    Actually, the other way around worked for me.
    – kristianp
    Feb 1, 2014 at 0:43
  • 41
    In many cases, you can combine these two commands together: history -cw
    – Elliot
    Apr 24, 2014 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. Dec 4, 2014 at 7:20
  • While I'm not Ubantu, works fine for my bash shell in OSX.
    – SMBiggs
    Jan 2, 2015 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. Mar 25, 2015 at 7:27
88

execute the following commands to clear history forever

history -c && history -w

good luck!

2
  • 3
    Much better than the accepted answer. ;)
    – Chiramisu
    Oct 3, 2018 at 23:04
  • 4
    This answer would be even more helpful if it explained what these flags do (and therefore why/when they're the right commands to use).
    – LarsH
    Mar 4, 2019 at 16:37
19

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

2
  • No, it is not. Log out, log in, arrow-up gives me everything. You need the "history -w" too.
    – stolsvik
    Nov 17, 2013 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, 2015 at 18:41
11

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.

1
  • 4
    You answer is accurate, but the question was two years old, had accepted answers, and included the same answer you have given. Jul 10, 2014 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.

1
  • 4
    At least on some OSes ~/.bash_history is written to disk when you log out - Not sure about *buntus.
    – d-_-b
    Jun 12, 2013 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.

2
  • 1
    @KasiyA It's about a server and a ssh session, so no need to edit... Oct 20, 2014 at 20:29
  • Slightly modified to rm -f ~/.bash_history; history -c; exit
    – downvoteit
    Jun 24 at 17:08
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.

0
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .