How to Change History File Location in Linux (with an Example)

Linux TLDR
Last Updated:
Reading time: 2 minutes

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
$ history


Checking the history file

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

DescriptionChanging the User’s History File Location
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
PrerequisitesVim, Nano
Internet RequiredNo

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


$ ls -l /home/$USER/.bash_history


Checking the history file permission

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


Creating a new file to store the user’s command history

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


$ nano ~/.bashrc


Modifying the "~/.bashrc" file

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_history


Adding a new history file path in the "~/.bashrc" 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 β€œhistory” command.

$ cat ~/command_history
$ history


Verifying the output of the new history file and history command

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.