Home Learn Linux How to backup and restore Linux Terminal history

How to backup and restore Linux Terminal history

by Hend Adel
terminal history backup

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
User Command-Line History

User Command-Line History

The second method, you can preview the content of the history file using the history command as following:

history
List history File Content

List history File Content

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.

Search For A Specific Command In The History File

Search For A Specific Command In The History File

The second method to search the history is by using the “grep” command with the “history” command as following:

history | grep 'kill'
Search For A Specific Command In The History File Second Method

Search For A Specific Command In The History File Second Method

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.

Backup The History File

Backup The History File

The second method is by using the “history” command along with the “>” symbol.

history > history_backup2
Backup The History File Second Method

Backup The History File Second Method

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 The Another User History File

Backup The Another User History File

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
Search The History and Backup To The Backup File

Search The History and Backup To The Backup File

The second method is by using the “history” and “grep” commands along with the “>>” symbol as following:

history | grep 'kill' >> history_backup2
Search The History and Backup To The Backup File Second Method

Search The History and Backup To The Backup File Second Method

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
Remove The Current History File

Remove The Current History File

Step 2. Now use the “mv” command to move the history backup file to the home directory.

mv history_backup /home/hendadel/.bash_history
Move The History Backup To The Current User Home

Move The History Backup To The Current User Home

Step 3. After moving the history backup file, you have to reload the history by using the following command.

history -r
Reload The History

Reload The History

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.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.