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

23,420FansLike
377FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Getting Started with Linux Operating System

The Linux operating system brings forth a vibrant mix of features and security, making it the best alternative to macOS or Windows operating systems. In this post, we will give you a master guide on Getting started with Linux systems - taking you from a complete beginner to a level where you can begin testing the various Linux distributions available with much ease.

How to Create a Comprehensive Mail Server on Ubuntu

Postal is a free and open-source mail server used to send and receive emails. It comes loaded with tons of excellent features and functionalities, making it extremely popular among large organizations as well as in enterprise settings.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

How to Boot your Windows or Linux PC from a USB Drive

Sometime back, the process of installing an operating system required users to pop a bootable media disk into their DVD or CD drive and use it to boot the PC. But times have changed. Nowadays, the most common way of installing an OS is booting from a USB drive. The use of USB drives is further propelled by the current production of slim and lightweight laptops with no support for DVD/CD drives.

Python For Loop: Everything You Need to Know

Loops are one of the essential elements in any programming language, and Python is not an exception to it. Loops are used to repeat a statement or a block of statements multiple times. If there were no concept of loops in programming languages, we have to write each statement again and again for the number of times we want to execute it.

How to install LibreOffice on Fedora

If you are looking for a feature-rich and reliable Office Suite for your Fedora PC, then you have an excellent option at hand. LibreOffice, a free and opensource app, has stood the test of time and evolved into a beautiful alternative to Microsoft Office.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

Installing multiple Linux distros on a USB drive

A bootable Linux USB drive, which also referred to as a Live USB drive, is a drive containing files that enable a computer to boot into a specific Linux distro. It is most commonly used to test-drive various Linux distros before installing on the computer for use as a daily driver.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.

5 Best Music Players for Linux

For sure, there will be Linux users out there who cannot live without music. If you're one of those people, you would know how important it is to have a good music player installed on your computer system. In this article, we present to you the five Best Music Players for Linux, which will hopefully be able to satisfy each of our readers' needs.

3 Best Ways to Uninstall Software on 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.