How to clear history in Ubuntu

Clearing the Linux system history log is important for maintaining a healthy computer. It allows to save precious disk space and protect users' digital activities from prying eyes.

 Today's article will show you how to delete historical data on the Bash shell, file manager and text editor on Ubuntu Linux.

Delete command history in Bash

Linux is a command-line based operating system. From file management to program configuration, almost every action in Linux is performed as a terminal command. Over time, these commands will pile up on the Bash system shell, which can take up a lot of disk space.

The easiest way to clear Bash command history in Ubuntu is to use the UNIX pipe to redirect an empty string to the '.bash_history' file:

echo "" > ~/.bash_history

That means this operation will not clear the history buffer for the current Bash session. To do that, run the following command:

history -cw

Note : You need to rerun this command for every active Bash shell on your current desktop session.

Confirm that your current Bash history is cleared by running the history command without any arguments.

How to clear history in Ubuntu Picture 1How to clear history in Ubuntu Picture 1

Disable history log in Bash

It is possible to force Bash not to save any command history from any future sessions. This can be useful if you are setting up a shared computer and don't want other users to know the commands you are running on your account.

Start by making sure that the current history buffer in Ubuntu is completely cleared:

history -cw

Open the .bashrc file with your favorite editor:

nano ~/.bashrc

Paste the following lines of code at the end of the .bashrc file:

export HISTSIZE=0 export HISTFILESIZE=0

Note : The HISTSIZE variable determines the number of lines that Bash will keep during a session. Meanwhile, HISTFILESIZE sets the number of lines that the history log file will maintain over time.

Save the .bashrc file, then run the following command to apply the new settings to the current shell session:

source ~/.bashrc

Test the new settings by running the ls command on the current directory, then running the history command again:

ls && history

How to clear history in Ubuntu Picture 2How to clear history in Ubuntu Picture 2

Delete specific Bash history entries

The history command can also delete specific entries from its cache and history log. This can be useful if you just want to skip some commands instead of deleting the shell's entire history.

Use the history command to find the index number of the command you want to delete. For example, if you want to delete the 7th command in your shell history.

How to clear history in Ubuntu Picture 3How to clear history in Ubuntu Picture 3

Note : You can search for the specific command you want to delete by passing the history command to grep:

history | grep my-history-command

Run the following command with the index number of the item you want to remove:

history -d 7

Confirm that the history entry is gone by rerunning the history command.

How to clear history in Ubuntu Picture 4How to clear history in Ubuntu Picture 4

4 ★ | 2 Vote