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.

Backup and restore terminal history

  1. Display history file content
  2. Backup history file
  3. Back up commands in specific history
  4. Restore history backup file for current user
  5. 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 

Picture 1 of How to back up and restore Linux Terminal historyPicture 1 of How to back up and restore Linux Terminal history

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

 history 

Picture 2 of How to back up and restore Linux Terminal historyPicture 2 of How to back up and restore Linux Terminal 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'.

Picture 3 of How to back up and restore Linux Terminal historyPicture 3 of How to back up and restore Linux Terminal history

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

 history | grep 'kill' 

Picture 4 of How to back up and restore Linux Terminal historyPicture 4 of How to back up and restore Linux Terminal history

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.

Picture 5 of How to back up and restore Linux Terminal historyPicture 5 of How to back up and restore Linux Terminal history

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

 history > history_backup2 

Picture 6 of How to back up and restore Linux Terminal historyPicture 6 of How to back up and restore Linux Terminal history

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 

Picture 7 of How to back up and restore Linux Terminal historyPicture 7 of How to back up and restore Linux Terminal history

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:

  1. > : Whenever this icon is used, it will overwrite the contents of the file after each.
  2. >> : However, this icon appends the output to the created file without overwriting the file.
 cat /home/hendadel/.bash_history | grep 'kill' >> history_backup 

Picture 8 of How to back up and restore Linux Terminal historyPicture 8 of How to back up and restore Linux Terminal history

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

 history | grep 'kill' >> history_backup2 

Picture 9 of How to back up and restore Linux Terminal historyPicture 9 of How to back up and restore Linux Terminal history

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 

Picture 10 of How to back up and restore Linux Terminal historyPicture 10 of How to back up and restore Linux Terminal 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 

Picture 11 of How to back up and restore Linux Terminal historyPicture 11 of How to back up and restore Linux Terminal history

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

 history -r 

Picture 12 of How to back up and restore Linux Terminal historyPicture 12 of How to back up and restore Linux Terminal history

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.

4.1 ★ | 16 Vote