How To Clear History In Linux – Clearing Bash History File

Linux tracks every command you run, and clearing that history helps maintain security and privacy. If you are wondering how to clear history in linux, you are in the right place. This guide covers every method, from simple commands to advanced automation.

Your command history is stored in a file called .bash_history (or .zsh_history for Zsh). Every time you type a command, it gets saved. This is useful for repeating commands, but it can also expose sensitive data like passwords or file paths.

Let’s jump straight into the solutions. You will learn multiple ways to clear, delete, or manage your history. No fluff, just practical steps.

How To Clear History In Linux

The most direct way to clear your command history is using the history command. Open your terminal and type:

history -c

This clears the current session’s history. But it does not delete the history file on disk. To remove the file entirely, run:

history -w

Then delete the file:

rm ~/.bash_history

Alternatively, you can combine both steps with a single command:

history -c && history -w && rm ~/.bash_history

This is the fastest way to wipe everything. However, there are more precise methods depending on your needs.

Clearing History For The Current Session Only

If you only want to hide commands from the current terminal session, use:

history -c

This clears the in-memory history for that session. Future commands will still be recorded. To stop recording temporarily, run:

unset HISTFILE

This unsets the history file variable. No commands will be saved until you set it again.

Deleting Specific Commands From History

You don’t always need to clear everything. To remove a single command, first view your history with line numbers:

history

Find the line number you want to delete, then run:

history -d [line_number]

For example, to delete line 42:

history -d 42

You can also delete a range of lines. Use a loop or manually remove multiple lines. This method is precise and leaves other commands intact.

Clearing History Without Leaving A Trace

Some users want to clear history without any record of the action. One trick is to run a command that overwrites the history file with random data before deleting it. Use:

shred -u ~/.bash_history

This securely deletes the file, making recovery difficult. Then create a new empty file:

touch ~/.bash_history

Another approach is to set the history file size to zero:

cat /dev/null > ~/.bash_history

This empties the file without deleting it. Both methods are effective for privacy.

Disabling History Completely

If you never want commands saved, you can disable history entirely. Edit your shell configuration file (~/.bashrc for Bash, ~/.zshrc for Zsh):

nano ~/.bashrc

Add these lines:

export HISTSIZE=0
export HISTFILESIZE=0

Save and exit. Then reload the file:

source ~/.bashrc

This sets the history size to zero. No commands will be stored. To re-enable it later, change the values to something like 1000.

Clearing History For Other Users

If you have root access, you can clear history for other users. First, switch to root:

sudo su

Then navigate to the user’s home directory:

cd /home/username

Clear their history file:

cat /dev/null > .bash_history

You can also delete the file entirely. Be careful—this action cannot be undone.

Automating History Clearing

You can set up a cron job to clear history automatically. Edit your crontab:

crontab -e

Add a line to clear history every day at midnight:

0 0 * * * cat /dev/null > ~/.bash_history

Save and exit. This ensures your history is wiped daily. You can adjust the schedule as needed.

Using A Script To Clear History

Create a simple script to clear history with one command. Open a new file:

nano clear_history.sh

Add the following:

#!/bin/bash
history -c
history -w
cat /dev/null > ~/.bash_history
echo "History cleared."

Save the file and make it executable:

chmod +x clear_history.sh

Run it whenever needed:

./clear_history.sh

This script combines multiple steps into one. You can also move it to a directory in your PATH for easy access.

Clearing History In Different Shells

Different shells store history differently. Here are commands for common shells:

  • Bash: history -c and delete ~/.bash_history
  • Zsh: history -c and delete ~/.zsh_history
  • Fish: history clear
  • Ksh: history -c and delete ~/.sh_history

Always check which shell you are using. Run echo $SHELL to confirm.

Preventing Commands From Being Saved

You can prevent specific commands from being saved by adding a space before them. For example:

 ls -la

Note the leading space. This only works if the HISTCONTROL variable is set to ignorespace or ignoreboth. Check your current setting:

echo $HISTCONTROL

If it is not set, add this to your ~/.bashrc:

export HISTCONTROL=ignoreboth

This ignores commands with a leading space and also ignores duplicate commands.

Clearing History On System Shutdown

You can configure your system to clear history automatically when you log out. Add this to your ~/.bash_logout file:

cat /dev/null > ~/.bash_history

This runs every time you exit a login shell. It is a clean way to ensure history is never left behind.

Using The history Command Options

The history command has several useful options:

  • -c: Clear the current session history
  • -d offset: Delete the history entry at the given offset
  • -a: Append the current session history to the history file
  • -w: Write the current history to the history file
  • -r: Read the history file and append its contents to the current history

These options give you fine-grained control over your history.

Common Mistakes When Clearing History

Many users think history -c is enough. It is not—it only clears the in-memory list. The file still exists. Always follow up with history -w and file deletion if needed.

Another mistake is forgetting to reload the shell configuration after making changes. Use source ~/.bashrc to apply updates.

Some users accidentally delete the wrong file. Double-check the path before running rm. Use ls -la ~/.bash* to see all history files.

Recovering Deleted History

If you delete your history file, it is gone. There is no built-in recovery. However, if you have backups, you can restore it. Some systems keep logs in /var/log/, but these are not command history files.

To avoid accidental loss, consider backing up your history file periodically:

cp ~/.bash_history ~/.bash_history.backup

This gives you a restore point if needed.

Securing Your History With Encryption

For advanced users, you can encrypt your history file. Use gpg to encrypt it:

gpg -c ~/.bash_history

Then delete the original file. To use history, decrypt it first. This is overkill for most users but adds a layer of security.

History Management Best Practices

Here are some tips to keep your history clean:

  • Set a small history size (e.g., 500 lines)
  • Use HISTCONTROL=ignoreboth to skip duplicates and spaces
  • Clear history regularly with a cron job
  • Never store passwords in commands—use prompts instead
  • Use history -d to remove sensitive commands immediately

These practices reduce the risk of exposing sensitive information.

FAQ: How To Clear History In Linux

1. Does history -c delete the history file?

No. It only clears the current session’s in-memory history. You must also delete or empty the file on disk.

2. Can I clear history for all users at once?

Yes, with root access. Use a loop to delete each user’s history file, or run a script that targets all home directories.

3. How do I clear history in a non-Bash shell?

Each shell has its own command. For Zsh, use history -c. For Fish, use history clear. Check your shell’s documentation.

4. Will clearing history affect my current session?

Clearing the history file does not affect the current session. You need to run history -c to clear the in-memory list.

5. Is there a way to clear history automatically after each command?

Yes. You can set PROMPT_COMMAND to clear history after each command. But this is not recommended because it defeats the purpose of history.

Final Thoughts

Knowing how to clear history in linux is essential for privacy and security. Whether you use the simple history -c command or automate the process with scripts, you have full control over your command logs.

Remember to clear both the in-memory history and the file on disk. Use the methods that fit your workflow. With these techniques, you can keep your terminal sessions clean and secure.

Experiment with the different approaches. Find what works best for you. And always double-check before deleting files—history is easy to clear but hard to recover.