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 named .bash_history in your HOME directory. This brief tutorial explains how to clear a specific command from Bash history in GNU/Linux.
Clear A Specific Command From Bash History In Linux
To view the list of recently executed commands, just run:
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 [email protected] 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 prefix number. And, the most recently executed commands are found in the bottom.
Have you ever wondered how 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 prefix number. Alternatively, you can edit .bash_history file and delete the commands of your choice.
Edit .bash_history file:
$ vi $HISTFILE
$ 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 again.
To clear all history, just run:
$ history clear
$ history -c
Finally, run the following command to take effect the changes immediately.
$ 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 detailed guide about history command in the link given below.