The Linux terminal has a “history” feature. With this feature, every command operation you enter will be backed up for later. Since all of your terminal commands are saved in “history,” it’s essential to keep a backup of it for safekeeping.
In this guide, we’ll show you how to back up Linux terminal history and how to restore backups as well. So, open up your favorite Linux terminal emulator and follow along!
Where is Linux terminal history stored?
The Linux terminal stores its history in a file. This file is named “.bash_history.” Anyone can edit it, and it is stored in the home directory. Since the terminal history file for Linux is stored in a user directory, every single user on the system has a file.
Special permissions do not protect these history files, and any user on the system can take a look at the history of another with a simple command. So, for example, if I want to take a look at the terminal/command-line history of username “user” on my Linux system, I’d do:
Users can also view the history of the current user they’re logged into in the Linux terminal shell, by simply executing the “history” command.
Best of all, since “history” is just a file, it can be searched like a regular text file using the grep function. So, for example, to find instances of “git clone” in username with the command below.
cat /home/user/.bash_history | grep 'git clone'
It also works as the current logged in user with the “history” command.
history | grep 'search term'
Save terminal history to a backup
In the previous section of this guide, I talked about how the “history” for the Linux terminal is just a neatly hidden text file that contains all user-entered commands. Well, since it’s just a file, that means it’s super easy to back up for safekeeping.
To create a backup, make use of the cat command. Why? With cat, you can view the entirety of a text file right in the terminal. We can use this command in combination with the “>” symbol to redirect the viewing output to a backup file.
So, for example, to backup your current history, run the cat command against “~/.bash_history” and save it to a file with the label of “history_backup.”
cat ~/.bash_history > history_backup
You can also run the history command in combination with “>” and save it that way.
history > history_backup
Lastly, it’s possible to back up the command-line/terminal history of another user not logged in by running the command below.
Note: be sure to change “username” to the user that you’d like to save history from.
cat /home/username/.bash_history > history_backup
Only backing up certain history items
You may only want to back up specific commands in your Linux terminal history. The way to do this is to view the history file and combine it with the grep command, which will filter specific keywords.
For example, to only backup commands in your Linux terminal history that contain the git clone or git commands, you can run the operation below.
Note: in these examples, we are using “>>” rather than “>.” The reason for “>>” is that it will not overwrite the contents of the history file backup, and can be re-run multiple times to add to the backup.
cat ~/.bash_history | grep 'git' >> history_backup
cat /home/username/.bash_history | grep 'git' >> history_backup
Filtering with grep can also be applied to the history command, like so.
history | grep 'git' >> history_backup
To back up certain keywords from the history file, replace “git” in the examples above with whatever commands you’d like to back up. Feel free to re-run this command as much as necessary.
How to restore the history backup
Restoring the history backup is as simple as deleting the original file and putting the backup in its place. To delete the original history file, use the rm command in a terminal window to delete “.bash_history.”
Once the original history file is deleted from the home folder of the user in which you want to restore history, use the mv command to rename “history_backup” to “.bash_history.”
mv history_backup ~/.bash_history
Now that the new history file is in place run the history -rw command to reload the terminal’s history function.
You’ll then be able to see your terminal history with:
Restore backups for other users
Need to restore history backups from other users on the system? To do this, start by logging into their user using the su command.
After logging in to the user, delete the current history file that resides in the user’s home directory (~).
From there, rename the history backup file as the new “.bash_history” file in the user’s directory.
mv /path/to/backup/file/history-backup ~/.bash_history
Write the changes with:
When done, run history to view the restored commands in the terminal window.