How to Remove a Command from History in Linux

The history command in Linux displays the list of previously executed commands in a tabular format; with the first column displaying the number of commands, and the second column displaying the actual commands that were executed.

The list of previously typed commands can also be accessed by pressing the ‘Up’ and ‘Down’ arrow keys on the keyboard, so as to avoid typing a recently executed command again.

The maximum number of previous commands which are stored can be configured.

Find All Executed Commands in History

$ history
List Last Executed Commands
List Last Executed Commands

There can be cases when a user has typed a potentially dangerous or risky command. It is unsafe to keep such a command in history, as it could be unintentionally invoked again.

For example, command no. 10 in the above image is a popular denial of service attack called the ‘Fork Bomb’ which will completely exhaust the memory of the computer.

Let us see how to delete such commands from history.

Deleting a Single Command in History

We can delete a single command from history using the '-d' parameter.

$ history -d <command no.>

Let’s run this and verify if command no. 10 is deleted from the history command.

$ history -d 10
$ history
Delete a Command from History Commands
Delete a Command from History Commands

As seen above, the fork bomb has been deleted from history, and command no. 10 is assigned to the next command in the list.

Deleting All History Commands

Execute the command with the parameter ‘-c’ will delete all the commands from the list of history.

$ history -c
$ history
Delete Linux Commands History
Delete Linux Commands History

As we can see above, all the commands have been deleted from history.

Conclusion

In this article, we discussed the history command and how to delete certain commands or all the commands from the history. The history is stored in a file called ‘.bash_history’ which is present in the user’s home folder.

For more information on the history command, refer to the man page of history by executing:

$ man history

If you have any questions or feedback, make sure you leave a comment below!

Got something to say? Join the discussion.