I'm working in Mac OSX, so I guess I'm using bash...?
Sometimes I enter something that I don't want to be remembered in the history. How do I remove it?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
If you want to run a command without saving it in history, prepend it with an extra space
prompt$ echo saved prompt$ echo not saved \ > # ^ extra space
For this to work you need either
HISTCONTROL. For example, run
To make this setting persistent, put it in your
If you've already run the command, and want to remove it from history, first use
to display the list of commands in your history. Find the number next to the one you want to delete (e.g. 1234) and run
history -d 1234
Additionally, if the line you want to delete has already been written to your $HISTFILE (which typically happens when you end a session by default), you will need to write back to $HISTFILE, or the line will reappear when you open a new session:
To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered the clear-text password as in the scenario above, find the line number in the history file and run this command.
$ history -d 2038
To delete or clear all the entries from bash history, use the history command below with the -c option.
$ history -c
Alternatively, you can use the command below to delete the history of all last executed commands permanently in the file.
$ cat /dev/null > ~/.bash_history
Also With Bash 5, you can delete a range aswell
history -d 511-520
If you are using Zsh,
history -d linenumber doesn't work to delete a specific line number in the command line history. However you can edit the history file. Close and reopen your terminal and edit the history like so:
See this for more details.
1- in bash terminal type
history # This will list all commands in history .bash_history file with line numbers
... 987 cd 988 ssh email@example.com 990 exit 991 cd
2- pick the CMD line number you want to delete
history -d 988
Note: if you want to delete for example last 3 CMDs, just pick the third line number from bottom ex: 988 and repeat the CMD
history -d 988 3 times in sequence.
historycommand in your terminal
In Ubuntu (but I'm pretty sure, it will be work for other Linux distributions and also MacOS the same way) the bash history file can be simply edited in an arbitrary text editor:
$ nano ~/.bash_history
If you don't know, where it's stored, you can find it as follows:
$ echo $HISTFILE
Or you can just do it a bit more generic way:
$ nano $HISTFILE
If you have
hstr (a way better
reverse-i-search that can be installed with
sudo apt install hstr), then it's really simple:
Ctrl+rto search your history.
deletekey, and press