Thứ Ba, Tháng Hai 11, 2025
spot_img
HomeTechnologyHow to clear history in Ubuntu

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 in your current desktop session.

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

Terminal shows the deleted history of the current shell session.
Terminal shows the deleted history of the current shell session.

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.

Xem thêm  9 Best Local/Offline LLMs You Can Try Right Now

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
Terminal displays the Bash history file and cache as completely deleted.
Terminal displays the Bash history file and cache as completely deleted.

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.

Seventh history entry on the current shell session.
Seventh history entry on the current shell session.

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.

Commands surrounding the seventh history entry have been removed.
Commands surrounding the seventh history entry have been removed.

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.

Location of category "Recent" inside the Nautilus file manager.
Location of the “Recent” category within the Nautilus file 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.

Item "Remove from Recent" in the Nautilus file manager.
“Remove from Recent” section in Nautilus file manager.

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

The gear icon in the Ubuntu system tray.
The gear icon in the Ubuntu system tray.

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

Item "Privacy and Security" inside Ubuntu's system settings.
The “Privacy and Security” section is inside Ubuntu's system settings.

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

Xem thêm  Should I buy a Notebook, Ultrabook or Laptop?
Setting "File History & Trash" on the System Settings menu.
Set “File History & Trash” on the System Settings menu.

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

Switch button "File History" and button "Clear History".
“File History” toggle button and “Clear History” button.

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.

Programme "Startup Applications" inside the system's app launcher.
“Startup Applications” program inside the system's app launcher.

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

Add button on the Startup Applications window.
Add button on the Startup Applications 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.

Browse… button on the Startup Applications window
Browse… button on the Startup Applications window

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

Location of the user script within the file picker.
Location of the user script within the file picker.

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

Click Add to commit to your current user session.

Add button for new startup script.
Add button for new startup script.

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
Prompt to switch from standard user to root user
Prompt to switch from standard user to root user

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
Output of custom systemd service in systemctl.
Output of custom systemd service in systemctl.

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.

Xem thêm  How to adjust Windows microphone settings for clearer audio calls

To turn this feature off, click the menu Options in the upper right corner of the app, then select Preferences.

Menu Options on Gedit.
Menu Options on Gedit.

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

Backup checkbox in Gedit.
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 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!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments