5

So I know ~/.bash_history should be the file storing all the history and that you can just type in history to see all the contents of this file.

Why would my .bash_history file have just 5 entries and history shows 135 results?

I'd like to remove one of the entries I see from history but if not in ~/.bash_history how can I remove it?

1
  • The .bash_history is where exactly? – George Udosen Apr 29 '18 at 20:18
8

There is a little misunderstanding here, history does not show the content of ~/.bash_history. Instead, it shows the current content of Bash's history list in memory for this session.

Assuming a default setup of Bash and Ubuntu, the history management works as follows:

  • Whenever you open a Bash shell, it will read in the content of your .bash_history file and append that to its session history list.

  • You type in commands, causing Bash to append them to its session history list.

  • When you close your shell again, Bash will save its history list to the disk by appending the contained entries to your .bash_history file.

Note: by default the history list gets truncated to at most 1000 entries, and the history file to 2000 entries.

You see, the history file will never get updated until the specific shell session ends, so everything you did in your current terminal window is not persisted to the disk yet. It resides only in your history list in memory.


To clear a specific entry from your history list, you can run history to display the full list and find out the index number of your offending entry. Then you can delete it using

history -d NUMBER

You can also simply wipe your complete session history list using history -c.

Note that deleting an entry from the history list does not automatically delete it from .bash_history if it got already written there, but neither does deleting an entry from the file remove it from your current session history list, if you have any Bash sessions open.

To delete an entry from the history file, open your ~/.bashrc in a text editor and delete the offending line there. You should either close all terminal before that and use a GUI editor, or make sure you remove the entry from both the file and the session lists.

For more information about the history command, type help history.
More information about Bash and its history management, read the relevant parts of man bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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