Featured

TechBytes on Linux

This is a growing list of Linux commands which might come handy for the of Linux users. 1. Found out i had to set the date like this: ...

Friday, September 12, 2025

How to remove the bash history from a Linux Terminal



Removing bash history in the terminal can be done in several ways, depending on whether you want to clear the current session's history, remove specific entries, or clear the entire history file.


1. Clear the current session's history:

To clear the history of commands in the currently running shell session, use:

ಕೋಡ್

history -c

This command clears the in-memory history for the current session but does not affect the ~/.bash_history file until the session exits (and even then, it will only write the current, now empty, history).

2. Remove specific entries from history:

To remove a specific command from the history, first, identify its line number using the history command. Then, use:

ಕೋಡ್

history -d <line_number>

Replace <line_number> with the actual line number of the command you wish to remove. To make this change permanent in the ~/.bash_history file, you need to write the current history to the file afterwards:

ಕೋಡ್

history -w

3. Clear the entire bash history file:

To completely clear the ~/.bash_history file, which stores your command history across sessions, you can use:

ಕೋಡ್

cat /dev/null > ~/.bash_history

This command effectively truncates the ~/.bash_history file, making it empty. For the changes to take effect immediately in the current session and in future sessions, it is also recommended to clear the current session's history:

ಕೋಡ್

history -c && cat /dev/null > ~/.bash_history

4. Temporarily disable history saving for the current session:

If you want to prevent commands from being saved to history for the current session, you can unset the HISTFILE environment variable:

ಕೋಡ್

unset HISTFILE

Commands executed after this will not be recorded in the ~/.bash_history file until a new shell session is started or HISTFILE is set again.

No comments:

Post a Comment

Popular Posts