How to Backup and Restore Linux Command History

Linux TLDR
Last Updated:
Reading time: 3 minutes

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

DescriptionBackup and Restore Userโ€™s Command History
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisiteshistory
Internet RequiredNo

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:

Viewing the user history record using the history command

Alternatively, you can read your โ€œ~/.bash_historyโ€ file using the โ€œcatโ€ command.

$ cat ~/.bash_history

#OR

$ cat $HOME/.bash_history

Output:

Viewing the user history record using the cat command

If you want to see another userโ€™s command history, you need to give the location of their โ€œ.bash_historyโ€ file.

๐Ÿ“
If you are not a root user, then you must be a sudo user in order to access another userโ€™s history file.
$ cat /home/jake/.bash_history

The above command will output the user โ€œjakeโ€ command history.

Reading another user history record

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:

Printing user executed commands related to ssh

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:

Taking the backup of the current user history record

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:

Taking the backup of another user history record

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:

Taking the backup of current user specific executed command

Alternatively, use the following command to backup another user specific executed command.

$ sudo cat /home/jake/.bash_history | grep ssh > command_history.bk

Output:

Taking the backup of another user specific executed command

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:

Restoring the Linux history

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.