Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 11 down vote accepted

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.

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


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.

share|improve this answer
not bad :) Good to get a new way.. – Saurav Kumar Feb 27 '14 at 11:34
@SauravKumar Not bad?!? Probably is the best way... – Radu Rădeanu Mar 27 '14 at 17:04
@Radu Rădeanu It's a deceive way. When I close terminal and open it again histories are not deleted. -1 to this answer. – Afshin Hamedi Jun 14 '14 at 20:44
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
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

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.


share|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
@bladed19899 history -c doesn't work When I close terminal and open it again histories are not deleted. -1 your answer – Afshin Hamedi Jun 14 '14 at 20:56
@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
@KasiyA Use history -w after history -c to write the changes. – Cammy_the_block Aug 2 '14 at 16:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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