Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Back up and Restore Linux Terminal History

Learn how to back up and restore linux terminal history with clear steps, practical context, and useful troubleshooting guidance.

Table of Contents

This updated guide examines How to Back up and Restore Linux Terminal History and organizes the essential facts, background, and practical takeaways in clear American English.

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. Tipsmake.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

How to Back up and Restore Linux Terminal History — contextual image 1

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

history

How to Back up and Restore Linux Terminal History — contextual image 2

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'.

How to Back up and Restore Linux Terminal History — contextual image 3

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

history | grep 'kill'

How to Back up and Restore Linux Terminal History — contextual image 4

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.

How to Back up and Restore Linux Terminal History — contextual image 5

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

history > history_backup2

How to Back up and Restore Linux Terminal History — contextual image 6

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

How to Back up and Restore Linux Terminal History — contextual image 7

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

How to Back up and Restore Linux Terminal History — contextual image 8

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

history | grep 'kill' >> history_backup2

How to Back up and Restore Linux Terminal History — contextual image 9

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

How to Back up and Restore Linux Terminal History — contextual image 10

Step 2 : Now use the mv command to move the historical backup file to the Home folder.

mv history_backup /home/hendadel/.bash_history

How to Back up and Restore Linux Terminal History — contextual image 11

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

history -r

How to Back up and Restore Linux Terminal History — contextual image 12

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.

FAQ

What is How to Back up and Restore Linux Terminal History about?

It provides a structured overview of Linux Terminal, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.