- How do I clear the terminal History?
- 7 Answers 7
- How to clear all history in linux/ubuntu terminal or bash permanently? [closed]
- 2 Answers 2
- How to clear command history in Linux
- Why should we remove the Linux Command line History?
- Removing the Linux Command line history
- Remove a Line from the bash history
- Remove or Clear all commands from the bash history
- Conclusion
- About the author
- Karim Buzdar
- How to clear bash history completely?
- 9 Answers 9
- However,
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.
How to clear all history in linux/ubuntu terminal or bash permanently? [closed]
When you use the up key in a Linux terminal, you can use previous commands again. Great feature. However, I started logging mysql into mysql with the sensitive details in the command. How can I delete that history permanently?
Since this question is closed I can’t add this as an answer. You can tell bash not to save any history for a particular session with this command: export HISTFILE=/dev/null
2 Answers 2
You can clear your bash history like this:
Really like this answer — this takes effect immediately, rather than deleting .bash_history which requires the shell to be restarted to take effect.
Does not work on my Ubuntu 14.04 machine. History just appears with new terminal. All that worked is >~/bash_history . Have to restart terminal for this though.
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 quit and restart your Terminal app. If you want to understand what this export command does, then you should definitely check this following link: superuser.com/questions/950403/…
isn’t only -c enough? Manuals says -c clears the history list by deleting all of the entries. -w wirtes the current history to history file and append them to history list. Just -c works fine.
If you use bash, then the terminal history is saved in a file called .bash_history. Delete it, and history will be gone.
However, for MySQL the better approach is not to enter the password in the command line. If you just specify the -p option, without a value, then you will be prompted for the password and it won’t be logged.
Another option, if you don’t want to enter your password every time, is to store it in a my.cnf file. Create a file named ~/.my.cnf with something like:
Make sure to change the file permissions so that only you can read the file.
Of course, this way your password is still saved in a plaintext file in your home directory, just like it was previously saved in .bash_history.
How to clear command history in Linux
The bash history stores the records of all terminal commands which are executed by a user on the command-line Linux system. Using the history feature, you can easily locate the previously executed commands on your Linux system through the arrow keys navigation.
Why should we remove the Linux Command line History?
Most of the time, we don’t want to reveal the terminal history of other users on your Linux system. For example, if you are giving classes to your students on a Lab computer and you might have taught some harmful commands and are not recommended to use on our system. But most of the students don’t have an idea about the side effects of these critical commands. A curious student may search about the command-line history and test them to check the working of each command. It may crash your system many times. However, you can repair or re-install your system in a quick way. But, it is not a good practice. So, in this case, we want to prefer to clear the command line history after working on it. Especially when you have shared access with your friends or colleagues. We have just elaborated it with just a simple example; there could be other problems through which you want to clear the Linux command line history. In this article, we will give you a demo of how you can clear the command line history in the Linux system.
We have executed all the commands on Ubuntu 20.04 terminal application in this article. Therefore, it is necessary to open the terminal window on your system. Click on the ‘Activities’ located at the left corner on your system’s desktop, and then using the application search bar, you will search the ‘terminal’ keyword. After completing the search, click on the terminal icon and launch it.
Removing the Linux Command line history
If you want to view the history of your terminal command then, type the following command on the terminal:
Normally, the command line history stores in a file named ‘bash_history’. This file you may locate in a particular user’s home directory /home/username/.bash_history. Execute the below-given command to locate the bash_history file:
Note: A root user can observe the command history of all user’s on your system. But, the standard user can only view their own command line history.
Remove a Line from the bash history
If you want to remove just a single line from the bash history file, then you can use the -d option with history command and enter the targeted line number, which you want to remove.
For example, we want to clear a command that contains your password where you have entered a password in a plain text; you can easily find the line number in the history file and clear it by executing the following command:
Remove or Clear all commands from the bash history
You can also clear or delete all entries from the bash history file. So, use the option -c with the history command. You can run the following command on the terminal to clear or delete the all bash history:
Alternatively, by running the following command, you can remove or delete all history of last executed commands in the bash history file permanently.
Conclusion
In this article, we have presented a better understanding of the history command and why we need to clear it. From the above all mentioned command, I hope now can clear the history of your system easily. But, always remember that what you perform on the terminal all commands recorded in a bash history file, so it is recommended that you never use passwords in a plain-text format on the Linux command line. If you have any queries and thoughts related to this article then, please share your ideas with us.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.
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).