How can I remove a certain line from
Typing "history -d xxx" will delete a specified line. You then have to type "history -w" to make them permanent. It also removes them from the .bash_history file. The surest fire way is cYrus's answer.
To prevent a command from being added to the history in the first place, make sure that the environment variable
This will prevent any command with a leading space from being added to the history. You can then clear the history completely by running
First of all, if the command you're about to issue is sensitive, unsafe, or you just don't need it cluttering up your history, it is best/quickest to just prevent it from entering the history in the first place. Make sure that
Then proceed any command you don't want in your history with a space:
If you accidentally put an unwanted command into history, providing that your bash session is still open, the command hasn't yet touched the disk. To delete the previous command in history, issue:
Note the leading space; this command requires
If you want to delete the last few commands, find the last and first history number:
In this case 602 and 599. Then issue:
If you don't want any history from your current session to hit the disk, exit using:
The approach so far is to not even let sensitive history items be written to disk for extra security, because in theory data deleted from non-volatile media can still be recovered.
If, however, the command(s) you wish to remove are from a previous session, they will have already been appended to the
Of course for history items already on disk, the alternative to editing the history with
Prevent sensible information from being stored in history file
If you've hitted some password on command line, than realize that all commands are logged, you could either:
Delete sensible information from history file
If you realize that sensible informations is already stored, you want delete them, but not whole history.
but, as you write this, you create another history line with info you try to delete... So you could:
This will stay locked until you hit sensible info on terminal, than use our entry to delete from
Another way could be to drop only sensible infos, but keep commands anyway. For this, you could simply replace sensible infos by a pattern of your choice:
Delete sensible information from any file in a specific tree
Finally, to be sure that this won't stay in another forgotten file:
will list all concerned files.
will replace all occurence of sensible infos in all files in directory
If you need to remove several lines at the same time I normally use this:
If you need to remove the last command you can use:
59 is the line number. Cannot be anything sweeter than this :)
If you need to remove a range of lines from history, the following bash function could save you some time:
Function should be typically added to
(Note: This question is among the top search results if you search for deleting a range of commands from bash history. So, while the above is more than what the question asks, it could be useful for some readers.)
Just try these,
this will display the id of the history and the command, e.g.
Here 211 is the id of the history. Now check this using