How do you clear the terminal history in Linux ?
In Linux terminal you can execute different types of command. Once the command is been executed, the history will get saved in .bash_history file which is hidden. Let’s say you have entered your password accidently in the terminal, which got stored in history file and you wanted clear the terminal history. This sneppet will teach you how to clear the terminal history completely or specific lines.
Clear the terminal history from .bash_history file
Let’s say you had typed password “admin$1234” in the terminal accidently and it got saved in the history as shown below.
$ history 1 7:14 cat /etc/passwd 2 7:15 cut -d: -f1 /etc/passwd | sort 3 7:16 sudo du -smc /home/* | sort -n 4 7:17 admin$1234 5 7:17 history
Any unauthorized person, if he get access to the system he can recover the file and if the history is not deleted then he will come to know your password which is dangerous. Hence you should know how to manage or clear the terminal history completely or specific line.
The terminal history is entirely managed by the shell. And when you exit from the current session, you actually exit bash and the terminal history will get saved by default to a history file called .bash_history in your home directory.
Whenever history is created in the current terminal session, new lines will be appended to the .bash_history file. And entries that were created before remains unchanged.
The history -c command clears the current shell. But it will not clear the history from the .bash_history file.
Finally, you can use history -w command to overwrite the .bash_history file with current terminal session’s history.
Remove specific lines from terminal history
You can also open .bash_history file in your favorite editor and remove specific line that you don’t want to keep in the history file.
The command history -d can be used to delete the entries that you don’t want. Finally, you can use history -w command to save the changes in history file.
That’s it you had learnt how to clear bash history in linux either completely or specific lines.
Hope this sneppet helped 🙂
- Set or change root password in Ubuntu Linux
- Linux: sudo: apt-get: command not found
- How to Start Stop Restart MariaDB on Linux OS ?
- Putty Fatal Error No supported authentication methods available
- How to find which users belongs to a specific group in linux
- Give write permissions for specific user or group for specific folder in linux
- How to unzip a zip file from Terminal (Google Cloud Shell)
- Build a Docker Image with a Dockerfile and Cloud Build in GCP?
- How to create GCP project on Google Cloud Platform
- MariaDB – How to set max_connections permanently ?
- How to check sshd logs and status in linux ?
- Create non-root SSH user account and provide access to specific folders
- MySQL : How to grant all privileges to the user on database ?
- Amazon Linux AMI : apt-get command not found
- How to get the first and last element of a list in Python ?
- Find the PID of the process that is using specific port ?
- Switch between users in Linux Terminal ?
- Add correct host key in .ssh/known_hosts to get rid of this message