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?

  • 5
    Please, change the accepted answer, there are other with more that 100 up votes. – greuze Sep 27 '17 at 14:20
  • 1
    @greuze: Changed to the best answer. Count of votes doesn't mean that it's the best! – Saurav Kumar Apr 20 '18 at 13:09

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
$ echo 2
$ echo 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!


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
| improve this answer | |
  • Many thanks Stefan! I must say that you brought an awesome things. Using this I found the most simplest way to modify/delete the history: 1. ctrl+r to search the command you want to delete/modify. 2. end to move the command and delete/modify the command. 3. ctrl+n to go to next line and then 4. ctrl+c – Saurav Kumar Feb 1 '18 at 14:10


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.

| improve this answer | |
  • not bad :) Good to get a new way.. – Saurav Kumar Feb 27 '14 at 11:34
  • 10
    @SauravKumar Not bad?!? Probably is the best way... – Radu Rădeanu Mar 27 '14 at 17:04
  • 5
    READ FIRST: Just to be clear you must do history -w afterwords to save the changes. Also OFFSET means which number that history displays. If the line you are try to delete is 873 then use history -d 873. – Cammy_the_block Aug 2 '14 at 16:07
  • 2
    @RaduRădeanu Use history -w after history -d to save changes. – Cammy_the_block Aug 2 '14 at 16:09
  • 20
    This should be the correct answer. – topher Jun 3 '15 at 11:47

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


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


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


| improve this answer | |
  • Thanks it is helpful! Do you know how to delete the entry at the time when entered from command line? Or is there any command to delete the entry without opening the .bash_history? – Saurav Kumar Sep 12 '13 at 9:39
  • 1
    @bladed19899 history -c doesn't work When I close terminal and open it again histories are not deleted. -1 your answer – αғsнιη Jun 14 '14 at 20:56
  • 1
    @KasiyA, -c Clear the history list. This may be combined with the other options to replace the history list completely. source – blade19899 Jun 16 '14 at 8:44
  • 2
    @KasiyA Use history -w after history -c to write the changes. – Cammy_the_block Aug 2 '14 at 16:10

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


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.

| improve this answer | |
  • Where to add PROMPT_COMMAND='history -a' in .bashrc file? And what exactly it will do? I tried sed -i '$d' ~/.bash_history it's working without editing .bashrc!! But alias thing is not working.. I will accept your answer if you just elaborate my question and make alias working.. :) – Saurav Kumar Sep 12 '13 at 15:15
  • @SauravKumar have edited the answer. Had missed the single and double quotes in aliasing command.. My bad.. :P – Stormvirux Sep 12 '13 at 16:29
  • Believe me or not.. alias thing is still not working.. It executes without any error but doesn't delete the entry from .bash_history file.. :P any way.. Not a problem.. I made my own command and placed it in /bin directory. Since because of your command: sed -i "$d" ~/.bash_history it is working fine. I accept your answer.. :) – Saurav Kumar Sep 12 '13 at 16:48
  • If you don't mind, I would like to edit your answer so that it would be helpful to others.. – Saurav Kumar Sep 12 '13 at 16:51
  • Actually I edited it. You've to approve it.. – Saurav Kumar Sep 12 '13 at 17:27

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.

| improve this answer | |
  • The note is very important. – Weijun Zhou Nov 24 '18 at 14:30

If you are using "set -o vi" option in bash (very useful to search back with Escape key), then you can search for the line and delete the line with "dd" in the same way you do in "vi".

| improve this answer | |
  • I find that I have to type j or k after dd to make it actually clear the command from history. If I type another command, or just press Enter, bash seems to assume it's just a new command and that it should retain the old command in history. It's also interesting to note that when it's done right, it clears the command, but doesn't delete it—the result is blank history entries. But this is good enough for removing passwords that I was forced to type at the command line, and much easier than other answers, so thanks very much for posting this! – Michael Scheper Jan 12 '18 at 0:30

I had a situation where I had superfluous entries in my bash history from a Yubikey- maybe 50 or so, matching 'ccccc.....'. Ideally, I wanted a way to delete entries matching a pattern, which I suppose you could do with sed or something, assuming your history ignores blanks.

Anyway, I got lazy and ended up using vim, searching for 'cccc' and using 'dd' while cycling through every match. Didn't take more than a minute.

| improve this answer | |
  • 1
    If you are doing this in vim then you could also do :g/cccc/d or :g/^cccc/d: or use a macro qqnddq then @@ (assumes your last search was for ccccc). – htaccess Jan 12 '19 at 3:19
  • @htaccess That would certainly have been easier, but I kind of wanted to visually inspect the rest of the file at the same time. – Dylan_Larkin Jan 24 '19 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.