Sorry, you have been blocked
This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.
What can I do to resolve this?
You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.
Cloudflare Ray ID: 7d99e5c95f362492 • Your IP: Click to reveal 88.135.219.175 • Performance & security by Cloudflare
How to clear bash history
Run the following command to create some bash history. ‘date’ command will display the current date and time. ‘ls’ command will display the list of the files and folders of the current location. ‘clear‘ command will clear the terminal screen.
Run the history command to display the current bash history.
Run the following command to clear the terminal history and exit from the terminal.
Clear specific bash history entry by using history command:
Run the following command to create some bash history. The first command will print ‘Hello’ message. The second command will print the current logged in user’s name. The third command will take input from the user and store in the variable $a. The fourth command will print the value of $a.
Run the ‘history’ command to display the current history.
Run the following commands to delete the 4 th entry of the history and print the history after delete.
Here, the entry of ‘echo $a‘ is removed from the history entry.
Clear all history by removing .bash_history:
/.bash_history file exists and stores the history information in that file, then you can run the following command to remove the file.
Prevent storing history information permanently:
Run the following unset command to prevent creating a history file and exit from the terminal. If you open a new terminal after running the following command, then no previous history information will display.
When the value of HISTSIZE is set 0, then no history entry will be stored permanently. The following command will stop storing history information and terminate the terminal. When a new terminal is opened after running this command, then no previous history information will display.
If you want to remove the history file forcefully, prevent creating a history file, and terminate from the terminal, then run the following command. After that, if a new terminal is opened, then it will work from the blank history.
The following command can also be used for deleting the current history information permanently and terminate from the terminal. When a new terminal is opened after running this command, then no previous history information will display.
Conclusion:
This article shows how bash history can be cleared and prevent storing history information permanently by using various bash commands. If the bash users work with normal bash commands, then he/she can use the history commands mentioned above to remove particular or all history information when requires. But if the users work with sensitive data, then it is better to select those commands shown in this article to prevent storing history information permanently.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
How To Clear Command Line History In Linux
In Bash, all the commands that you run in the Terminal are stored in a text file named in .bash_history in your home directory. We can use the history command to display all the commands that you run since you started the session. For some reason, you may want to delete all or a specific command from the Bash history. In this brief guide, let us learn how to clear command line history in Linux with examples.
Before getting to the topic, first let us see why should we clear the command line history and why shouldn’t we do it in some cases.
Why should we clear Command line history?
There are chances that you don’t want to expose the Command line history of your Linux system. Say for example, if you are a Linux trainer/tutor, you might have taught some commands to your students in the Lab computer.
Those commands might be harmful and are not supposed to use. But the students may not fully aware of the consequences of such critical commands.
A curious student might search the command line history, and wonder what does those commands will do, and start to test them one by one. The result? He/she might inadvertently break the system.
Would you allow that? Of course, we can re-install or repair the system in couple minutes. However, it is completely unnecessary if you are bit careful. So, clearing Command line history from time to time, especially in a shared computer, is a good practice.
It is just an example. There could be many other reasons to clear Linux command line history.
Why shouldn’t we clear Command line history?
Clearing Command line history is a good practice, however you must not clear history in some cases. Say for example, you want to repeat a specific command often. Would you type the same command every time? That’s not necessary.
As I said earlier, the commands that you run on the Terminal will be retained in the
/.bash_history file. You can retrieve the previously executed commands by pressing the Page Up button in the keyboard.
When you hit the Page Up button, the list of commands that you run recently will appear. Just keep hitting the UP arrow key until you find the command that you want to run. Once you find the command that you want to run, just hit ENTER to execute. That’s it. You don’t have to type the entire command every time.
Sometimes you can’t remember some lengthy and complex commands in a previous session. In such cases, you can use history command to retrieve the previously executed commands.
So if you already cleared the Bash history, you can access the previously entered commands. This is why you should decide whether you want to clear the command line history or not.
Without further ado, let us see how to Bash history in Linux.
Clear Command line history in Linux
There are many ways to clear Command line history. Here I have listed five ways.
1. Clear entire Command line history using history command
As stated earlier, the history command will display the last executed commands.
To view the previously executed commands in Linux, run:
To clear the history, just run any one of the following commands:
The above commands will clear the command line history.
2. Avoid saving commands in history list by inserting a blank space before each command
Using this method, you can eliminate a specific command getting saved in the history list. Just put a blank space (Hit space bar from the keyboard) before any command. The command will not be recorded in history.
To do that, you must set HISTCONTROL environment variable’s value as “ignorespace” or “ignoreboth”.
Add the following line at the end:
Save and close the file. Run the following command to take effect the changes.
Or, simply log out and log back in to apply the changes.
To verify it, run a few commands. And put a blank space in-front of any command. I put a blank space in last command.
Here <space> indicates a blank space.
Refer the following screenshot:
Put a blank space before a command to avoid saving it in history in Linux
Now, run the history command to view the recently executed commands:
Sample output:
Clear Command line history in Linux
As you see in the above output, the «sudo ping ostechnix.com» command is not displayed in the history command output.
3. Clear or delete specific commands from Bash history
Sometimes you might want to delete some particular commands from history list, instead of clearing the entire history.
Let me show you an example. I am going to run the following commands in the Terminal.
Then, display the history command output using command:
sample output:
As you see in the above output, the history command displays the last executed commands.
To clear or delete a particular command from the history, use -d flag with the command prefix number:
Here <number> represents the line number of each command.
For example, to delete the «time» command with line number «5» from the history, run:
Now, display the command line history to see if the command is removed or not.
Sample output:
Clear or delete specific commands from history
As you see, the «time» command has been removed. Similarly, you can delete any command from the history list.
4. Clear Bash history automatically at logout
Instead of manually clearing history each time, you can automatically clear it at logout.
Add the following line:
Save and close the file. Now, the history will be cleared after you logout from the session.
5. Delete Command line history permanently
All of the above methods will only remove the commands from the active session i.e currently opened terminal. If you have multiple terminals running different commands, the history command will only delete the history from where you run this command. The commands from other terminals will still be available.
To remove all commands from Bash history in all sessions, you must remove the contents of .bash_history file. This file contains the list of commands that we run in the terminal. You can either clear this file manually each time or setup a cron job to clear it at regular intervals.
To manually clear the contents of this file, run:
Like I said, this will delete the entire history. The next time this file will start to record the history. You should run this command everyday to clear the contents of this file.
Alternatively, you can schedule this task at regular interval using cron jobs.
Add the following commands:
Save and exit file. The history will be automatically cleared at 8 pm everyday.
Recommended read:
Conclusion
In this tutorial, you have learned why should we clear the command line history, and why you shouldn’t do it in some cases. Also, you have learned the different ways to clear command line history in Linux. If you know any other method to do it, feel free to let us know in the comment section below.
Senthilkumar Palani (aka SK) is the Founder and Editor in chief of OSTechNix. He is a Linux/Unix enthusiast and FOSS supporter. He lives in Tamilnadu, India.
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:
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.
/.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.