The Linux shell (specifically, βBashβ) takes each executed command as an event and saves the command in the β.bash_history
β file located in the userβs home directory.
Now, there are two ways to view the history record of a userβs executed command: one using the history command, and the other by reading the β~/.bash_history
β file using the cat command.
In this article, you will learn how to view, backup, and restore the Linux command history on a Linux system, so stick with it till the end to learn everything.
Tutorial Details
Description | Backup and Restore Userβs Command History |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | history |
Internet Required | No |
How to View Linux Command History
All Linux distributions ship with an amazing tool named βhistory
β that gives you many features and functionality to interact with β~/.bash_history
β file, including reading the user command history.
$ history
Output:
Alternatively, you can read your β~/.bash_history
β file using the βcat
β command.
$ cat ~/.bash_history
#OR
$ cat $HOME/.bash_history
Output:
If you want to see another userβs command history, you need to give the location of their β.bash_history
β file.
$ cat /home/jake/.bash_history
The above command will output the user βjake
β command history.
Usually, when you execute the history command or view the user command history using the cat command, you will get a list of all user executed commands in the terminal.
Although you might not be interested in a complete list, you can use the grep command to output only a specific user executed command.
For example, the following two commands will only list the current userβs SSH-related executed commands from the history file:
$ history | grep ssh
#OR
$ cat ~/.bash_history | grep ssh
Output:
How to Backup Linux Command History
Now, as you understood, that β~/.bash_history
β file keeps the record of user executed commands in the Linux terminal.
So, you can backup this file for the current user or a different user, including all or a specific command.
Backup Current User Command History
To backup your existing command history record, you can use the history command or β~/.bash_history
β file to redirect their output to a different file using the β>
β or β>>
β redirection symbol.
$ history > command_history.bk
#OR
$ cat ~/.bash_history > command_history.bk
Output:
Backup Another Userβs Command History
Like the previous command, you can backup another userβs command history record; however, the history command will not work in this situation as it only reads the current userβs history file.
Still, you can backup another userβs command history by using the cat command with the β>
β or β>>
β redirection symbol, as shown.
$ sudo cat /home/jake/.bash_history > command_history.bk
Output:
Backup Specific Executed Command History
As we discussed earlier, you are not always going to require the complete list of the userβs command history, so you can pull the specific executed command from the history file for the user.
The following command will backup the current userβs SSH-related executed commands.
$ history | grep ssh > command_history.bk
#OR
$ cat ~/.bash_history | grep ssh > command_history.bk
Output:
Alternatively, use the following command to backup another user specific executed command.
$ sudo cat /home/jake/.bash_history | grep ssh > command_history.bk
Output:
How to Restore Linux Command History
To bring back the user command history, first make a copy of your existing history file using one of the methods above. Then, use the following command to delete your existing history file:
$ rm ~/.bash_history
#OR
$ rm $HOME/.bash_history
After that, restore the backed up user command history using the following command:
$ mv command_history.bk ~/.bash_history
#OR
$ mv command_history.bk $HOME/.bash_history
Last, reload the history file and run the following commands to make sure everything was done right:
$ history -r
$ history
Output:
Final Tip!
Thatβs all it takes to backup and restore the current or another userβs history record of all or specific commands.
If you want more articles related to this topic, then let us know in the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.