Friday, April 19, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  23] [ 0]  / answers: 1 / hits: 90011  / 2 Years ago, thu, january 6, 2022, 12:24:11

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?


More From » bash

 Answers
2

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




(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!


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



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


[#29479] Saturday, January 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tialmes

Total Points: 14
Total Questions: 108
Total Answers: 102

Location: Oman
Member since Thu, Jun 16, 2022
2 Years ago
;