How To Clear History Linux : Using History -C Command

Your Linux terminal remembers each command, and clearing that history is a straightforward privacy practice. If you’ve ever typed a password, a sensitive file path, or just a command you’d rather not have logged, knowing how to clear history linux is essential for maintaining your privacy and keeping your terminal tidy.

This guide walks you through every method, from quick one-liners to permanent settings. You’ll learn to delete specific commands, wipe the entire history, and even stop Linux from recording commands at all. Let’s get started.

Why Clearing Your Linux History Matters

Your shell history is stored in a hidden file, usually ~/.bash_history for Bash users. Every command you run gets appended there. Over time, this file can become cluttered with typos, failed attempts, or sensitive information.

If you share your computer or use a server with multiple users, that history is a goldmine for anyone looking to see what you’ve been doing. Clearing it regularly is a simple habit that protects your privacy and keeps your workspace clean.

How To Clear History Linux: The Quickest Methods

Here are the fastest ways to wipe your command history. These work on most Linux distributions using the Bash shell.

Method 1: Use The History Command

The history command itself has a built-in option to clear everything. Open your terminal and type:

history -c

This clears the current session’s history. But it doesn’t delete the history file on disk. To also wipe the file, run:

history -w

This writes the current (now empty) history to the file. Combined, these two commands give you a fresh start.

Method 2: Delete The History File Directly

You can simply remove or truncate the history file. For Bash, that file is ~/.bash_history. Run:

cat /dev/null > ~/.bash_history

This empties the file without deleting it. Alternatively, use:

rm ~/.bash_history

But be careful—if you delete it, the shell will create a new empty file next time you log in. That’s fine, but it might cause issues if you have custom settings that expect the file to exist.

Method 3: Use The Kill Command

Another quick trick is to use kill -9 on the shell process, but that’s extreme. Instead, you can clear history by running:

history -c && history -w

This combines both steps into one command. It’s my go-to for a fast cleanup.

How To Clear History Linux For Specific Commands

Sometimes you don’t want to wipe everything. Maybe you just want to remove a single embarassing command or a few lines. Here’s how.

Delete A Specific Command By Number

First, view your history with line numbers:

history

Each line has a number. To delete a specific line, say number 42, run:

history -d 42

You can delete a range too. For example, to delete lines 10 through 20:

for i in {10..20}; do history -d $i; done

Note that after deleting one line, the numbers shift. It’s easier to delete from highest to lowest if you’re removing multiple lines.

Remove The Last Command

If you just ran a command you regret, you can delete it immediately. The last command is always at the end of the history. Use:

history -d $((HISTCMD-1))

This deletes the command you just ran. It’s a lifesaver when you accidentally type a password in the terminal.

Clear History For The Current Session Only

If you don’t want to affect the permanent history file, just clear the session history. Run:

history -c

This only clears the in-memory history for your current terminal session. The file remains untouched. When you close the terminal, the session history is lost anyway, but this gives you a clean slate right now.

How To Clear History Linux Permanently: Disable History

If you never want Linux to remember your commands, you can disable history entirely. This is useful for sensitive work or if you’re on a shared system.

Set HISTSIZE And HISTFILESIZE To Zero

Open your shell configuration file, usually ~/.bashrc or ~/.bash_profile. Add these lines:

export HISTSIZE=0
export HISTFILESIZE=0

Save the file and run source ~/.bashrc to apply the changes. This tells Bash to keep no commands in memory and to truncate the history file to zero.

Use The HISTFILE Variable

You can also point the history file to /dev/null, which discards everything written to it. Add this to your .bashrc:

export HISTFILE=/dev/null

Now every command is written to the void. This is a more aggressive approach, but it works perfectly.

Set HISTCONTROL To Ignore Duplicates And Spaces

If you don’t want to disable history entirely, you can at least prevent sensitive commands from being saved. Add this to your .bashrc:

export HISTCONTROL=ignoreboth:erasedups

This ignores commands that start with a space (so you can type password and it won’t be saved) and removes duplicate entries. It’s a great middle ground.

How To Clear History Linux For Other Shells

Not everyone uses Bash. If you’re using Zsh, Fish, or another shell, the commands are slightly different.

Zsh History

Zsh stores history in ~/.zsh_history. To clear it:

history -c
rm ~/.zsh_history

Or just truncate the file:

cat /dev/null > ~/.zsh_history

Fish Shell History

Fish uses ~/.local/share/fish/fish_history. To clear it:

history clear

This is a built-in Fish command. Simple and clean.

Other Shells

For shells like Ksh or Tcsh, the principle is the same: find the history file and delete or truncate it. Check your shell’s documentation for the exact file location.

How To Clear History Linux Using Graphical Tools

If you prefer a GUI, most file managers let you delete hidden files. Press Ctrl+H to show hidden files, then navigate to your home directory and delete .bash_history or .zsh_history.

Some terminal emulators like GNOME Terminal also have a “Clear” option in the menu, but that only clears the screen, not the history file. Don’t confuse the two.

How To Clear History Linux Automatically

You can set up a cron job to clear your history on a schedule. For example, to clear it every day at midnight, add this to your crontab:

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

Or, if you want to clear it every time you log out, add the same command to your ~/.bash_logout file:

cat /dev/null > ~/.bash_history

This ensures your history is wiped clean every time you end a session.

Common Mistakes When Clearing History

Even experienced users make errors. Here are a few to watch out for.

  • Forgetting to write the file: Using history -c alone only clears the session. You must also run history -w to update the file.
  • Deleting the wrong file: Make sure you’re deleting the correct history file for your shell. Check with echo $HISTFILE.
  • Not sourcing the config file: If you edit .bashrc, you need to run source ~/.bashrc or restart the terminal for changes to take effect.
  • Assuming it’s permanent: Clearing history only removes past commands. New commands will still be recorded unless you disable history.

How To Clear History Linux On A Server

If you’re logged into a remote server via SSH, the same commands work. But be aware that other users might have access to the history file if permissions are loose. Always check file permissions with ls -la ~/.bash_history.

For shared servers, consider using unset HISTFILE before starting your session. This prevents any history from being written at all.

Frequently Asked Questions

Does Clearing History Affect My Current Terminal Session?

Yes, if you use history -c, it clears the in-memory history for that session. But the screen content remains. You can use clear or Ctrl+L to clear the screen separately.

Can I Recover Cleared History?

Generally no. Once you truncate or delete the history file, the data is gone. Some file recovery tools might work if you act quickly, but it’s not reliable. Be sure before you clear.

How Do I Clear History For All Users?

You need root access. Each user has their own history file in their home directory. You can write a script to loop through all home directories and truncate each .bash_history file.

Does Clearing History Improve Performance?

Not really. The history file is small, so clearing it has no noticeable effect on performance. It’s purely for privacy and organization.

What If I Use Multiple Terminals?

Each terminal session has its own in-memory history, but they all share the same history file. Clearing the file affects all future sessions, but current sessions keep their in-memory history until you close them.

Final Tips For Managing Linux History

Knowing how to clear history linux is just the first step. Here are a few extra tips to keep your terminal private and efficient.

  • Use history -a to append the current session to the file immediately. This is useful if you want to save commands from one session for later.
  • Set HISTTIMEFORMAT to include timestamps. Add export HISTTIMEFORMAT="%F %T " to your .bashrc to see when each command was run.
  • Limit history size. Set HISTSIZE=1000 and HISTFILESIZE=2000 to keep history manageable.
  • Use history | grep to search for past commands. This is faster than scrolling through hundreds of lines.

Clearing your Linux history is a simple but powerful habit. Whether you’re protecting sensitive data or just decluttering, the methods in this guide give you full control. Start with the quick commands, then customize your setup to match your workflow. Your terminal will thank you.