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?

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

7 Answers 7



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.

  • not bad :) Good to get a new way.. Feb 27, 2014 at 11:34
  • 10
    @SauravKumar Not bad?!? Probably is the best way... Mar 27, 2014 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.
    – cubecubed
    Aug 2, 2014 at 16:07
  • 2
    @RaduRădeanu Use history -w after history -d to save changes.
    – cubecubed
    Aug 2, 2014 at 16:09
  • 20
    This should be the correct answer.
    – topher
    Jun 3, 2015 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.


  • 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? Sep 12, 2013 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 Jun 14, 2014 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, 2014 at 8:44
  • 2
    @KasiyA Use history -w after history -c to write the changes.
    – cubecubed
    Aug 2, 2014 at 16:10

Warning (03/05/23):

The method described in my original answer isn't really safe. Scroll to the bottom of this post to see more.

(original post)

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.

(added by Saurav Kumar on 02/06/2018)

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

(added on 03/05/2023)

Let's say you just typed and executed a command in an incorrect way, and you want to change or delete the erroneous line in your history. Then you would probably go to that line and change it (like fixing a typo) so it contains the correct command, and then press and Ctrl+c, so the correct command is saved on that line. However, if you now go that line and execute it, the old line will reappear at its original position.

One workaround for this is the following: Go to the erroneous line and correct it, then hit Alt+#. This will append the correct command as a comment to the current history list. You can now delete the incorrect line the way described above. After that, you can execute the corrected command by pressing , Home, Del, (or Ctrl+p, Ctrl+a, Ctrl+d, ).

Caution: You have to avoid hitting or closing the terminal when you are on the line with the deleted command, or it will get saved to the history file anyway. One way to eliminate this risk is to use

 history -d -2

(Note the leading space at the start of the command) instead of deleting the incorrect line the way described in the original answer. The option -2 works if you performed the steps above immediately after executing the incorrect command. Otherwise, use a larger number.

  • 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 Feb 1, 2018 at 14: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.

  • 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.. :) Sep 12, 2013 at 15:15
  • @SauravKumar have edited the answer. Had missed the single and double quotes in aliasing command.. My bad.. :P
    – Stormvirux
    Sep 12, 2013 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.. :) Sep 12, 2013 at 16:48
  • If you don't mind, I would like to edit your answer so that it would be helpful to others.. Sep 12, 2013 at 16:51
  • Actually I edited it. You've to approve it.. Sep 12, 2013 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.

  • The note is very important. Nov 24, 2018 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".

  • 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! Jan 12, 2018 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.

  • 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, 2019 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. Jan 24, 2019 at 4:54

You must log in to answer this question.

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