Ubuntu – Deleting history from ~/.bash_history

bashbash-historycommand linegedit

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?

Best Answer

  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.