How To Clear A Specific Command From Bash History In Linux

Clear A Specific Command From Bash History

Clear A Specific Command From Bash History In Linux

As we all know, history command is used to view the list of recently-executed commands in Terminal. Not just commands, It will show you the list of recent activities in your Terminal. Every commands you execute in the Terminal will be recorded in a file called .bash_history in your HOME directory. To view the list of recently executed commands, just run:

history

You will see the most recent commands on the bottom. Simply press UP or DOWN arrow keys to select any previously executed command and run it again if you want to. Here is the sample output of history command from my Arch Linux.

 [...]
 485 sudo pacman -Su
 486 sudo nano /etc/pacman.d/mirrorlist
 487 sudo nano /etc/pacman.conf 
 488 fc-list | grep termi
 489 fc-list | grep terminus
 490 man rm
 491 clear
 492 ssh root@192.168.1.150
 493 exit
 494 sudo pacman -S powershell
 495 yaourt -S powershell
 496 clear
 497 sudo pacman -Sy libreoffice
 498 uname -r
 499 clear
 500 history

As you see in the above output, my history list file has around 500 recently executed commands. Each command has a number. And, the most recently executed commands will be saved in the bottom. Let us now get in to the topic. Just in case, have you ever wanted to clear a specific command from Bash history in Linux? It’s very easy! Follow the steps given below.

Say for example, to delete “sudo pacman -S powershell” command from the history, just run:

history -d 494

Here, -d flag deletes the selected command from the history file. 494 is the command number. Alternatively, you can edit .bash_history file and delete the commands of your choice.

Edit .bash_history file:

vi $HISTFILE

Or,

vi .bash_history

You will see the list of recently executed commands in this file. Just delete the commands you don’t want to see in the history command output.

To clear all history, just run:

history clear

Or,

history -c

Finally, run the following command to take effect the changes.

source ~/.bashrc

That’s it. Your history has been cleared now. This trick could be useful if you want to allow your junior admins or students to see what commands you have executed so far lately. For more details, check our details guide about history command in the following link.

That’s all for now. If you find this guide useful, share it on your social networks and support us.

Cheers!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...