Clearing Terminal history in Linux and why it is important to do on occasion
Social Share:
Saturday, August 17, 2024 at 1:44 PM | 5 min read
Last modified on Saturday, June 13, 2026 at 1:15 PM
#bash, #command line, #linux, #history, #security, #terminal history, #vim

Photo by Enes Bayraktar on pexels.com
Table of Contents
- What is the history command?
- Why history is not actually a command
- history
- Removing history
- bash history, privacy, and security
- Conclusion
- Related Resources
I wrote this post to remind myself of how to remove .bash_history bloat and only keep unique and viable commands in my Linux Mint .bash_history.
What is the history command?
The history command in Linux (and macOS) stores a list of commands that have been used in terminal sessions, and it permits us to reuse them instead of retyping them.
Why history is not actually a command
If I run which history in Terminal, no output is returned. This confirms that history is not actually a command. history is a built-in keyword of our shell. Another example is the cd command. It also is a builtin keyword and not a command. And because history is written into the particular shell we are using at any given time, there can be a difference in its behavior across shells. But since I use bash in Linux, I will be discussing bash-specific history behavior in my particular Linux distro (Linux Mint).
history
If I run history in Terminal, I get back 428 lines, each line containing a command I have run in Terminal. The following is a sneak peek at history stdout:
1 ls 2 cd Desktop 3 ls 4 cd cron-job-scripts/ 5 ls 6 mailx 7 sudo apt update 8 sudo apt upgrade 9 sudo apt install mailx ... 428 history
If I wanted to run a specific command using its line number, I would run the following:
!84
Which would return the following:
cd desktop-backup/
This is the command at line 84 in (Terminal) history. I could also access and run this command by running the following:
!cd desktop-backup/
Sometimes, as in this case, we get back something like the following:
cd desktop-backup/ desktop-backup/ bash cd: too many arguments
What I should have done was !+ line number of the command. For example, if cd desktop-backup/ was located on line 105 in my bash history, I would run !105, which would return cd desktop-backup/. Then I could rerun the command. The whole idea behind referring to the .bash_history file is to refresh my memory on a command I might have somewhat forgotten.
So I tried another one that does not result in any ambiguity:
!clear
It runs the clear command, clearing the Terminal window. Using ! before the command itself prompts history to search for the last command that matches the pattern I provided. In this case, it is clear.
But I digress. Let's get to the task at hand.
Removing history
There are several ways to remove some or all commands saved to history.
Deleting a command by line number
If I want to delete a command saved to history by line number, I would run the following command:
history -d 423
This removes the command at line 423. This is a great command to know if you have mistyped a command, for example, and want to remove it from history because it is essentially useless. But it can also be used to remove specific commands containing sensitive information. Why? What if you had to use history or some other related tool or command in a presentation, and you wanted to make sure that nothing sensitive would appear on the screen, and you didn't want to completely remove your history. This would be one way to do that.
history -c
The history -c command removes the contents of a specific terminal session's history. If I run history -c and then check to see if the contents of my terminal session's history was indeed completely removed, I would run history again, and something like the following would be returned:
1 history
Manually removing the contents of .bash_history
history stores the commands run in Terminal in a file called .bash_history, which is located in a user's home directory. If, after running history -c, I want to double check if all my history has actually been removed, I can run vim .bash_history to open up the file in Vim. And something like the following would appear:
# The contents of .bash_history contained 426 lines. I am truncating it. ... which diff diff -s secret_message.txt decrypted_super_secret.txt sha512sum secret_message.txt
history -c only clears the history of a specific terminal session, but not the entire contents of .bash_history itself. Some articles state that it completely removes all your history, but it does not. After running history -c, your .bash_history still contains the history of the commands you have run in Terminal.
If I wanted to manually remove the contents of .bash_history, I would have to open the file and manually delete the contents of the file. I could open the file using vim .bash_history inside my home directory where the file resides, Control + A the file contents, and then hit the delete key and remove the file contents.
I could also run vim .bash_history and be taken into the Vim interface. There, I would do the following:
- I go into Vim normal mode by pressing the esc key
- I enter command mode by pressing :%d to delete everything
- To save my changes, I press the esc key + :x. This saves my changes and takes me out of Vim.
If, after deleting the entire contents of .bash_history, subsequent commands are not saved to .bash_history (it remains empty), run the following command:
history > .bash_history
Then open .bash_history with the vim .bash_history command, and you should see your stored terminal history reappear. That is because you have redirected history as stdout to the .bash_history file.
Next, to make sure that everything is working as it should, run a few commands in Terminal, and then re-open .bash_history to see if those commands are being saved to .bash_history. When I did this and re-opened .bash_history, I saw that new commands were not being added to .bash_history. Even when I quit Terminal and started a new session. So I had to do the following to do a history "reset":
# I ran this inside my home directory /home/maria where my .bash_history resides rm .bash_history touch .bash_history
Once I re-created the .bash_history file, I saw that terminal history was saved to .bash_history, but commands from a terminal session would save to the .bash_history file when I would exit out of a terminal session and start a new one. But it worked just fine and as expected!
Clearing .bash_history using the echo command
In order to remove the complete contents of the ~/.bash_history file, I ran the following command:
# I did not have to add ~/ in front of .bash_history because I ran the command inside my home directory echo > .bash_history
And when I opened up .bash_history using vim .bash_history, the file was completely empty. This is the ONLY command that successfully removed the contents of my .bash_history file!
bash history, privacy, and security
Clearing your bash history is not just about keeping things "clean". It's also about safeguarding your security and privacy. By clearing your .bash_history on a regular basis, you can help protect sensitive information that you may have used in a command, such as passwords or other confidential data. You might want to clear some lines from your Terminal history if you are going to share a terminal screen containing that history. Many a presenter has overlooked that possibility. Revealing sensitive information that has been stored in Terminal history could potentially result in security breaches.
Conclusion
Occasionally, I clear my .bash_history because of several reasons. Perhaps I made a typo when typing a command, and want to remove it since it serves no purpose. Or perhaps I am going to make a presentation which involves sharing my .bash_history in Terminal, and it contains sensitive information such as passwords, API keys, hostnames, or internal paths. And anyone with access to my computer could potentially retrieve that sensitive information. Therefore, I'd want to clear my history after running commands that include secrets or credentials before sharing my system, VM, or shell with someone else. There are several ways to do this. One is to remove a command by line number or manually in Vim. Another is to remove the contents of a particular terminal session's history. And lastly, I can empty my .bash_history with the echo > .bash_history command. Which method I choose depends on the end goal.
Related Resources
- Editing your zsh history
- How to use the history command in Linux: by Steve Morris, opensource.com
- Select all in Vim / Vi: warp.dev
- Clear Bash History: Bash Shell and Command Line Guide: I/O Flood
- How to Clear the Terminal History in Linux: Geeks for Geeks
- How to Clear History in Linux: Step-by-Step Guide: techyorker.com