How to Clear BASH Command Line History in Linux?


As we execute various commands they get stored in a file called .bash_history. We can refer to this file later to find all the commands. Sometimes there may be sensitive information in bash commands. To remove the sensitive information, we may remove specific lines from the bass history file or we may remove the entire file itself. In this article we will see both the approaches.

Here we see the .bash_history file.

$ ls -l .bash_history
$ cat .bash_

Running the above code gives us the following result −

-rw------- 1 ubuntu ubuntu 6495 Dec 31 19:30 .bash_history

Clearing Individual commands

To clear a specific command from the history we will first find out the line number of the command in the bash history file and then issue the –d option to remove it.

cat -n .bash_history

Running the above code gives us the following result −

u@ubuntu:~$ cat -n .bash_history
1perl -v
2sudo -apt update
3sudo apt -update
4cal
5today
6now
7now() ;
..
..

Lets say we aim to remove the command at line 4 which is the cal command.

$ history

Running the above code gives us the following result −

1 perl -v
2 sudo -apt update
3 sudo apt -update
4 today
5 now
6 now() ;
……
……

Clearing Entire History

We can clear the entire history by using the following command. Here we overwrite the .bash_history file with null.

$cat /dev/null > ~/.bash_history
$ cat .bash_history
$

As you can see, when we look for the content of .bash_history it is all blank.

Updated on: 26-Jul-2023

461 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements