The Linux terminal has a “history” characteristic. With this characteristic, each command operation you enter will likely be backed up for later. Since all your terminal instructions are saved in “history,” it’s important to hold a backup of it for safekeeping.

On this information, we’ll present you the way to back up Linux terminal history and the way to restore backups as properly. So, open up your favourite Linux terminal emulator and comply with alongside!

The place is Linux terminal history saved?

The Linux terminal shops its history in a file. This file is called “.bash_history.” Anybody can edit it, and it’s saved within the house listing. Because the terminal history file for Linux is saved in a consumer listing, each single consumer on the system has a file.

Particular permissions don’t shield these history information, and any consumer on the system can check out the history of one other with a easy command. So, for instance, if I would like to check out the terminal/command-line history of username “user” on my Linux system, I’d do:

9k=

cat /house/consumer/.bash_history

Customers may also view the history of the present consumer they’re logged into within the Linux terminal shell, by merely executing the “history” command.

history

Better of all, since “history” is only a file, it may be searched like an everyday textual content file utilizing the grep perform. So, for instance, to discover situations of “git clone” in username with the command under.

cat /house/consumer/.bash_history | grep 'git clone'

It additionally works as the present logged in consumer with the “history” command.

history | grep 'search time period'

Save terminal history to a backup

Within the earlier part of this information, I talked about how the “history” for the Linux terminal is only a neatly hidden textual content file that incorporates all user-entered instructions. Properly, because it’s only a file, which means it’s tremendous straightforward to back up for safekeeping.

To create a backup, make use of the cat command. Why? With cat, you possibly can view the whole lot of a textual content file proper within the terminal. We will use this command together with the “>” image to redirect the viewing output to a backup file.

So, for instance, to backup your present history, run the cat command towards “~/.bash_history” and put it aside to a file with the label of “history_backup.”

cat ~/.bash_history > history_backup

You can too run the history command together with “>” and put it aside that approach.

history > history_backup

Lastly, it’s potential to back up the command-line/terminal history of one other consumer not logged in by operating the command under.

Word: ensure to change “username” to the consumer that you just’d like to save history from.

cat /house/username/.bash_history > history_backup

Solely backing up sure history objects

Chances are you’ll solely need to back up particular instructions in your Linux terminal history. The best way to do that is to view the history file and mix it with the grep command, which is able to filter particular key phrases.

For instance, to solely backup instructions in your Linux terminal history that include the git clone or git instructions, you possibly can run the operation under.

Word: in these examples, we’re utilizing “>>” relatively than “>.” The rationale for “>>” is that it’ll not overwrite the contents of the history file backup, and may be re-run a number of occasions to add to the backup.

cat ~/.bash_history | grep 'git' >> history_backup

Or

cat /house/username/.bash_history | grep 'git' >> history_backup

Filtering with grep may also be utilized to the history command, like so.

history | grep 'git' >> history_backup

To back up sure key phrases from the history file, exchange “git” within the examples above with no matter instructions you’d like to back up. Be at liberty to re-run this command as a lot as essential.

How to restore the history backup

Restoring the history backup is so simple as deleting the unique file and placing the backup as a substitute. To delete the unique history file, use the rm command in a terminal window to delete “.bash_history.”

rm ~/.bash_history

As soon as the unique history file is deleted from the house folder of the consumer wherein you need to restore history, use the mv command to rename “history_backup” to “.bash_history.”

mv history_backup ~/.bash_history

Now that the brand new history file is in place run the history -rw command to reload the terminal’s history perform.

history -rw

You’ll then have the ability to see your terminal history with:

history

Restore backups for different customers

Want to restore history backups from different customers on the system? To do that, begin by logging into their consumer utilizing the su command.

su username

After logging in to the consumer, delete the present history file that resides within the consumer’s house listing (~).

rm ~/.bash_history

From there, rename the history backup file as the brand new “.bash_history” file within the consumer’s listing.

mv /path/to/backup/file/history-backup ~/.bash_history

Write the modifications with:

history -rw

When performed, run history to view the restored instructions within the terminal window.



Source link

Share.
Leave A Reply

Exit mobile version