How to delete a single command from history in Linux?

Hello guys, In this article, we will discuss how to delete a single command from history in Linux. Sometimes, you may enter an incorrect command which you don’t want repeating or you may have entered any password which you don’t want other users to see.

If you are familiar with the terminal, we can access the previous command by using the up and down arrow keys. Terminal history can be accessed if you have root access. Any user with root access can see the terminal history. So here’s how you can clear the terminal history or remove a single command from the terminal history with ease.

Viewing the terminal history

To view history using the history command, Open a terminal by pressing Ctrl+Alt+T and enter the following command:

history
delete-command-from-history-1

Searching the terminal history

To search commands in the terminal history, Execute the following command in a terminal window,

history | grep <command_name>
history | grep pwd
delete-command-from-history-2

Deleting commands from the terminal history

To delete a single command, execute the following command:

history -d <command no.>
history -d 262
delete-command-from-history-3
delete-command-from-history-4
delete-command-from-history-5

As shown in the picture above, line 262 has been deleted and has been replaced by the next command.

Delete Entire Terminal History

To delete the complete terminal history at once, execute the following command:

history -c
delete-command-from-history-6

Conclusion

So, We discussed how to view, search and delete commands from history. The history command is used to view the history, It also stores the exact date and time of the commands. By default, It does not show the date and time. To view the date and time along with the commands, You can refer to the article: How to check Linux command history by dates?

Thank You for reading!