How do I change the .bash_history file location?
18,356
You need to set HISTFILE for your users to the location you need, set the following in .bash_profile for the user, and for new users set it in the user skeleton directory, most likely /etc/skel/.bash_profile
export HISTFILE=/home/$USER/.bash_history
Related videos on Youtube
![Nahydrin](https://i.stack.imgur.com/1Y4rB.jpg?s=256&g=1)
Author by
Nahydrin
Updated on September 18, 2022Comments
-
Nahydrin almost 2 years
I'm running CentOS 6.x and want to move the .bash_history to a different location.
The home directories of my users are (because I run a VPS) in
/var/www/vhost/<domain>.<tld>
which is FTP accessible (and it should be).Because of this, I have changed the
AuthorizedKeysFile
for SSH connections out of the normal~/.ssh/authorized_keys
since FTP connections would easily be able to locate them.At the same time I want to move the
.bash_history
file to/home/%u/.bash_history
where%u
is the current user. -
Nahydrin about 10 yearsThanks for how to set it up for new users automatically as well.
-
Daniël W. Crompton about 10 yearsAs you are running a VPS I assumed it might come in handy. :)
-
Rich Homolka about 10 yearsone note... this needs to be pretty early in the RC file parsing. As soon as anything gets written to history, any changes to this VAR are ignored.
-
Daniël W. Crompton about 10 years@RichHomolka according to the man file changes to HISTFILE aren't ignored, notably:
The name of the file in which command history is saved (see HISTORY below). The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits.
-
Rich Homolka about 10 years@DaniëlW.Crompton I agree :) I use this to have separate history files based on the
tty
command. My point was WHEN you set it. At some point the location is fixed and further changes are ignored. -
Daniël W. Crompton about 10 years@RichHomolka that must depend on your distribution. I just tried it on bash under Ubuntu, Debian and OSX, all let me change the HISTFILE mid stride.
-
mc0e about 3 yearschanging the HISTFILE variable will make the current shell save to this location when it exits but it won't load the file content into the current shell so it is usable with the
history
command. That's useful if you only want to avoid having your history written into the default history file, but not if you want to make use of the history from previous sessions that have used your alternative history file. You might as well just unset HISTFILE. -
Daniël W. Crompton about 3 years@mc0e I don't understand, is this a comment to my reply in 2014 or to the answer? The answer is about editing the
.bash_history
file.bash_profile
globally for all new users. -
mc0e about 3 years@DaniëlW.Crompton I was referring to the comment. I don't think the OP was asking about changes mid-stride, but that is what I'm interested in. Changing HISTFILE early is just fine in the answer. If you change HISTFILE mid-stride, then you have a write-only file situation, unless you do some other stuff as well. I think you need to (1) write out any history in the current shell to the old history file, (2) change the HISTFILE variable, then (3) load from the new history file into the history of the current shell. It'd be nice to have a tested recipe though.