When working with the Ubuntu or Linux terminal, every command you enter is saved in the history file (~/.bash_history
). This is useful for quickly recalling previous commands, but there are times when you might want to clear your terminal history for security, privacy, or simply to remove clutter.

Why Should You Clear Terminal Command History?
Whenever you enter a command in the terminal, Ubuntu saves it in the ~/.bash_history
file. This helps you quickly retrieve previously used commands. However, there are cases where you might want to clear this history:
✅ Security: If you’ve entered sensitive information (passwords, API keys, login credentials).
✅ Cleanup & Optimization: To remove clutter from your history and make it easier to find important commands.
✅ Shared Computer Usage: To prevent others from seeing your command history.
✅ Accidental Mistakes: If you’ve mistakenly entered an incorrect or sensitive command.
Below are four different ways to clear command history in Linux.
How to Clear Command History in Ubuntu Terminal
#1: Clear the Entire History in the Current Session
If you want to delete all command history in the current terminal session, use:
history -c && history -w
📌 Explanation:
history -c
: Clears all history in the current terminal session.history -w
: Overwrites the~/.bash_history
file with the cleared history, ensuring the old history is completely removed.
📌 Note: This command only affects the current session. If you open a new terminal before running it, the old history may still be available.
#2: Delete a Specific Command from History
If you want to remove a single specific command instead of clearing everything, follow these steps:
Step 1: View the history list
history
This will display a list of previously executed commands, each with a unique number.
Example:
1 ls
2 cd /var/www
3 nano config.php
4 mysql -u root -p'SuperSecretPassword'
...
Step 2: Delete a specific command
For instance, if you want to remove command number 4 because it contains a password, run:
history -d 4
📌 Note:
- This only removes the command from the current session’s memory.
- To permanently remove it, overwrite the history file with: bashSao chépChỉnh sửa
history -w
#3: Manually Edit the History File
Since the command history is stored in ~/.bash_history
, you can edit it directly.
Step 1: Open the history file using nano
nano ~/.bash_history

Step 2: Delete the unwanted commands
Use the arrow keys to navigate, then delete the lines you want to remove.
Step 3: Save the changes
Press CTRL + X
→ Press Y
→ Press Enter
to save and exit.
#4: Completely Remove the History File
If you want to permanently delete all history, use:
rm ~/.bash_history
📌 Important Notes:
- This will permanently delete all command history, and it cannot be recovered.
- Once deleted, the history file will be recreated when you start using the terminal again.
Additional Tips for Managing Terminal History
✅ Prevent Terminal from Saving History Temporarily
If you don’t want commands to be saved in history during a session, disable history logging:
unset HISTFILE
This ensures no commands entered afterward will be saved.
✅ Run a Command Without Saving It to History
A useful trick is to add a space before a command to prevent it from being recorded in ~/.bash_history
.
Example:
mysql -u root -p'SuperSecretPassword'
(Note the space at the beginning of the command.)
To enable this feature permanently, add this to ~/.bashrc
:
export HISTCONTROL=ignorespace
✅ Automatically Clear History on Logout
If you want to automatically delete history every time you log out, add the following line to ~/.bash_logout
:
rm ~/.bash_history
From now on, your history will be wiped every time you exit the terminal.
Comparison with Other Systems (Zsh, Fish, macOS)
System | History File Location | Command to Clear History |
---|---|---|
Bash (Ubuntu/Linux) | ~/.bash_history | history -c && history -w |
Zsh (macOS/Linux) | ~/.zsh_history | rm ~/.zsh_history && fc -W |
Fish Shell | ~/.local/share/fish/fish_history | rm ~/.local/share/fish/fish_history |
If you’re using Zsh or Fish, make sure to use the correct history file for deletion.
Clearing terminal history helps protect your privacy, improves security, and keeps your environment organized. Depending on your needs, you can choose to:
- Completely clear history immediately → history -c && history -w
- Delete a specific command → history -d <number>
- Manually edit the history file → nano ~/.bash_history
- Permanently delete the history file → rm ~/.bash_history
💡 Do you use any other methods to manage terminal history? Feel free to share your thoughts!