How to delete selected results from bash history?

history command shows all the results but we can filter to get particular command using history | grep searchingCommand. It is really helpful.

But the problem is it shows those commands also which was entered with typo error or which was unsuccessful. Then identifying the correct one is really a pain. I checked this link: Selective command-history in the terminal? but that was not my solution.

So is there a way to delete those commands from the history which was incorrect at the time entered or later?


Use:

history -d OFFSET

to delete the history entry at offset OFFSET even before it was added to your bash history file. To find out the right OFFSET, you need only to run history command. It's the number from from the start of the line which contain the history entry that you want to delete it.

And to save the modifications to the history use:

history -w

See more details in this guide.


Edit the file ~/.bash_history and delete the once with typos

For example, insert this command:

gedit ~/.bash_history

Edit something you like and after than save file and restart terminal. The root command is:

sudo -i 
inser your password
gedit ~/.bash_history

if you want to delete all history -c should do the trick


SYNTAX

history
history [n]
history -c
history -d offset
history [-anrw] [filename]
history -ps arg

KEY

-c Clear the history list. This may be combined with the other options to replace the history list completely.

-d offset Delete the history entry at position offset. offset should be specified as it appears when the history is displayed.

-a Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.

-n Append the history lines not already read from the history file to the current history list. These are lines appended to the history file since the beginning of the current Bash session.

-r Read the current history file and append its contents to the history list.

-w Write out the current history to the history file.

-p Perform history substitution on the args and display the result on the standard output, without storing the results in the history list.

-s The args are added to the end of the history list as a single entry.

source:

  • history Man Page | Bash | SS64.com

I'd like to add another method of modifying (or deleting) history entries, which I found rather accidentally when I was working with the bash:

To demonstrate this, start by executing the following three commands in bash:

$ echo 1
1
$ echo 2
2
$ echo 3
3

You can now select these commands again using the arrow keys or Ctrl+p and Ctrl+n.

Say you want to modify the first two commands. Move through history until echo 1 appears and change it to echo 1 - changed, but DO NOT PRESS ENTER. If you now move further through your history, this line stays in its modified state and you can move away from and back to it. Now move to the line echo 2 and change it to echo 2 - changed, again don't press enter. In order to save the changes to these lines, select any command in history except for these two, and hit Ctrl+c.

Of course, instead of modifying the history entry, you may also remove it which will result in an empty line at that entry. To delete the line currently displayed at the prompt, hit Ctrl+e (which jumps to the end of the line) followed by Ctrl+u (which deletes the text from the start of the line to the cursor).

See also https://unix.stackexchange.com/questions/195668/what-can-cause-an-item-to-be-deleted-from-my-bash-history/195726#195726 for a more detailed explanation of the technical background.

Simplest Way:

  1. ctrl+r to search the command you want to delete/modify.
  2. end to select the searched command to delete/modify
  3. Delete or modify the selected command (don't press enter or ctrl + c)
  4. up arrow(or ctrl+p) or down arrow(or ctrl+n) to select any other command.
  5. ctrl+c That's it!

Note:

This changes only the current session commands. If we want to change older commands and save the changes we need to run following command before closing the terminal:

history -w

If you want to immediately delete it form the same terminal first you have to add the following to your ~/.bashrc file.

PROMPT_COMMAND='history -a' 

and restart your terminal.

You can add it anywhere in .bashrc file .I have added as below along with other history related stuff.

enter image description here

Usually during a bash session the executed commands are not written into .bash_history until the session is terminated hence PROMPT_COMMAND='history -a' enters the command then and there into .bash_history.

Now whenever you make mistake or error in a command and want to delete it then and there just execute the following

sed -i '$d' ~/.bash_history

and tada it would be deleted.

To make it simpler you can alias it to something more simpler and use it such as

alias rh ='sed -i '\''$d'\'' ~/.bash_history'

So executing rh will remove the last executed command from history.

The above is temporary aliasing which only lasts for a session.To make it permanent or persistent add

alias rh = 'sed -i '\''$d'\'' ~/.bash_history' 

to .bashrc

NOTE

There should not be any space on both sides of =

If You Dont Want to Alias then You could also do the following

Make a commad name rh and place it in /bin directory:

  • Open a file say rh and paste following code, save and close:

    sed -i '$d' ~/.bash_history

  • Make rh executable and place it in /bin directory:

     chmod +x rh
     sudo cp rh /bin 
  • Now use rh command to delete recent command from history.


to remove multiple lines (e.g. from 1974 to 1990, check with history command):

  for i in `seq 1974 1990` ; do history -d 1974 ; done

note that the line number to remove is always the same, as you are removing that one constantly and go to the next.