- How do I clear the terminal History?
- 7 Answers 7
- Delete history without a trace in Linux
- Deleting your history without a trace in Linux
- Removing a specific line from the .bash_history file
- Turning off the bash history
- Limiting the bash history size
- Conclusion
- About the author
- David Adams
- How to Clear BASH Command Line History in Linux
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.
Delete history without a trace in Linux
Your bash history can be seen using the history command. If you run it, all the user history will be displayed as shown below.
By default, the bash history is located in the file .bash_history file located in the user’s home directory. In my case, the user is linuxhint, to see the history using cat I run:
Note: you can learn your history file location by running the command below.
Deleting your history without a trace in Linux
Deleting the command line activity history is simple, and there are few ways to achieve it. The first method uses the history command shown above, followed by the -c (clear) option, as shown in the screenshot below.
As you can see, after running history -c, if we run the history command without options again, there will not be output except for the current command.
Another way to remove your bash history is to remove the hidden .bash_history file located in the user home directory.
Since we want to remove the command line history without leaving a trace, let’s do it so that recovering the history file is impossible. To do it, we need to install the wipe tool.
To install wipe on Debian based Linux distributions, run:
Now wipe was installed, you can securely remove the .bash_history file without the chance it will be recovered.
Now your .bash_history file was fully wiped but will be automatically generated again to record future commands when you log out and log in back as the same user. At the end of this tutorial, you will find instructions to prevent the .bash_history file from recording commands.
Removing a specific line from the .bash_history file
Let’s suppose, like most Linux users, you use the .bash_history when repeating commands, but you want to remove a specific mistake. In some cases, some users may type a password in their terminal; this is extremely insecure.
In the following screenshot, we can see 7 commands.
Let’s say you only want to remove the sixth command (wipe .bash_history). To do it, you can use the history -d command followed by the line you want to remove, as shown below.
As you can see, the command was removed, but you need to write the changes using the history command followed by the -w flag, as shown below.
Now the specific line you wanted to delete was removed.
You also can remove line ranges. The following example shows how to remove a specific number of lines starting from a specific line. The command below will remove 10 lines starting from line 40. If you want to remove a number other than 10 lines, replace the number 10 with the number of lines you want to get deleted. Replace the number 40 with the line you want the line range to start.
Turning off the bash history
This tutorial section explains different methods to disable the bash history.
If you want to disable the bash history for the current shell only, run the command below.
As you can see, after running set+o history, that command was the last recorded, which means no commands typed after that were recorded, including the history command used to check the result.
You can see if the history is enabled by running the command below.
Note: You can enable the bash history back by running the command below.
To disable the bash history permanently, run the command below to add the rule set +o history to your .bashrc file. The .bashrc file stores the configuration for your terminal sessions, including the shell history, among other features.
As you can see, at the bottom of your .bashrc file, now you’ll see the set+o history rule added.
Apply the changes by running the .bashrc file as shown below.
To disable the history for the whole system, run the command below with root privileges to unset the HISTFILE variable.
Note: If you want to recover the shell activity history, you can remove the file you just created under /etc/profile.d.
You also can disable the bash history for new or future users, keeping it for existing users. To achieve it, run the command below.
To undo the previous command and restore the bash history, you need to remove the set+o history from the /etc/profile file
Remove the highlighted line (the last one containing the set+o history command).
Limiting the bash history size
There are 2 variables limiting the bash history size, the $HISTFILESIZE and $HISTSIZE variables. The difference between them is that $HISTFILESIZE defines the number of lines that will be saved to disk and remain after the session ends. The second variable, $HISTSIZE, defines the number of lines that will be saved in the history but won’t remain after you close the session.
To see the number of lines saved in your history, run the commands below even after you close the session.
As you can see, after closing and logging back, my history will keep my last 100 commands.
To change the number of lines saved to disk, run the command below.
Note: 50 is the number of lines you want to keep in your history even after the closed session. Replace it with the number of lines you want.
As you can see, now your history saves your last 50 commands to disk.
Run the command below to see the number of lines saved while in the same session until logging out.
As you can see, your session history records your last 500 commands.
To change the $HISTSIZE run:
Now your sessions will keep your last 50 commands.
Conclusion
As you can see, removing your bash history or specific content within it is pretty easy, including for new Linux users. Knowing how to remove your history is especially important when you work remotely and need to keep activity safe. In some cases, users may type their passwords by mistake on the terminal, leaving sensible information. In some cases, some administrators may opt for disabling this feature at all. Yet, the bash history is an excellent feature that helps us repeat commands very easily by pressing a key.
I hope this tutorial explaining how to delete the history in Linux without a trace was useful. Keep following Linux Hint for more Linux tips and tutorials.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
How to Clear BASH Command Line History in Linux
The bash history keeps a record of all commands executed by a user on the Linux command line. This allows you to easily run previously executed commands by using the “up arrow” or “down arrow” keys to scroll through the command history file.
In this article, we will show you two simple ways to clear your command-line history on a Linux system.
The major reason for removing command-line history from the Linux terminal is to prevent another user, who could be using the same account.
For instance if you have typed a command that contained a password in plain-text and you don’t want another system user or an attacker to see this password, you need to delete or clear the history file.
Take a look at the command below, here the user aaronkilik has typed the database server password on the command line.
If you look into th bash history file towards the end, you will see the password typed above in there.
The bash_history file is normally located in a user’s home directory /home/username/.bash_history.
$ ls -l /home/aaronkilik/.bash_history
To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered clear-text password as in the scenario above, find the line number in the history file and run this command.
To delete or clear all the entries from bash history, use the history command below with the -c option.
Alternatively, you can use the command below to delete history of all last executed commands permanently in the file.
$ cat /dev/null > ~/.bash_history
Note: A normal user can only view his/her own command history, but the root user can view the command history of all other users on the system.
You can learn more about the bash history file and useful history commands here: The Power of Linux “History Command” in Bash Shell.
Always remember that all commands you run are recorded in a history file, so do not type plain-text passwords on the command line. If you have questions or thoughts to share with us, make use of the feedback form below.