After typing a few commands in the terminal you may eventually want to flush it out. A quick (but insufficient way) to clear the command history on your Mac or Linux box is to type:
This purges all the commands for the current session stashed in a file called: ~/.bash_history.
That’s good but I’m going to show you a better way to kill the history for all sessions that way if you have multiple terminal windows open you’ll make sure your history is deleted from all of them.
But first let’s explore the .bash_history file.
You can check out your most recent commands by typing
or to see the full shebang just type:
Filenames preceded by a dot are hidden so ~/.bash_history isn’t visible in a basic directory listing unless you explicitly attempt to view it.
You can list all the files (including hidden files) on your Mac or Linux computer by typing the following command in the terminal:
Note the a after the l. This tells Linux to list all files.
.bash_history and a few of his bash friends show up.
I ran the pwd (Print Working Directory) command in the third line from the bottom of the screenshot to show you that .bash_history is actually stored in my home folder.
That’s what the little tilde before the forward slash means: it’s just an alias for my user directory.
When history fails
Sometimes you might notice that history -c doesn’t actually clear all your history.
If you have multiple terminal windows open, history -c only clears the history from the currently active terminal window. All the other terminals retain the history and if you log off and then back on again all your history will be sitting there waiting for you.
Therefore we need to type an additional command: history -w to clear the history from all terminal sessions.
As a shortcut to typing the word history twice you can usually compress the command like so:
Ahh, that did the trick on my Mac!
“Null” you know how to do it
But that’s not all.
There’s one other way to kill your bash history. This last trick overwrites your command history with empty content:
cat /dev/null > ~/.bash_history && history -cw && exit
I know the command looks monolithic so let me break it down:
First you’ll notice the command begins with my admiration for felines…
Actually, in the Linux world, cat has nothing to do with those selfish furry critters who only care about themselves and hiss at you when you try to pet them.
cat is short for catinate because it’s often used to chain files and redirect text inputs.
In this case, we’re saying:
cat /dev/null > .bash_history
Which means we want to dump the contents of a file called /dev/null to our command history file, .bash_history.
Now why would we do that?
/dev/null is an interesting guy.
He’s perhaps the most enigmatic file on your Linux or Mac because he’s a black hole.
Any data written to /dev/null get’s
I honestly don’t know what it’s used for but programmers have their purposes.
The bottom line is that we’re overwriting .bash_history with an empty file then flushing the command history and exiting.
Those && ampersands are like chains that link together separate commands on a single line.
The Bottom Line
When it’s time to dump the command history on your Mac or Linux boxes you have three options:
- history -c
- history -cw
- cat /dev/null >~/.bash_history && history -cw && exit
That last one works great when you’re done for the day because it exits the session.