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

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

May be interested

  • What do you know about Linux distros?What do you know about Linux distros?
    linux has existed for nearly 30 years, this is a historic journey. if you are interested in the history of some of the major linux distributions, read the following article.
  • 7 Ways to Restart Linux from Terminal7 Ways to Restart Linux from Terminal
    restarting linux from the terminal may seem difficult, but don't worry, it's quite simple once you know the right commands. restarting linux is necessary after updating, troubleshooting, or performing system maintenance.
  • 5 interesting entertainment games in Linux Terminal5 interesting entertainment games in Linux Terminal
    if you are looking to learn how to use the linux terminal, a great approach for beginners is to install and play some simple games.
  • How to run 2 or more Terminal commands at the same time on LinuxHow to run 2 or more Terminal commands at the same time on Linux
    if you use linux, you probably know how to use useful commands to work with files, install software and launch programs. however, one thing you don't know is that you can still run multiple commands at the same time.
  • 14 interesting Linux commands in Terminal14 interesting Linux commands in Terminal
    terminal is a very powerful tool, but it can become 'interesting' through a few bash commands that quantrimang will introduce to you later. let's follow up and find out because some commands are quite useful.
  • How to use Timeshift to backup and restore a Linux systemHow to use Timeshift to backup and restore a Linux system
    fortunately, there are system restore tools that take snapshots of your files and settings, which you can restore on your system to bring it back to a previous operating point.
  • 6 interesting terminal commands on Mac6 interesting terminal commands on Mac
    terminal is a handy utility, often found on unix-based computers, such as linux and macos. there are many interesting things to do in the terminal. regardless of your experience, you can use these great terminal commands on mac.
  • 18 Interesting Linux Commands in Terminal18 Interesting Linux Commands in Terminal
    terminal is a very powerful tool, but it can be made 'interesting' through a few bash commands that tipsmake.com will introduce to you below. let's follow and learn because some commands are quite useful.
  • How to Create and Edit Text File in Linux by Using TerminalHow to Create and Edit Text File in Linux by Using Terminal
    this wikihow teaches you how to use the terminal app in linux to create a text file. after doing so, you can use one of linux's built-in text editors to make changes to the file. open terminal. to do so, click menu, then find the terminal...
  • How to spell check in Linux TerminalHow to spell check in Linux Terminal
    linux also has a command line spell checker utility in terminal. the utility is called aspell and is installed by default on ubuntu. this is how you can use aspell to check spelling in a terminal.