How can I remove a certain line from
You can achieve removal from the history file using the commandline in two steps:
The two steps together remove the line permanently from the in-memory history and from the .bash_history file as well.
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 sensitive information from being stored in the history file
If you've entered some password on a command line, then realize that all commands are logged, you could either:
Delete sensitive information from the history file
If you realize that sensitive information is already stored, and you want to delete it, but not your entire history:
but, as you type this, you create another history line containing the search pattern (sensitive info) you are trying to delete. So you could:
This will run
Another way could be to delete only the sensitive info, but keep the commands that contain the information. For this, you could simply replace sensitive info with substitute text of your choice:
Delete sensitive 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 occurrences of sensitive info in all files
in the directory tree rooted at
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
Locate the line you want to delete by pressing ↑ (up arrow) until it appears, then press Ctrl+U. That should remove the line.
If you use the
I know it's been a long time since this question has been asked and answered but I'll show you my method for cleaning my history file and logging out at the same time.
I run :
What it does is simple:
After that command, you'll be logged out with a clean history file. Log in to check your history ;)
I hope this will help someone.