ItsmeITItsmeIT
  • Linux
    • Debian
    • Ubuntu
  • PC/Windows
Reading: How to Clear Command History in Linux
Share
Notification Show More
Font ResizerAa
ItsmeITItsmeIT
Font ResizerAa
  • Categories
    • Linux
    • Debian
    • Ubuntu
    • PC/Windows
Have an existing account? Sign In
Follow US
© 2025 ItsmeIT. All rights reserved.

Home » Linux » How to Clear Command History in Linux

How to Clear Command History in Linux

avatar
By
Loibv
avatar
ByLoibv
The ItsmeIT team – delivering cutting-edge updates, tech trends, and insider knowledge from the world of technology.
Follow:
Last updated: August 26, 2025

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.

Clear shell history Ubuntu
Ways to Clean Your Terminal

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ửahistory -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
Edit bash shell history
Edit bash shell 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)

SystemHistory File LocationCommand to Clear History
Bash (Ubuntu/Linux)~/.bash_historyhistory -c && history -w
Zsh (macOS/Linux)~/.zsh_historyrm ~/.zsh_history && fc -W
Fish Shell~/.local/share/fish/fish_historyrm ~/.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!

Share This Article
Facebook Reddit Telegram Threads
avatar
ByLoibv
Follow:
The ItsmeIT team – delivering cutting-edge updates, tech trends, and insider knowledge from the world of technology.
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

Most Popular

Backup WordPress Data in Docker
How to Backup WordPress Data in Docker
What Is Docker?
What Is Docker? How It Works and Why You Should Use It
Install Docker on Linux
How to Install Docker on Linux (Ubuntu, Debian, RHEL, AlmaLinux)
Nginx localhost domain setup
How to Map a Custom Domain to Localhost Using Nginx
install wordpress nginx
How to Install WordPress on Ubuntu 22.04/24.04 with Nginx, MariaDB, PHP8.2 (LEMP)
Previous Next
ItsmeITItsmeIT
Follow US
© 2025 ItsmeIT. All Rights Reserved.
  • Privacy Policy
  • Terms of Service
Logo ItsmeIT logo
Welcome Back!

Sign in to your account

Continue with Google
Register   |Lost your password?