18

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?

2
  • Is the history file not just a simple .txt file that can be edited?. Sorry in Windows right now and never actually tried. Apr 10, 2017 at 22:11
  • superuser.com/a/384378/74576 showed me where to delete any line I wanted from the terminal history.
    – Ryan
    Jul 23, 2021 at 16:08

5 Answers 5

18

You can achieve removal from the history file using the command line 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.

Source: Super User - Remove a certain line from Bash history file.

6
  • 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. Apr 10, 2017 at 22:18
  • Is there not a history man page? Sry I'm in Windows.... Apr 10, 2017 at 22:20
  • 1
    @EODCraftStaff Type man and the name of the command manual you want to see. In this case: man history. Apr 10, 2017 at 22:26
  • 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.
    – Stephen
    Apr 11, 2017 at 0:13
  • @AnthonyPinto You should consider this superuser question
    – Cadoiz
    Jan 6, 2022 at 15:40
12

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
    
3
  • 1
    (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. Nov 20, 2017 at 8:50
  • 1
    (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
    – theking2
    Oct 14, 2018 at 10:39
  • 4
    @theking2 For some systems pre-pending a space tells bash not to write the command to the history https://stackoverflow.com/questions/6475524.
    – chaptuck
    Jul 26, 2019 at 15:39
10

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.

17
  • 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? Apr 10, 2017 at 22:15
  • ps. tried it locally first and got "head: illegal line count -- -5" Apr 10, 2017 at 22:15
  • 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 Apr 10, 2017 at 22:20
  • Test the new code. works fine for me.
    – Ravexina
    Apr 10, 2017 at 22:25
  • Says, "head: illegal line count -- -5" still. :( Apr 10, 2017 at 22:26
2
  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
    
1

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

3
  • 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? Apr 10, 2017 at 22:24
  • If you want to hide commands from history use the HISTIGNORE variable, e.g. in your .bashrc, HISTIGNORE='clear:history:ls:cd' Apr 10, 2017 at 22:33
  • You can use gedit ~/.bash_history, don't have to use vim or nano. Apr 10, 2017 at 22:41

You must log in to answer this question.

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