In the process of working on the Ubuntu or Linux terminal, you can see the history of the executed commands being stored. However, there are cases when you want to clear your history for security or simply to remove the clutter. Here are 4 different ways to clean command line Linux.
4 Methods to Clear Shell History in Linux/Ubuntu Terminal
Method 1: Use the command to clear history
history -c && history -w
This is a simple way to clear the Terminal history. This command will delete all entries in the history, which will not be visible when using the history command right in the current session.
Method 2: Clean command line Linux with ordinal number
history -d <ordinal number>
Use the command history
to see a history list of commands that have been executed on the terminal. Find the command line you want to delete and memorize its serial number. Then use the command history -d <number>
to clear shell history in terminal. For example, history -d 5
will delete the command history on the terminal of the 5th command line.
Method 3: Edit the history file, history
nano ~/.bash_history
The terminal stores the history in a text file, usually ~/.bash_history
. You can edit this file to delete history entries manually. Use a text editor nano
to open the history file and delete the corresponding lines. After editing, save the file and close it.
Method 4: Delete the history file on the system
rm ~/.bash_history
If you want to completely clear history terminal on Linux or Ubuntu, you can use the above command to delete the current history file. Then a new bash_history file will be created when you open the terminal and continue executing the command. Note that deleting the history file will permanently delete all stored history and cannot be undone, so use this method with caution.