Deleting history from ~/.bash_history
Solution 1
You just forgot the preceding dot, the command to open your (bash) terminal history file is
gedit ~/.bash_history
This file is only updated when you close a terminal.
To remove the last 10 lines from this file and don't get this command itself recorded, open a new terminal and execute the following chain of commands:
sed -n -e :a -e '1,10!{P;N;D;};N;ba' ~/.bash_history && history -c && exit
or
for i in {1..10}; do sed -i '$d' ~/.bash_history; done && history -c && exit
or
head -n -10 ~/.bash_history > ~/.b_h_2 && mv ~/.b_h_2 ~/.bash_history && history -c && exit
sed
orhead
respectively deletes the selected lines from~/.bash_history
,history -c
clears the terminal's history andexit
closes it.
Solution 2
The bash_history file is a hidden file, starting with a dot. You need to do
gedit ~/.bash_history
This will open up the file in gedit.
Solution 3
I realize this already has an accepted answer, but this is how I've been doing it since the '80s. It's a great way to hide your tracks and it's a multi phase approach. For the most part its one that isn't noticed unless one is really paying attention. Here is how its done.
Open a terminal/shell/session and do:
chmod 444 ~/.bash_history
exit the shell open a terminal/shell/session and do: Here are some creative ways to go about erasing the history permanently
1:
vi ~/.bash_history
9999999 dd
<esc>wq!
2 (create a fake history)
sudo echo "which ls" > ~/.bash_history
sudo echo "cd ~/;ls" >> ~/.bash_history
then for good measure:
chmod 444 ~/.bash_history
exit the shell
open a new shell and type
history
what makes it less noticeable is if one does a ls -ratl, the ~/.bash_history will eventually disappear in the rattle of the directory listing. It will not show up as a link which would attract unnecessary attention. Most people do not do a -a on their root, which is useful when emulation is desired.
The downside to this approach is you lose your ability to go back and retrieve your history. the actual ~/.bash_history file could also end up at the top of a directory listing, since it may end up with a really old modification date over time, which in itself might lead to discovery that something is amiss.
Solution 4
If you want to delete only certain parts of your command log then the above methods are just fine. If you want to stop command logging for a particular bash session, then issue the command
unset HISTFILE
To be more drastic, if you don't want any of your commands to be logged, then you can do
rm ~/.bash_history
ln -s /dev/null ~/.bash_history
Note that the first method would be in effect only for a single session, whereas the second method would stop command logging for all future sessions.
Related videos on Youtube
IDK
Updated on September 18, 2022Comments
-
IDK over 1 year
I have a couple questions about the terminal or command line history that is stored in
~/.bash_history
.I can see the file in the terminal with the
history
command but if I try to open it withgedit bash_history
the file is completely empty. Why?I've found how to delete a certain number of lines in the file from the terminal with this code line:
for i in {1..N}; do history -d N; done
where N is the number of lines (or commands) you want to delete, but now the history file shows this last command and thats not very smart if you're trying to cover your stuff. So the question is: How can I give the last code line and make sure this doesn't get recorded?
-
dessert over 6 years
-
Paddy Landau over 6 yearsFor future note, if you start any command with a space, that command will not be saved in your history.
-
Melebius over 6 yearsA shorter and hopefully faster alternative to
for
+sed
:head -n -N
where uppercaseN
is the number of lines to be removed. stackoverflow.com/a/13380679/711006 -
dessert over 6 years@Melebius Looks promising, but how do you redirect the output to the input file overwriting it?
head -n -N testfile > testfile
just clears the file! I added a workaround, however it's certainly not shorter. -
Melebius over 6 yearsYou are right, I forgot that
head
cannot work in-place likesed
. I was primarily looking for a singlesed
command replacing yourfor
loop. There are some options on the page I linked but they do not look as understandable as your code. -
dessert over 6 years@Melebius See sed1line.txt for that – I added it above, it's not really shorter but should be faster.
-
The Lightning Stalker over 6 yearsEdited as suggested Good enough now?
-
phuclv over 6 yearsif you use
9999999 dd
to delete all lines in the file then:%d
,:1,$d
ordG
work much better