Where is the bash history for the root user saved?

Solution 1:

Well, when you're logged in as root, the bash history is saved in /root/.bash_history file, where /root is the default home directory for root user. To prove this, run the following command when you are logged as root:

echo $HISTFILE

The $HISTFILE variable points to the file that contains your history.

Source: $HISTFILE

Generally when you log into another user account, the bash history will be saved in a file called .bash_history located in the home directory of that user.

Furthermore, if you want to save the bash history for the root user in another file, let say in .bash_history file from my home directory, you have to edit /root/.bashrc file, and add a new line at the end of file with the following content:

HISTFILE="/home/username/.bash_history"       #change username with your user name

Solution 2:

I'm used to using the shortcut list via the up key on the keyboard which shows the last used command for that user. If you log in as root then you will be shown the history for the root user.

Test it like this, sudo -i fill in the password and then hit the up key on the keyboard. Now you will scroll through the last used commands in the order recent to oldest.

I know Radu Rădeanu answer is what you wanted. But this also solves the problem at hand. My solution is more of a handy way of remembering an old command which may save time and hustle.

Solution 3:

Typing history on the shell list the all the commands in the history. One can use the following command to save history to another file:

history > *textfile*