How to back up and restore Linux Terminal history
Linux maintains a backup of the command lines used in the Terminal. This guide shows you how to back up a Linux Terminal history file. Then show you how to restore those backups.
Linux maintains a backup of the command lines used in the Terminal. This guide shows you how to back up a Linux Terminal history file. Then show you how to restore those backups.
Linux Terminal comes with a handy feature called 'History'. For example, every command entered in the terminal will be 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 rights. Therefore, any user who has an account on the Linux system can view the history file of other users.
In the first part, I will show you how to back up a Linux Terminal history file.Quantrimang.com will then show you how to restore those backups.
Display history file content
Step 1 : To list the contents of the history file, you can use one of the following methods.
The first method is to use the cat command as follows:
cat /home/hendadel/.bash_history

The second method, you can preview the contents of the history file with the history command as follows:
history

Step 2: You can search the history by using the grep command with one of the following methods.
The first method is to use kill in the .bash_history file as follows:
cat /home/hendadel/.bash_history | grep 'kill'
As you can see in the screenshot below, the command will list all commands containing the keyword 'kill'.

The second method to search history is by using the grep command with the history command as follows:
history | grep 'kill'

Backup history file
Step 1 : Now to create a backup from the Linux Terminal history file, you can use one of the following methods.
The first method is to use the cat command with the '>' symbol . This step will 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 will be created.

The second method is to use the history command with the '>' symbol .
history > history_backup2

Step 2 : To make a backup of the history file that belongs to another user, you can use the following command.
cat /home/admin/.bash_history > admin_history_backup

Back up commands in specific history
Step 1 : To back up specific commands from the history file, you can use one of the following methods.
The first method is to use the grep command , along with the symbol '>' or '>>'. The differences between the previous icons are:
- > : Whenever this icon is used, it will overwrite the contents of the file after each.
- >> : However, this icon appends the output to the created file without overwriting the file.
cat /home/hendadel/.bash_history | grep 'kill' >> history_backup

The second method is to use the history and grep commands , along with the '>>' symbol as follows:
history | grep 'kill' >> history_backup2

Step 2 : In case you need to back up specific commands from other user history, you can use the following command.
cat /home/admin/.bash_history | grep 'sudo' >> admin_history_backup
Restore history backup file for current user
Step 1 : To restore the historical backup file, all you need to do is delete the original .bash_history history file , located in the Home folder as follows:
rm /home/hendadel/.bash_history

Step 2 : Now use the mv command to move the historical backup file to the Home folder.
mv history_backup /home/hendadel/.bash_history

Step 3 : After moving the historical backup file, you must reload the history using the following command.
history -r

You can now preview your history with one of the commands listed earlier.
Restore history backup file to other users
Step 1 : In case you need to restore a historical backup file to another user, you must transfer the account to that user with the following command.
su admin
Step 2 : After successful login, delete the current history file.
rm /home/admin/.bash_history
Step 3 : Now, move the historical backup file to the new location.
mv /home/hendadel/admin_history_backup /home/admin/.bash_history
Step 4 : Reload history with the following command.
history -r
All operations are completed.
Hope you are succesful.
You've just finished reading the article "How to back up and restore Linux Terminal history" edited by the TipsMake team. You can save how-to-back-up-and-restore-linux-terminal-history.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.