There are different ways to accomplish this task, but lets clarify something before going further.
There is a file named: ~/.bash_history
, which contains your older terminal sessions history, whenever you close your terminal, your history will be saved there.
At the same time the history of your old sessions along with current session is temporary accessible by history
commands until you close the terminal which then will be saved into ~/.bash_history
file.
So if you remove 5 lines at the end of ~/.bash_history
, then closing terminal will cause your current command to be accessible at next sessions.
So if I do a wc
on .bash_history:
wc -l ~/.bash_history
Most of the time I'll get a smaller number than of history | wc -l
.
If you want to remove the last 5 line of the file, you can use this command:
for i in {1..5}; do sed -i '$d' .bash_history; done;
And if you want to keep all history except last 5 command issued in current session run:
history | awk '{ print $2 }' | head -n -5 > .bash_history
Don't forget to run history -c
too.
Best Answer
The file
~/.bash_history
holds the history.To clear the bash history completely on the server, open terminal and type
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):