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.
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
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
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
Alternatively, to get a preview of what the above file entails, we will execute the following command:
To search for a specific command pattern like update within the history file, we will borrow from the grep command:
$ history | grep 'update'
$ 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
Alternatively, we could create the history file backup via the history command:
$ history > my_backup2_history $ ls -l my_backup2_history
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
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
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 ]