# How to Delete History in Linux


Deleting your command history in Linux is a useful skill, especially if you want to keep your activities private or clean up your system. Whether you're a beginner or an experienced user, understanding how to manage and delete history files can help you maintain control over your Linux environment. In this article, I’ll guide you through the different methods to delete your command history safely and effectively.

You might wonder why deleting history matters. Sometimes, your command history can reveal sensitive information like passwords or server details. Other times, you just want to start fresh without old commands cluttering your terminal. Whatever your reason, I’ll show you step-by-step how to clear your history using various tools and commands.

## Understanding Linux Command History

Linux keeps a record of the commands you type in the terminal. This record is called the command history. It helps you recall previous commands, making your work faster and easier. The history is stored in a file, usually named `.bash_history` for Bash shell users.

Here’s what you should know about Linux command history:

- **Location:** The history file is typically located in your home directory as `.bash_history`.
- **Shell-specific:** Different shells like Bash, Zsh, or Fish have their own history files.
- **Session-based:** Commands are saved when you close the terminal session.
- **Size limits:** History files have size limits, which you can configure.

Knowing this helps you understand where and how to delete your history.

## How to View Your Command History

Before deleting, you might want to see what’s in your history. You can do this by typing:

```bash
history
```

This command lists all the commands you’ve entered in the current shell session. You can also use:

```bash
cat ~/.bash_history
```

to view the saved history file directly.

## Deleting History Using the History Command

One of the easiest ways to clear your command history is by using the `history` command itself.

- To clear the current session’s history, type:

  ```bash
  history -c
  ```

  This clears the history list in memory but does not delete the history file.

- To clear the history file as well, you need to combine commands:

  ```bash
  history -c && history -w
  ```

  Here, `-c` clears the history in memory, and `-w` writes the empty history back to the file, effectively erasing it.

## Manually Deleting the History File

If you want to delete the history file directly, you can remove or empty the `.bash_history` file.

- To delete the file:

  ```bash
  rm ~/.bash_history
  ```

- To empty the file without deleting it:

  ```bash
  > ~/.bash_history
  ```

Deleting the file removes all saved commands, but a new history file will be created automatically when you start a new session.

## Preventing History from Being Saved

Sometimes, you might want to run commands without saving them to history. Here are some ways to do that:

- **Prefix commands with a space:** If you set the environment variable `HISTCONTROL=ignorespace`, any command starting with a space won’t be saved.

  ```bash
  export HISTCONTROL=ignorespace
  ```

- **Disable history temporarily:**

  ```bash
  set +o history
  ```

  This turns off history recording until you enable it again with:

  ```bash
  set -o history
  ```

- **Use `unset HISTFILE`:** This prevents the shell from writing history to a file.

  ```bash
  unset HISTFILE
  ```

## Clearing History in Other Shells

If you use shells other than Bash, the history files and commands differ.

### Zsh

- History file is usually `.zsh_history`.
- Clear history with:

  ```bash
  > ~/.zsh_history
  ```

- Clear current session history:

  ```bash
  history -c
  ```

### Fish Shell

- History file is located at `~/.local/share/fish/fish_history`.
- Clear history by deleting the file:

  ```bash
  rm ~/.local/share/fish/fish_history
  ```

- Or clear history within Fish shell:

  ```fish
  history clear
  ```

## Automating History Deletion

If you want to regularly clear your history, you can automate the process.

- Add commands to your `.bash_logout` file to clear history when you log out:

  ```bash
  history -c
  history -w
  ```

- Use cron jobs to delete history files periodically.

Automating helps maintain privacy without manual effort.

## Why You Should Be Careful When Deleting History

Deleting history can be helpful, but it also has downsides:

- You lose the ability to recall past commands.
- Some system audits rely on history for troubleshooting.
- If you share your system, deleting history might raise suspicion.

Always consider why you want to delete history and whether it’s necessary.

## Tips for Managing Linux History Effectively

To keep your history useful and secure, try these tips:

- Regularly clear sensitive commands using `history -d <line_number>`.
- Use `.bashrc` to customize history behavior, like ignoring duplicates.
- Limit history size by setting `HISTSIZE` and `HISTFILESIZE`.
- Use encrypted home directories for extra security.

## Conclusion

Deleting your command history in Linux is straightforward once you know the right commands and files to target. Whether you want to clear your current session, delete the history file, or prevent commands from being saved, there are multiple methods to suit your needs. Remember to handle history carefully, balancing privacy with the convenience of having a command record.

By managing your history wisely, you can keep your Linux environment clean and secure. Try out the commands and tips shared here to take control of your terminal history today.

### FAQs

#### How do I delete a specific command from my Linux history?

Use `history` to find the command number, then delete it with `history -d <number>`. For example, `history -d 25` removes the 25th command.

#### Will deleting `.bash_history` remove all my past commands?

Yes, deleting `.bash_history` removes all saved commands, but new commands will be saved in a new file after the next session.

#### Can I stop Linux from saving any command history?

Yes, by unsetting the `HISTFILE` variable with `unset HISTFILE`, the shell won’t save commands to a file.

#### How do I clear history in the Zsh shell?

Clear the current session with `history -c` and empty the file with `> ~/.zsh_history`.

#### Is it safe to delete command history on a shared system?

It depends. Deleting history can protect privacy but may affect system audits or troubleshooting. Use caution and consider system policies.
