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
$ historyOutput:

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.
Tutorial Details
| Description | Changing the Userβs History File Location |
| Difficulty Level | Low |
| Root or Sudo Privileges | No |
| OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
| Prerequisites | Vim, Nano |
| Internet Required | No |
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.
You can check these file permissions using the ls command, as shown:
$ ls -l ~/.bash_history
#OR
$ ls -l /home/$USER/.bash_historyOutput:

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_historyOutput:

As you can see, the permissions for the β~/.bash_historyβ and β~/command_historyβ files are identical.
So, now you need to modify the β~/.bashrcβ file using your choice of text editor, Vim or Nano.
$ vim ~/.bashrc
#OR
$ nano ~/.bashrcOutput:

And then add the following line at the end of the file, specifying the full path of your new history file.
export HISTFILE=/home/$USER/command_historyOutput:

Save and close the file, then reload the configuration changes using the source command.
$ source ~/.bashrcNow 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 -aThen you can verify the results by looking at the output of the β~/command_historyβ file and the βhistoryβ command.
$ cat ~/command_history
$ historyOutput:

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.





Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.