how do I remove the last 5 lines in bash history?

33,351

Solution 1

There are different ways to accomplish this task, but lets clarify something before going further.

There is a file named: ~/.bash_history, which contains your older terminal sessions history, whenever you close your terminal, your history will be saved there.

At the same time the history of your old sessions along with current session is temporary accessible by history commands until you close the terminal which then will be saved into ~/.bash_history file.

So if you remove 5 lines at the end of ~/.bash_history, then closing terminal will cause your current command to be accessible at next sessions.

So if I do a wc on .bash_history:

wc -l ~/.bash_history

Most of the time I'll get a smaller number than of history | wc -l.

If you want to remove the last 5 line of the file, you can use this command:

for i in {1..5}; do sed -i '$d' .bash_history; done;

And if you want to keep all history except last 5 command issued in current session run:

history | awk '{ print $2 }' | head -n -5 > .bash_history

Don't forget to run history -c too.

Solution 2

You can achieve removal from the history file using the commandline in two steps:

Typing history -d <line_number> deletes a specified line from the history in memory. Typing history -w writes the current in-memory history to the ~/.bash_history file. The two steps together remove the line permanently from the in-memory history and from the .bash_history file as well.

Ref: Super User

Solution 3

I think you need to try this its very easy & simple.

  1. How to delete history without any trace

    history -d $((HISTCMD-1)) && history -d NO_of_History_you_want_to_delete
    
  2. if you want to excute a command without leaving any trace.

    history -d $((HISTCMD-1)) && type_your_command_here_and_execute
    

Solution 4

  1. Write the current history to the history file, overwriting the history file's contents

    history -w
    
  2. Edit history as you wish

    vi ~/.bash_history
    
  3. Read the contents of the history file and use them as the current history.

    history -r
    

Solution 5

Open ~/.bash_history in your editor, and remove last 5 lines.

Share:
33,351

Related videos on Youtube

Anthony Pinto
Author by

Anthony Pinto

Updated on September 18, 2022

Comments

  • Anthony Pinto
    Anthony Pinto over 1 year

    How can I remove the last 5 lines in bash history? So that when I reload the Ubuntu server, or restart it they're not there at all?

    history -c only removes it from current session, but when I re-login I see the commands again, I want to clear the last 5.

    I've run:

    history
    

    Then i'll see the numbers of the commands e.g:

      489  cd ..
      490  cd .zshrc
      491  cat .zshrc
    

    Then I run for example:

    history -d 489
    history -c 
    

    Then i close terminal and reopen it and i still see line 489 it was only deleted for that current session, how do I delete it permentantly from all sessions going forward?

  • Anthony Pinto
    Anthony Pinto about 7 years
    Will that remove only the last 5 commands in bash? And permanently? e.g. so when I shut down terminal and re-login to ssh the last 5 won't be there?
  • Anthony Pinto
    Anthony Pinto about 7 years
    ps. tried it locally first and got "head: illegal line count -- -5"
  • Anthony Pinto
    Anthony Pinto about 7 years
    This is a lot closer, so I run history -wd 493 for example, and it works to delete it for good, however, then in history I have a bunch of history -wd's etc... so it's counterintuitive, how can i clear e.g. the last 5 commands all in one shot that would be best, and then the actual command i used to clear them itself.
  • Anthony Pinto
    Anthony Pinto about 7 years
    What seems to kind of work is when I run history -wd 493 for example, and it works to delete it for good, however, then in history I have a bunch of history -wd's etc... so it's counterintuitive, how can i clear e.g. the last 5 commands all in one shot that would be best, and then the actual command i used to clear them itself
  • EODCraft Staff
    EODCraft Staff about 7 years
    Is there not a history man page? Sry I'm in Windows....
  • Anthony Pinto
    Anthony Pinto about 7 years
    Not that great with VIM - can I run, history -wd 1, history -wd 2, history -wd 3, history -wd 4, history -wd 5, and then history -c ? Then next time i open all 5 will be gone plus the commands for that session to delete them?
  • Ravexina
    Ravexina about 7 years
    Test the new code. works fine for me.
  • Apologician
    Apologician about 7 years
    @EODCraftStaff Type man and the name of the command manual you want to see. In this case: man history.
  • Anthony Pinto
    Anthony Pinto about 7 years
    Says, "head: illegal line count -- -5" still. :(
  • Ravexina
    Ravexina about 7 years
    what is the head --version output?
  • Anthony Pinto
    Anthony Pinto about 7 years
    [~]$head --version head: illegal option -- - usage: head [-n lines | -c bytes] [file ...]
  • Ravexina
    Ravexina about 7 years
    What version of Ubuntu are you running?
  • Anthony Pinto
    Anthony Pinto about 7 years
    Oh was testing locally first, ok so if I run "history | cut -f4 -d' ' | head -n -5 > .bash_history" in Ubuntu, and then run history -c I won't see the command "history | cut -f4 -d' ' | head -n -5 > .bash_history" either right?
  • Ravexina
    Ravexina about 7 years
    you shouldn't get any output. it will read your history, removes the last lines, then write it to the .bash_history file.
  • Nick Rodriguez
    Nick Rodriguez about 7 years
    If you want to hide commands from history use the HISTIGNORE variable, e.g. in your .bashrc, HISTIGNORE='clear:history:ls:cd'
  • Anthony Pinto
    Anthony Pinto about 7 years
    so it will remove the last 5 lines, but it will still write: "history | cut -f4 -d' ' | head -n -5 > .bash_history" to the .bash_history file? Is there anyway to remove the last 5 lines, and the remove the command that removes the 5 liens as well? If we can do that, then we're all set! :)
  • Ravexina
    Ravexina about 7 years
    after running that command, use history -c. but after that don't run the suggested command again because it will remove all history (after running history -c).
  • EODCraft Staff
    EODCraft Staff about 7 years
    You can use gedit ~/.bash_history, don't have to use vim or nano.
  • Anthony Pinto
    Anthony Pinto about 7 years
    ran this command: "history | cut -f4 -d' ' | head -n -5 > .bash_history" it really messed up the history, if you close out of terminal then open terminal again and look at history it's all messed up,, don't think it's doing what it';s supposed to.. it removes a lot more than 5. can you look into it?
  • Ravexina
    Ravexina about 7 years
  • Ravexina
    Ravexina about 7 years
    @AnthonyPinto I made the answer more clear, I think it wort to read, also I changed cut to awk in second code. ;)
  • Anthony Pinto
    Anthony Pinto about 7 years
    Thanks! :) ps. any experience with dumping db's from postgresql to local machine? stackoverflow.com/questions/43334419/…
  • Ravexina
    Ravexina about 7 years
    @AnthonyPinto Unfortunately, never touched it.
  • Stephen
    Stephen about 7 years
    Use a space before your history delete command and it wont go to your history after it removes content from the history. Alternately, kill -s 9 $$ when you're done.
  • David Foerster
    David Foerster over 6 years
    (2.) doesn't answer the question and it would be far easier to prepend the traceless command with a space character to omit its addition to the history.
  • theking2
    theking2 over 5 years
    (1.) is both an excellent and elegant solution. I'm not sure how adding a space prevents it from being added to the history, David
  • chaptuck
    chaptuck almost 5 years
    @theking2 For some systems pre-pending a space tells bash not to write the command to the history https://stackoverflow.com/questions/6475524.
  • Cadoiz
    Cadoiz over 2 years
    @AnthonyPinto You should consider this superuser question