8

I have a couple questions about the terminal or command line history that is stored in ~/.bash_history.

  1. I can see the file in the terminal with the history command but if I try to open it with gedit bash_history the file is completely empty. Why?

  2. 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?

2

7 Answers 7

18
  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.

  2. 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 or head respectively deletes the selected lines from ~/.bash_history, history -c clears the terminal's history and exit closes it.

4
  • A shorter and hopefully faster alternative to for + sed: head -n -N where uppercase N is the number of lines to be removed. stackoverflow.com/a/13380679/711006
    – Melebius
    Sep 4, 2017 at 14:06
  • 2
    @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.
    – dessert
    Sep 4, 2017 at 14:24
  • You are right, I forgot that head cannot work in-place like sed. I was primarily looking for a single sed command replacing your for loop. There are some options on the page I linked but they do not look as understandable as your code.
    – Melebius
    Sep 4, 2017 at 14:30
  • @Melebius See sed1line.txt for that – I added it above, it's not really shorter but should be faster.
    – dessert
    Sep 4, 2017 at 14:31
4

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.

1

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.

1

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.

1
  • if you use 9999999 dd to delete all lines in the file then :%d, :1,$d or dG work much better
    – phuclv
    Sep 5, 2017 at 12:45
0

1) Run sh; this spawns a subshell of ash, which doesn't record history at all. Edit from there

2) Add the line HISTCONTROL=ignoreboth to .bashrc, and do exec bash to permanently replace the current shell with a reinitialized one, then do all commands you don't want logged with a leading space.

0

since i cant comment yet, will add my comments as answer:

man history will answer it all.

answer to #1 is that history is on memory. history -a to save to file immediately. history -w will also save but might mess up other sessions.

answer #2: history -a, gedit, delete lines you want, history -r

0

The history isn't saved until you log out of the shell. Try logging out of the shell and then look at .bash_history again. The command history should be in there.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .