This tutorial explains how to delete your bash history without leaving traces in Linux.

Your bash history can be seen using the history command. If you run it, all the user history will be displayed as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-01.png" data-lazy- height="424" src="data:image/svg xml,” width=”939″>

By default, the bash history is located in the file .bash_history file located in the user’s home directory.  In my case, the user is linuxhint, to see the history using cat I run:

cat /home/linuxhint/.bash_history

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-02.png" data-lazy- height="431" src="data:image/svg xml,” width=”939″>

Note: you can learn your history file location by running the command below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-03.png" data-lazy- height="343" src="data:image/svg xml,” width=”939″>

Deleting your history without a trace in Linux

 Deleting the command line activity history is simple, and there are few ways to achieve it. The first method uses the history command shown above, followed by the -c (clear) option, as shown in the screenshot below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-04.png" data-lazy- height="343" src="data:image/svg xml,” width=”939″>

As you can see, after running history -c, if we run the history command without options again, there will not be output except for the current command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-05.png" data-lazy- height="343" src="data:image/svg xml,” width=”939″>

Another way to remove your bash history is to remove the hidden .bash_history file located in the user home directory.

Since we want to remove the command line history without leaving a trace, let’s do it so that recovering the history file is impossible. To do it, we need to install the wipe tool.

To install wipe on Debian based Linux distributions, run:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-06.png" data-lazy- height="449" src="data:image/svg xml,” width=”939″>

Now wipe was installed, you can securely remove the .bash_history file without the chance it will be recovered.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-07.png" data-lazy- height="531" src="data:image/svg xml,” width=”939″>

Now your .bash_history file was fully wiped but will be automatically generated again to record future commands when you log out and log in back as the same user. At the end of this tutorial, you will find instructions to prevent the .bash_history file from recording commands.

Removing a specific line from the .bash_history file

 Let’s suppose, like most Linux users, you use the .bash_history when repeating commands, but you want to remove a specific mistake. In some cases, some users may type a password in their terminal; this is extremely insecure.

In the following screenshot, we can see 7 commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-08.png" data-lazy- height="410" src="data:image/svg xml,” width=”918″>

Let’s say you only want to remove the sixth command (wipe .bash_history). To do it, you can use the history -d command followed by the line you want to remove, as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-09.png" data-lazy- height="410" src="data:image/svg xml,” width=”918″>

As you can see, the command was removed, but you need to write the changes using the history command followed by the -w flag, as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-10.png" data-lazy- height="200" src="data:image/svg xml,” width=”793″>

Now the specific line you wanted to delete was removed.

You also can remove line ranges. The following example shows how to remove a specific number of lines starting from a specific line. The command below will remove 10 lines starting from line 40. If you want to remove a number other than 10 lines, replace the number 10 with the number of lines you want to get deleted. Replace the number 40 with the line you want the line range to start.

for i in {1..10}; do history -d 40; done

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-11.png" data-lazy- height="102" src="data:image/svg xml,” width=”939″>

Turning off the bash history

 This tutorial section explains different methods to disable the bash history.

If you want to disable the bash history for the current shell only, run the command below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-12.png" data-lazy- height="366" src="data:image/svg xml,” width=”918″>

As you can see, after running set o history, that command was the last recorded, which means no commands typed after that were recorded, including the history command used to check the result.

You can see if the history is enabled by running the command below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-13.png" data-lazy- height="275" src="data:image/svg xml,” width=”939″>

Note: You can enable the bash history back by running the command below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-14.png" data-lazy- height="166" src="data:image/svg xml,” width=”939″>

To disable the bash history permanently, run the command below to add the rule set o history to your .bashrc file. The .bashrc file stores the configuration for your terminal sessions, including the shell history, among other features.

echo ‘set o history’ >> ~/.bashrc

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-15.png" data-lazy- height="206" src="data:image/svg xml,” width=”939″>

As you can see, at the bottom of your .bashrc file, now you’ll see the set o history rule added.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-16.png" data-lazy- height="493" src="data:image/svg xml,” width=”939″>

Apply the changes by running the .bashrc file as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-17.png" data-lazy- height="235" src="data:image/svg xml,” width=”852″>

To disable the history for the whole system, run the command below with root privileges to unset the HISTFILE variable.

echo ‘unset HISTFILE’ >> /etc/profile.d/nohistory.sh

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-18.png" data-lazy- height="110" src="data:image/svg xml,” width=”939″>

Note: If you want to recover the shell activity history, you can remove the file you just created under /etc/profile.d.

You also can disable the bash history for new or future users, keeping it for existing users. To achieve it, run the command below.

echo ‘set o history’ >> /etc/profile

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-19.png" data-lazy- height="175" src="data:image/svg xml,” width=”939″>

To undo the previous command and restore the bash history, you need to remove the set o history from the /etc/profile file

Remove the highlighted line (the last one containing the set o history command).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-20.png" data-lazy- height="489" src="data:image/svg xml,” width=”939″>

 Change it to:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-21.png" data-lazy- height="489" src="data:image/svg xml,” width=”939″>

Limiting the bash history size

 There are 2 variables limiting the bash history size, the $HISTFILESIZE and $HISTSIZE variables. The difference between them is that $HISTFILESIZE defines the number of lines that will be saved to disk and remain after the session ends. The second variable, $HISTSIZE, defines the number of lines that will be saved in the history but won’t remain after you close the session.

To see the number of lines saved in your history, run the commands below even after you close the session.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-22.png" data-lazy- height="279" src="data:image/svg xml,” width=”895″>

As you can see, after closing and logging back, my history will keep my last 100 commands.

To change the number of lines saved to disk, run the command below.

Note: 50 is the number of lines you want to keep in your history even after the closed session. Replace it with the number of lines you want.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-24.png" data-lazy- height="279" src="data:image/svg xml,” width=”895″>

As you can see, now your history saves your last 50 commands to disk.

Run the command below to see the number of lines saved while in the same session until logging out.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-25.png" data-lazy- height="279" src="data:image/svg xml,” width=”895″>

As you can see, your session history records your last 500 commands.

To change the $HISTSIZE run:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/Delete-history-Linux-26.png" data-lazy- height="279" src="data:image/svg xml,” width=”895″>

Now your sessions will keep your last 50 commands.

Conclusion

As you can see, removing your bash history or specific content within it is pretty easy, including for new Linux users. Knowing how to remove your history is especially important when you work remotely and need to keep activity safe. In some cases, users may type their passwords by mistake on the terminal, leaving sensible information. In some cases, some administrators may opt for disabling this feature at all. Yet, the bash history is an excellent feature that helps us repeat commands very easily by pressing a key.

I hope this tutorial explaining how to delete the history in Linux without a trace was useful. Keep following Linux Hint for more Linux tips and tutorials.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/linuxinstitute_icono-150×150.png61213d3e26df9.jpg" height="112" src="data:image/svg xml,” width=”112″>

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.