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 in 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.

Delete recent file history in Nautilus
In addition to deleting the command history in the terminal, you can delete the recent history in the system's default file manager. To do this, open the file manager from the system's application launcher.
Click on category Recent on the left sidebar of the manager.

Press Ctrl + A to select all recently opened files in Nautilus.
Press Right Click, then select Remove from Recent to clear the file manager's current history.

Open the System Menu in the upper left corner of the screen, then click the gear icon on the pop-up window.

Select Privacy and Security under the left sidebar of the window.

This will display a new subcategory on the right panel of the window. Click on category File History & Trash.

Turn off conversion options File Historythen click the button Clear History.

Automatically delete file history in Nautilus
Another way to delete file history in Ubuntu is to delete files containing data for the “Recent” category using Bash.
Start by creating a local binary directory in your home directory:
mkdir -p ~/.local/bin/ && cd ~/.local/bin/
Create a new Bash script in the new directory using your favorite text editor:
nano ./user-clear-history.sh
Paste the following block of code into the 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 the script file, then run the following command to update the file's permission bits:
chmod +x ./user-clear-history.sh
Open the system's app launcher, then select Startup Applications.

Click the button Add in the upper left corner of the window.

Provide a name for the script you want to run. In this case, the example would be labeled: “User File History Autoclear”.
Click the button Browse… below the Name text box.

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

Select your shell script, then click Open to add a new startup entry.
Click Add to commit to your current user session.

Delete file history for all users
One of the disadvantages of deleting file history through the GUI is that it only deletes file history for the current user. This can be a problem if you are maintaining a machine that is shared between multiple people.
To fix this, open a new terminal session and then run the following command to switch to the root user:
sudo -s

Go to the root user's home directory, then create a local bin directory inside that directory:
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 into 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 the file's permission bits to execute:
chmod +x ./system-clear-history.sh
Create a new systemd service file in “/etc/systemd/system” for your new script:
sudo nano /etc/systemd/system/system-clear-history.service
Paste the following block of code into 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 into your systemd daemon:
sudo systemctl daemon-reload
sudo systemctl enable --now system-clear-history.service
Confirm that the service is running properly by viewing its current status:
systemctl status system-clear-history.service

Disable recent backups in Gedit
Gedit is the default simple text editor available on some recent versions of Ubuntu. In some cases, this editor creates backup copies of every file you have opened and saved in the system. This can be a problem if you want to save space on your machine.
To turn this feature off, click the menu Options in the upper right corner of the app, then select Preferences.

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

Close the Settings window, then reload Gedit to apply your new settings.
Open a new terminal session, then run the following command to delete any backup files that Gedit created in the home directory:
rm "$(find ~ -regex '.*~$')"
Finally, confirm that there are no Gedit backup files left in your home directory:
find ~ -regex '.*~$'
Deleting various historical records from the system is just one step towards maintaining and securing your Linux computer. Explore the wonderful world of system security by learning how to anonymize your Linux distribution with Whoami!