How to backup and restore Linux Terminal history

Linux maintains a backup of the command-lines used in the Terminal. This tutorial shows you how to backup the Linux terminal history file. After that, we shall show you how to restore those backups.

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.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

24,536FansLike
172FollowersFollow

LATEST ARTICLES

How to create a bootable CentOS Live USB drive on Windows

create centOS Live USB drive
CentOS ISO downloads are available in two different variations - Minimal ISO and DVD ISO. So what are these? In this guide, you will know how to create a CentOS Live USB drive that can also be used to install CentOS.
best video players for linux
Most Linux-based systems come with a built-in video player, but let's be honest, it most definitely won't be able to satisfy all of your requirements, due to the fact that default software is mostly made for basic purposes. In this article, we will review the 5 Best Video Players for Linux and thoroughly discuss their features as well as their pros and cons.
best linux distros for laptops
Whether buying a Linux pre-installed laptop or selecting a Linux distro for your existing laptop, there are many things to consider. Let's take you through some of the best Linux distros that are optimized for Laptops in this 2019 edition of the article. Read on.
Free up hard disk space ubuntu
Hard disk space can easily get filled up with cached package files, old kernels, and other obsolete files that occupy unwanted hard disk space. Here are top five best and safer ways to clean and free up hard disk space in Ubuntu. We show you Terminal and GUI way of cleaning up system.
screen capturing apps for linux
Do you want to make a video tutorial for YouTube or show-off your epic victory royale to your friends? Look no further — as we have compiled the 10 Best Screen Capturing Software for Linux. Whether you need to record your desktop screen or take a simple screenshot, FOSSLinux has got your back.
Uninstall Programs in Ubuntu
Uninstallation of programs can be done by graphical way using the Ubuntu Software Center, and the Synaptic Package manager. Command-line way of doing it is also possible using apt-get and aptitude commands. We shall discuss each one of them in detail.