The Linux shell (specifically, “Bash“) records all the commands you run in your terminal and stores them in the “
.bash_history” file in your home directory.
You can use tools like the history command, which provides you many features and functionality to work with this file, including viewing your command’s history.
Of course, instead of using this, you can directly use the cat command to read the content of this file, as shown.
$ cat ~/.bash_history
As you can see, the user’s command history is stored in the “
~/.bash_history” file, and the “
history” command uses this file to display or perform other actions on the user’s command history.
Now, you can directly edit this file to remove the specific command record, but we recommend reading the recent article on how to run a command without saving it to history.
Also, we have written an article on how to display or filter history records based on date and time or how to backup and restore Linux command history.
However, today in this article you will learn how to use another file instead of the traditional “
~/.bash_history” file to store your command history.
|Changing the User’s History File Location
|Root or Sudo Privileges
|Ubuntu, Manjaro, Fedora, etc.
Changing the User’s History File Location in Linux
As we’ve already talked about, the user’s command history is kept in the “
.bash_history” file, which is in the user’s home directory.
$ ls -l ~/.bash_history
$ ls -l /home/$USER/.bash_history
So, let’s create a new file with the name “
command_history” that will be used to store the user’s command history.
$ touch command_history
$ ls -l command_history
As you can see, the permissions for the “
~/.bash_history” and “
~/command_history” files are identical.
$ vim ~/.bashrc
$ nano ~/.bashrc
And then add the following line at the end of the file, specifying the full path of your new history file.
Save and close the file, then reload the configuration changes using the source command.
$ source ~/.bashrc
Now you can just run your commands, and when you’re done, use the following command to append the current session user’s command to the new history file:
$ history -a
Then you can verify the results by looking at the output of the “
~/command_history” file and the “
$ cat ~/command_history
As you can see, your new command records are being saved in the new “
~/command_history” file; however, if you want to go back to your previous history file, then simply remove the added line shown in this article.
So, that’s all for now. If you have any questions or queries related to this topic, then feel free to ask them in the comment section.