June 16, 2024

How to Clear History in Ubuntu

Clearing your Linux system’s history logs is vital to maintaining a healthy machine. It allows you to save precious disk space and protect your digital activities from prying eyes. Here, we show you how to clear the history data on your Bash shell, file manager, and text editor on Ubuntu Linux.

Clearing the Command History in Bash

Linux is a command line-driven operating system. From file management to configuring programs, almost every action inside Linux takes some form of a terminal command. Over time, these commands pile up on your Bash system shell, which can take up a lot of disk space.

The easiest way to clear your Bash command history in Ubuntu is to use a UNIX pipe to redirect an empty string to your “.bash_history” file:

echo "" > ~/.bash_history

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

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

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

A terminal showing a cleared history for the current shell session.

Disabling History Logs in Bash

You can force Bash to not save any command history from any of your future sessions. This can be useful if you’re setting up a shared computer and you don’t want the other users to know the commands that you’re running on your account.

Start by ensuring that your current history buffer in Ubuntu is completely clear:

Open your .bashrc file using your favorite text editor:

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

export HISTSIZE=0
export HISTFILESIZE=0

Note: the HISTSIZE variable determines the amount of lines that Bash will hold during an active session. Meanwhile, HISTFILESIZE sets the amount of lines that the history log file will maintain over time.

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

Test your new settings by running an ls command on the current directory, then re-running the history command:

A terminal showing a completely clear Bash history file and cache.

Good to know: learn more about shell scripting by reading up on the most important Bash special characters.

Removing Specific Bash History Entries

The history command can also remove specific entries from its cache and history log. This can be useful if you only want to omit a handful of commands instead of clearing the entire history of your shell.

Use the history command to find the index number of the command that you want to clear. In my case, I want to remove the 7th command in my shell history.

A terminal highlighting the seventh history entry on the current shell session.

Note: you can search for the specific command that you want to remove by piping the history command to grep: history | grep my-history-command.

Run the following command along with the index number of the entry that you want to remove:

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

A terminal highlighting the surrounding commands around the deleted seventh history entry.

Clearing the Recent File History in Nautilus

Aside from clearing the history of commands on your terminal session, you can delete the recent history of your system’s default file manager. To do this, open your file manager from your system’s application launcher.

Click the Recent category on the manager’s left sidebar.

A screenshot highlighting the location of the

Press Ctrl + A to select all the recently opened files in Nautilus.

Press Right Click, then select Remove from Recent to delete the file manager’s current history.

A screenshot highlighting the

Open the System Menu on the desktop’s upper left corner, then click the Gear icon on the popup panel.

A screenshot highlighting the Gear icon on Ubuntu's System Tray.

Select Privacy and Security under the window’s left sidebar.

A screenshot highlighting the "Privacy and Security" category inside Ubuntu's system settings.

This will bring up a new subcategory on the window’s right panel. Click the File History & Trash category.

A screenshot highlighting the

Disable the File History switch, then click the Clear History button.

A screenshot highlighting the

Tip: learn how you can make Nautilus even more useful with plugins and extensions.

Automatically Remove the File History in Nautilus

Another way to clear file history in Ubuntu is to delete the files that hold the data for the “Recent” category with Bash.

Start by creating a local binaries folder in your home directory:

mkdir -p ~/.local/bin/ && cd ~/.local/bin/

Create a new Bash script under your new folder using your favorite text editor:

nano ./user-clear-history.sh

Paste the following block of code inside your script file:

#!/bin/bash
rm -f ~/.recently-used.xbel
rm -f ~/.recently-used.xbel.*
rm -f ~/.local/share/recently-used.xbel
rm -f ~/.local/share/recently-used.xbel.*

Save your script file, then run the following command to update its permission bits:

chmod +x ./user-clear-history.sh

Open your system’s application launcher, then select Startup Applications.

A screenshot highlighting the "Startup Applications" program inside the system's app launcher.

Click the Add button on the window’s upper left corner.

A screenshot highlighting the

Provide a name to the script that you want to run. In my case, I will label it as: “User File History Autoclear.”

Click the Browse… button under the Name textbox.

A screenshot highlighting the

Press Ctrl + H on the file picker menu, then navigate to your “~/.local/bin/” directory.

A screenshot showing the location of the user script inside the file picker.

Select your shell script, then click Open to add it to your new startup entry.

Click Add to commit it to your current user session.

A screenshot highlighting the

On a side note: learn how you can launch your script from the GUI using .desktop files in Linux.

Clearing the File History for All Users

One of the downsides of removing the file history through the GUI is that it only clears it for the current user. This can be a problem if you’re maintaining a machine that’s shared between multiple people.

To fix this, open a new terminal session then run the following command to switch to the root user:

A terminal showing the switch from user to root prompt.

Go to your root user’s home directory, then create a local bin folder inside it:

cd ~ && mkdir -p ~/.local/bin/ && cd ~/.local/bin/

Create a new script file using your favorite text editor:

nano ./system-clear-history.sh

Paste the following block of code inside your script file:

#!/bin/bash
rm -f /home/*/.recently-used.xbel
rm -f /home/*/.recently-used.xbel.*
rm -f /home/*/.local/share/recently-used.xbel
rm -f /home/*/.local/share/recently-used.xbel.*

Save your new script file, then set its permission bits to execute:

chmod +x ./system-clear-history.sh

Create a new systemd service file under “/etc/systemd/system” for your new script:

sudo nano /etc/systemd/system/system-clear-history.service

Paste the following block of code inside your new service file:

[Unit]
Description=Clear File History Before Shutdown
DefaultDependencies=no
Before=shutdown.target
 
[Service]
Type=oneshot
ExecStart=/root/.local/bin/system-clear-history.sh
 
[Install]
WantedBy=halt.target reboot.target shutdown.target

Save your new service file, then run the following commands to load it to your systemd daemon:

sudo systemctl daemon-reload
sudo systemctl enable --now system-clear-history.service

Confirm that the service is running properly by looking at its current status:

systemctl status system-clear-history.service
A terminal showing the output of the custom systemd service in systemctl.

Disabling the Recent Backups in Gedit

Gedit is a simple text editor that comes by default on some of the recent versions of Ubuntu. In some cases, this editor creates a backup of every file that you’ve opened and saved in the system. This can be an issue if you’re looking to save space on your machine.

To disable this, click the Option menu on the app’s upper right corner, then select Preferences.

A screenshot highlighting the location of the

Click the Editor tab on Gedit’s Settings window, then untick the Create a backup copy of files before saving checkbox.

A screenshot highlighting the location of the backup checkbox in Gedit.

Close the Settings window, then reload Gedit to apply your new settings.

Open a new terminal session, then run the following command to remove any backup file that Gedit made on your home directory:

rm "$(find ~ -regex '.*~$')"

Lastly, confirm that there’s no Gedit backup files left on your home directory:

Deleting the various history logs from your system is just one step on maintaining and securing your Linux machine. Explore the wonderful world of system security by learning how to anonymize your Linux distro with Whoami.

Image credit: Samu Lopez via Unsplash. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.

Leave a Reply

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