How to Change History File Location in Linux

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

Output:

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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, 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

#OR

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

Output:

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

Output:

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

#OR

$ nano ~/.bashrc

Output:

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

Output:

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

Output:

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.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.