Clearing your command history in Linux ensures no one sees the commands you previously ran. If you are wondering how to delete history in linux, you have come to the right place. This guide walks you through every method, from quick one-liners to permanent solutions. Whether you use Bash, Zsh, or another shell, you will find clear steps here.
Your command history is stored in a file, usually ~/.bash_history for Bash users. Every command you type gets saved there. This is handy for recalling past commands, but it can also expose sensitive data like passwords or file paths. Deleting this history is straightforward once you know the commands.
Why Delete Command History In Linux?
Privacy is the main reason. If you share your terminal or work on a public machine, your history reveals your workflow. You might have typed a password, a private API key, or a command that shows confidential file names. Deleting history removes these traces.
Another reason is to keep your history clean. Over time, your history file grows large. Removing old or repetitive commands makes it easier to find what you need later. Some users also delete history to avoid accidental reuse of a dangerous command.
How To Delete History In Linux
This section covers the most common methods. Each method works for the Bash shell, which is the default on most Linux distributions. We will also mention Zsh and other shells where relevant.
Using The History Command With -C Flag
The quickest way is to run history -c in your terminal. This clears the current session’s history from memory. It does not delete the history file on disk. To also remove the file, you need an extra step.
Here is the full command sequence:
- Open your terminal.
- Type
history -cand press Enter. This clears the in-memory history. - Then type
history -wto write the empty history to the file. This overwrites~/.bash_historywith nothing.
After these steps, your history is gone. Future commands will start a fresh history file.
Deleting The History File Directly
You can also delete the history file itself. Use rm ~/.bash_history to remove it. Then run history -c to clear the current session. This method is permanent unless you create a new history file.
Be careful: if you delete the file, you lose all previous commands. There is no undo. Some users prefer to truncate the file instead of deleting it.
Truncating The History File
Truncating means emptying the file without deleting it. Use cat /dev/null > ~/.bash_history. This writes nothing to the file, making it zero bytes. Then run history -c to clear the session history.
This method keeps the file in place, which can be useful if your shell expects it to exist. It is a clean way to reset history.
Clearing History For Zsh Users
If you use Zsh, the process is similar. The history file is usually ~/.zsh_history. Run history -c to clear the current session. Then use rm ~/.zsh_history or truncate it with cat /dev/null > ~/.zsh_history.
Zsh also has a fc -P command that clears history. But the history -c method works consistently across shells.
Clearing History For Fish Shell
Fish shell stores history in ~/.local/share/fish/fish_history. To clear it, run history clear. This removes all history. You can also delete the file manually.
How To Delete Specific Commands From History
Sometimes you only want to remove a single command, not everything. This is useful if you accidentally typed a password. You can delete a specific line from the history file.
Using History -D Command
Run history to see a numbered list of commands. Find the number of the command you want to delete. Then run history -d [number]. For example, history -d 42 removes line 42 from the current session.
This only affects the in-memory history. To make it permanent, you need to write the history to the file with history -w. Note that the numbers change after you delete a line.
Editing The History File Directly
You can open ~/.bash_history in a text editor like nano or vim. Delete the lines you want to remove. Save the file. Then run history -c and history -r to reload the edited file.
This method gives you full control. You can remove multiple commands at once. Just be careful not to delete important lines.
Using Grep To Remove Lines
If you want to delete all commands containing a specific string, use grep. For example, grep -v "password" ~/.bash_history > ~/.bash_history.tmp && mv ~/.bash_history.tmp ~/.bash_history. This removes every line with the word “password”.
Then clear the session history with history -c and reload with history -r. This is a powerful way to clean up sensitive data.
How To Prevent Commands From Being Saved
Instead of deleting history later, you can stop commands from being saved in the first place. This is useful for sensitive sessions.
Using A Space Before The Command
If you start a command with a space, it is not saved to history. This works in Bash and Zsh by default. For example, typing ls (with a leading space) will not appear in history.
Make sure the HISTCONTROL variable includes ignorespace. Check with echo $HISTCONTROL. If it is not set, add export HISTCONTROL=ignorespace to your .bashrc.
Setting History Size To Zero
You can set the history size to zero in your shell configuration. Add export HISTSIZE=0 and export HISTFILESIZE=0 to ~/.bashrc. This stops all history recording. After saving, run source ~/.bashrc.
This is a drastic step. You will lose the ability to recall previous commands. Use it only for temporary sessions.
Using The HISTFILE Variable
You can point the history file to /dev/null. Add export HISTFILE=/dev/null to your .bashrc. This discards all history. It is a clean way to prevent saving.
Remember to remove this line when you want history back. Otherwise, you will never have a history file.
How To Clear History For All Users
If you have root access, you can clear history for all users. This is useful on shared systems. Each user has their own history file in their home directory.
Using A Script To Clear All Histories
Write a script that loops through all home directories. For example:
#!/bin/bash
for user in /home/*; do
if [ -f "$user/.bash_history" ]; then
cat /dev/null > "$user/.bash_history"
fi
done
Run this as root. It truncates the history file for every user. You can also delete the files if you prefer.
Clearing Root’s History
Root’s history is usually in /root/.bash_history. Use the same commands as for a regular user. Run history -c and then truncate or delete the file.
Be cautious: root history may contain critical commands. Only clear it if you are sure.
How To Clear History In Different Linux Distributions
The commands are the same across distributions. However, some distros use different default shells. Ubuntu and Debian use Bash. Fedora and CentOS also use Bash. Arch Linux uses Bash by default, but many users switch to Zsh.
If you are on a minimal system like Alpine Linux, the shell might be Ash. The history command works differently. For Ash, you can delete the history file directly. There is no history -c command.
For Bash On Ubuntu
Use history -c && history -w. This is the standard method.
For Bash On CentOS
Same as Ubuntu. The commands are universal for Bash.
For Zsh On Manjaro
Use history -c and then truncate ~/.zsh_history.
Common Mistakes When Deleting History
Many users forget to write the history after clearing it. Running history -c only clears memory. The file remains unchanged. Always follow with history -w to save the empty history.
Another mistake is deleting the history file while the shell is running. This can cause errors. It is safer to truncate the file instead of deleting it.
Some users accidentally delete the wrong file. Double-check the path before running rm. The file is ~/.bash_history, not .bashrc.
How To Verify History Is Deleted
After clearing, run history to see if any commands remain. It should show nothing or only the current session’s commands if you have not closed the terminal. Then check the file with cat ~/.bash_history. It should be empty.
You can also use wc -l ~/.bash_history to count lines. A zero means the file is empty.
Automating History Deletion
You can set up a cron job to clear history regularly. For example, to clear history every day at midnight, add this to your crontab:
0 0 * * * cat /dev/null > ~/.bash_history
This truncates the file daily. You can also add history -c but it only affects the current session. For a system-wide solution, use a script that runs for all users.
Security Implications Of Deleting History
Deleting history can be a security measure, but it also removes audit trails. On shared systems, administrators may rely on history to track misuse. If you clear history, you might raise suspicion.
Consider using HISTCONTROL to ignore commands that start with a space. This allows you to keep most history while protecting sensitive commands.
Frequently Asked Questions
How do I delete history in Linux permanently?
Run history -c then history -w. This clears both memory and the file. Alternatively, delete or truncate ~/.bash_history.
Can I delete history for a specific user?
Yes, log in as that user or use sudo to access their home directory. Clear their history file with the same commands.
Does clearing history affect other terminals?
No, each terminal has its own in-memory history. Clearing one does not affect others. However, writing to the file affects all terminals that read from it.
How do I delete history in Linux without a trace?
Use shred -u ~/.bash_history to overwrite and delete the file securely. Then clear memory with history -c.
What if I use a different shell like Fish?
Use history clear for Fish. For other shells, check the documentation. The concept is the same: clear memory and delete or truncate the file.
Now you know how to delete history in linux using multiple methods. Whether you need a quick cleanup or a permanent solution, these steps will keep your terminal private. Remember to always verify that the history is gone. Practice these commands on a test file first if you are unsure. Your command history is yours to control.