Linux Terminal comes with a handy feature called “history.” Every command entered in the terminal gets saved in a file called “.bash_history.” A history file is created for each user and can be found in the user’s home directory, for example, “/home/username/.bash_history.”
Note that the history file “.bash_history” is not protected with special permissions. So any user that has an account on that Linux system can take a look at the history file of the other users.
Backup and Restore Terminal History
In the first section, we are going to cover how to back up the Linux terminal history file. After that, we shall show you how to restore those backups.
Display History File Content
Step 1. To list the history file content, you can use one of the following methods.
The first method is by using the “cat” command as following:
cat /home/hendadel/.bash_history
The second method, you can preview the content of the history file using the history command as following:
history
Step 2. You can search the history using the “grep” command using one of the following methods.
The first method we shall search for the command “kill” in the “.bash_history” file as following:
cat /home/hendadel/.bash_history | grep 'kill'
As you can see in the below screenshot, the command shall list all the commands that include the “kill” word.
The second method to search the history is by using the “grep” command with the “history” command as following:
history | grep 'kill'
Backup History File
Step 1. Now to create a backup from your Linux terminal history file you can use one of the following methods.
The first method is by using the “cat” command along with the “>” symbol. This should redirect the output to the backup file.
cat /home/hendadel/.bash_history > history_backup
As you can see in the next screenshot, a backup file should be created.
The second method is by using the “history” command along with the “>” symbol.
history > history_backup2
Step 3. To perform a backup to the history file that belongs to another user, you can use the following command.
cat /home/admin/.bash_history > admin_history_backup
Backup Specific History Commands
Step 1. To backup specific commands from the history file, you can use one of the following methods.
The first method is by using the “grep” command along with the “>” or the “>>” symbol. The difference between the previous symbols is that:
- >: Whenever this symbol is used, it overwrites the contents of the file every time.
- >>: However this symbol appends the output to the already created file, without the need to overwrite the file.
cat /home/hendadel/.bash_history | grep 'kill' >> history_backup
The second method is by using the “history” and “grep” commands along with the “>>” symbol as following:
history | grep 'kill' >> history_backup2
Step 2. In case you need to backup specific commands from another user history, you can use the following command.
cat /home/admin/.bash_history | grep 'sudo' >> admin_history_backup
Restore History Backup File For The Current User
Step 1. To restore the history backup file, all you need is just deleting the original history file “.bash_history” that is located in the home directory as follows:
rm /home/hendadel/.bash_history
Step 2. Now use the “mv” command to move the history backup file to the home directory.
mv history_backup /home/hendadel/.bash_history
Step 3. After moving the history backup file, you have to reload the history by using the following command.
history -r
Now you can preview your history using one of the commands listed previously.
Restore History Backup File For Other Users
Step 1. In case you need to restore the history backup file for another user, you have to switch the account to that user using the following command.
su admin
Step 2. After logging successfully, delete the current history file.
rm /home/admin/.bash_history
Step 3. Now let’s move the history backup file to its new location.
mv /home/hendadel/admin_history_backup /home/admin/.bash_history
Step 4. Reload the history using the following command.
history -r
That’s it for now.