How to clear history in Ubuntu
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.
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
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.
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.
You should read it
- What's New in Ubuntu 21.10?
- Why is Ubuntu LTS preferred over regular distros?
- What's new in Ubuntu 18.04 LTS just released?
- How to prolong the life of Ubuntu 18.04 installation with Ubuntu Pro
- What is the difference between Ubuntu Desktop and Ubuntu Server?
- Ubuntu 21.04 users need to update the system ASAP
- 5 things to do after upgrading to Ubuntu 22.04 LTS
- 5 Reasons Ubuntu Cinnamon Is Better Than Other Ubuntu Versions
- 7 reasons to upgrade to Ubuntu 23.04
- Ubuntu 19.10: Roadmap for release & planned features
- Tips and tricks after installing Ubuntu
- What is Ubuntu? Why should you use Ubuntu to replace Windows?