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 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.
Backup and restore terminal history
- Display history file content
- Backup history file
- Back up commands in specific history
- Restore history backup file for current user
- Restore history backup file to 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 should read it
- Top 7 best Linux terminal emulators
- How to use Guake Terminal in Linux
- How to install dictionaries in Linux Terminal
- How to play Game Boy games on Linux Terminal
- How to use Linux Terminal as a computer
- How to fix the problem of being unable to enter Terminal in Linux
- 5 interesting entertainment games in Linux Terminal
- How to run 2 or more Terminal commands at the same time on Linux
- 14 interesting Linux commands in Terminal
- 6 interesting terminal commands on Mac
- How to Create and Edit Text File in Linux by Using Terminal
- How to spell check in Linux Terminal
Maybe you are interested
The Surprising History Behind Popular Halloween Costumes
How to Fix Clipboard History Error in Windows 11 Latest Update
8 Ways to Fix Clipboard History Stopped Working in Windows 11
The Little-Known Dark History of Time Capsules
How to Check FaceTime Call History on iPhone
10 strangest military vehicles in world history