How to Clear History in Ubuntu

Spread the love

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.

Content

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:

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 clear by running the history command without any arguments.

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:

history -cw

Open your .bashrc file using your favorite text editor:

nano ~/.bashrc

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:

source ~/.bashrc

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

ls && history

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.

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:

history -d 7

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

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.

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.

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

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

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

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

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.

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

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.

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

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.

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:

sudo -s

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

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.

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

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:

find ~ -regex '.*~$'

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

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Ramces Red
Staff Writer

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.

Comments (2)