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
Here is a simple way to clear history in terminal, this command will delete all entries in the history of terminal 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
Terminal stores 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
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 delete all stored history and cannot be undone.