clear terminal command history linux

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

clear terminal command history linux

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 🙂


Notify of

Inline Feedbacks
View all comments