12 Aug Quick Tip: How To Delete Commands from your Bash/Terminal History
Quick Tips #1
Let’s say you’re working through your terminal getting into system sensitive information or commands, you may not realize it but your terminal/bash keeps track of what you’re doing( honestly what doesn’t these days). That leads me to the quick tip of the week a new series of really short posts just covering one tip that may or may not help you. If it doesn’t help you, you’re probably a terrible person so stop being so judgemental. That’s the level of comedy gold you can expect here.
Anyway…Here We Go
To see what you’ve entered into your terminal/bash just type history. Your mind is blown I know.
So now you’re looking at a list of everything you’ve entered into your terminal. I’ve selected a small portion of my history to inspect below:
I don’t have many secrets in this capture so I’ll just pick line 388 to remove. You can find the line you want to remove and enter history, the -d flag and the number(388) of the line you’d like removed from your history.
That’s it that line is gone, now let’s type in history one more time and see what happened.
What you’ll see below is that line 388 was removed from the list and the proceeding lines were just moved down one spot so line 389 is now 388 and your secret/sensitive command is gone.
Using a text editor: Nano
Just in case you’re not sure what nano is, it’s just a text editor for Unix-like systems you use directly in your bash. So let’s go about it the fancy way using the nano editor and just type in nano .bash_history and from there you’ll be able to edit all the lines you want to be removed, just remove what you want, save it and you’re good to go.
Or…kill everything all at once.
Not a picky one hu? If you want to clean house completely, just type in history -c; history -w which will clear history in memory and write that to the HISTFILE file, meaning it’ll clear both memory and file history.
Want to work in stealth you Sneaky hacker?
If you don’t want your computer to log any of your commands for your current session, which is bonkers to me but hey I’m not judging you (yes I am). Just type in the above history -c; history -w to clear whatever you already have and then type in unset HISTFILE
Now you’re in ninja mode… an inefficient code writing ninja but still a ninja.
Also published on Medium.