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?
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?
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
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
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.
history -cw
– Elliot
Apr 24 '14 at 22:52
execute the following commands to clear history forever
history -c && history -w
good luck!
There's another much simpler one: running history -c
on the terminal prompt and gone are all entries in the bash_history
file.
history -c
is the exact method that led to this question.
– J.Money
Sep 22 '15 at 18:41
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.
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.
rm ~/.bash_history; history -c; logout
Now log back in and witness that your arrow-up doesn't give you anything.
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.
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.
> ~/.bash_hstory
be enough? – configurator Jan 10 '13 at 04:36I 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~/.bash_profile
:export SHELL_SESSION_HISTORY=0
, then do asource ~/.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~/.bash_profile
file only held very old command history. I had to use thehistory
command to assess the recently issued commands andhistory -d NNN
to delete a specific command holding sensitive information. – Paulo Carvalho May 25 '18 at 12:23vim ~/.bash_history
to erase only the lines you really want to get rid of from the history and then invokehistory -c && exit
to make it persistent – Rizan Zaky Sep 28 '18 at 13:17cat /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:52history -cw
as mentioned in callpraths' answer. – Daniel K. Aug 12 '20 at 09:40cat /dev/null > ~/.bash_history && history -c && exit
norhistory -c
worked for me, it saysfc: event nor found: -c
. What's going on? – snek programmer Sep 20 '21 at 23:01