- How to clear bash history completely?
- 9 Answers 9
- However,
- How to Use History command in Kali Linux Terminal
- How to Use Kali Linux History Command
- How to see and type previous command
- How to run previous command fast shortcut key
- How To Remove Or Delete Kali Linux Terminal Command History
- Conclusion
- How do I clear the terminal History?
- 7 Answers 7
How to clear bash history completely?
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
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
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
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 :/
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.
In every open bash shell (you may have multiple terminals open):
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.
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.
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.
execute the following commands to clear history forever
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).
There’s another much simpler one: running history -c on the terminal prompt and gone are all entries in the bash_history file.
If you read the OP’s question you would know that using history -c is the exact method that led to this question.
Clear the current shell’s history:
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:
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).
How to Use History command in Kali Linux Terminal
In Kali Linux, the “history” command allows you to access a numbered list of previously executed commands within the current Terminal session. By typing “history” in the Terminal, you can view the command history and scroll through it using the arrow keys. To execute a specific command, enter its corresponding number. Additionally, the “Ctrl+R” shortcut enables you to search for specific commands by typing related keywords. Using these features, you can conveniently revisit and re-execute commands from your history in the Kali Linux Terminal, but most new Kali users have questions about how to clear Kali Linux history in the terminal.
History command in Kali Linux Terminal:- Most of the time every Kali Linux user uses ↑ Upwards Arrow symbol and ↓ Arrow symbol for previous command but you can use History command see all previous command.
How to Use Kali Linux History Command
Command:- history ( GNU History Library )
If you want see terminal previous command history just type history.
┌──(kali㉿kali)-[~] └─$ history 1 history 2 gedit .zsh_history 3 history 4 clear 5 history . . 289 cal 290 clear 291 cal -y 292 clear 293 cal 1988 294 clear
How to see and type previous command
┌──(kali㉿kali)-[~] └─$ !291 ┌──(kali㉿kali)-[~] └─$ cal -y 2021 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 1 2 3 4 5 6 3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13 10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20 17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27 24 25 26 27 28 29 30 28 28 29 30 31 31 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 1 1 2 3 4 5 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 30 31 July August September Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 1 2 3 4 5 6 7 1 2 3 4 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25 25 26 27 28 29 30 31 29 30 31 26 27 28 29 30 October November December Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 1 2 3 4 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31 31
How to run previous command fast shortcut key
┌──(kali㉿kali)-[~] └─$ !! ┌──(kali㉿kali)-[~] └─$ cal -y 2021 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 1 2 3 4 5 6 3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13 10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20 17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27 24 25 26 27 28 29 30 28 28 29 30 31 31 April May June Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 1 1 2 3 4 5 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 30 31 July August September Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 1 2 3 4 5 6 7 1 2 3 4 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25 25 26 27 28 29 30 31 29 30 31 26 27 28 29 30 October November December Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 1 2 3 4 5 6 1 2 3 4 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31 31
How To Remove Or Delete Kali Linux Terminal Command History
Any other Linux operating system you need to just enter history -d to delete all history or history -d for the particular command to clear the contents of the history file and then use history -c execution or save changes.
But Kali Linux this command nor executable you need to delete manually see below command.
Find this .zsh_history file and open any text editor just clear all text and save then close terminal
You need to just type below command
┌──(kali㉿kali)-[~] └─$ mousepad .zsh_history
Now history file open, then do whatever you want.
This is the best method you can remove or delete Kali Linux command history.
Conclusion
- Open the Terminal.
- Type “history” and press Enter.
- The Terminal will show a list of previously executed commands.
- Use the arrow keys to scroll through the list.
- To execute a command from history, type the command number and press Enter.
- Use “Ctrl+R” to search for specific commands by typing keywords.
As per your request, we write this article if you have any other requests please visit our YouTube channel:- https://youtube.com/@amolblog.
Other Articles for you:
How do I clear the terminal History?
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.
@jasonwryan That alone wouldn’t solve the problem since the sed command would end up in the shell history.
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
7 Answers 7
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.