Your bash history can be seen using the history command. If you run it, all the user history will be displayed as shown below.
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:
Note: you can learn your history file location by running the command below.
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.
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.
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:
Now wipe was installed, you can securely remove the .bash_history file without the chance it will be recovered.
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.
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.
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.
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.
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.
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.
Note: You can enable the bash history back by running the command below.
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.
As you can see, at the bottom of your .bashrc file, now you’ll see the set+o history rule added.
Apply the changes by running the .bashrc file as shown below.
To disable the history for the whole system, run the command below with root privileges to unset the HISTFILE variable.
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.
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).
Change it to:
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.
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.
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.
As you can see, your session history records your last 500 commands.
To change the $HISTSIZE run:
Now your sessions will keep your last 50 commands.
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.