How to Backup and Restore Linux Terminal History

Brief: This article walks us through an understanding of the Linux terminal history, a demonstration of its backup and restore procedures, and why it is important.

The Linux operating system is a feature-rich operating system environment. Its command line environment alone has the capability of transforming an ordinary user into a super user. In this case, we will look at the concept behind how to back up and restore Linux terminal history.

Linux History Command

The Linux operating system is equipped with a feature-rich command called the history command, which is used to keep track of user keystrokes (executed commands) associated with commands used on the terminal.

$ history
Linux History Command
Linux History Command

The Linux operating system has a hidden file called .bash_history, which saves all user commands used in the command line. Each Linux user has a unique copy of the history file which can be found in the user’s home directory.

$ ls -l $HOME/.bash_history
User Bash History File
User Bash History File

As per the execution of the above command, it is quite clear that no special system permissions protect your history file. Therefore, any user with access to an open Linux account is able to preview the content of .bash_history file.

Backup and Restore Linux Terminal History

This section of the article demonstrates how to display, backup, and restore specific or entire terminal history in Linux.

Viewing Linux History File

To display the history file for the currently logged-in user, implement the following cat command:

$ cat $HOME/.bash_history
View History of Linux User
View History of Linux User

Alternatively, to get a preview of what the above file entails, we will execute the following command:

$ history
View Last Executed User Commands
View Last Executed User Commands

To search for a specific command pattern like update within the history file, we will borrow from the grep command:

$ history | grep 'update' 
Search Particular History Command
Search Particular History Command

Alternatively, run:

$ cat $HOME/.bash_history | grep 'update'

Backup Linux Terminal History

Now that we can fully and partially display the history file’s content, it’s time to create its backup copy. Here, we can use the cat command and point it to the output file for the created backup.

$ cat $HOME/.bash_history > my_backup_history
$ ls -l my_backup_history
Backup Linux Terminal History
Backup Linux Terminal History

Alternatively, we could create the history file backup via the history command:

$ history > my_backup2_history
$ ls -l my_backup2_history 
Backup Linux History Commands
Backup Linux History Commands

If you are a system or network admin and need a copy of the history command belonging to a specific user, implement:

$ cat /home/targeted_system_username/.bash_history > my_backup_history

To backup specific history commands, we will filter through the history file for a specific command pattern via the grep command and backup all matching instances.

$ cat $HOME/.bash_history | grep 'update' >> my_backup3_history
$ cat my_backup3_history
Backup Specific History Commands
Backup Specific History Commands

Alternatively, we could achieve the same objective via history command:

$ history | grep 'update' >> my_backup4_history

To backup up specific commands for specific users:

$ cat /home/targeted_system_username/.bash_history | grep 'update' >> my_backup3_history           

Restoring Linux Terminal History

The first step is to delete the original history file and restore the created history file backup.

$ rm $HOME/.bash_history 
$ mv my_backup_history $HOME/.bash_history

Next, reload the history file and confirm that the history file has been restored:

$ history -r 
$ history
Restore Linux Terminal History
Restore Linux Terminal History

Understanding the process behind how to back up and restore Linux terminal history has essential benefits. It helps a Linux system admin audit system user’s activities on the network. Also, it makes it easier to reuse previously executed commands that might be too long to memorize.

[ You might also like: How to Remove a Command from History in Linux ]

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

Got Something to Say? Join the Discussion...

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.