The bash history is a log file that contains all commands that the user executed on the Linux shell. When you use the "arrow up" key on our keyboard, then Bash will look up the previous command from that file and display it on the screen, ready to be executed again.

Where is this history file?

The .bash_history file is in the home directory of the user. When your username is "tom", then the .bash_history file is in the path:

/home/tom/.bash_history

Or a more general path is:

~/.bash_history

as ~ points always to the home directory of the currently logged in user.

Why clean up the history file?

Sometimes you might want to clean the file to prevent that another user that uses the same account (or the admin user of the server) can look up the commands that were executed. E.g. you used a shell command that contained a password in cleartext and you don't want that someone is able to look this password up. Another scenario is that you are building a clean virtual machine image to share it with others and you don't want that everyone can see all commands that you executed to build that VM image.

How do I clear Bash history?

Run the commands:

history -w
history -c

To clear the history file.

Finally you should check if history clearing has worked, press the "arrow up" key on your keyboard, if it does not show the past commands (except of the history cleaning command) then the procedure was successful.

Another way to clean the history file is this command sequence:

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

It will clear the history and end the current user session. Be aware that this command will kick you off the shell.

How to prevent that the Bash history is written?

If you know upfront that you don't want to save the command history to the history file, then you can prevent that the .bash_history gets saved with this command:

unset HISTFILE

This command removes the HISTFILE Shell variable which contains the number of records that shall be saved in the history.

If you like to get this behaviour on each login, then add "unset HISTFILE" in the .bashrc file of your user:

echo "unset HISTFILE" >> ~/.bashrc
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *