History command, how to backup in Ubuntu

about history command

In the next article we are going to take a look at how can we backup the history of the terminal in Ubuntu and how to restore it. The Gnu / Linux terminal offers users a command called history. This function will make a backup copy of each operation with commands that we use, which allows users to use it at another time.

Since all that terminal commands that we execute the users are saved in the 'historial', in some cases it can be very useful to have a backup copy of it safely for later use. In the following lines we are going to see how to make a backup of the terminal history in Ubuntu and also how to restore it later.

History command

unusual terminal commands
Related article:
Some unusual but entertaining terminal commands

Where is the history of the terminal stored?

The Gnu / Linux terminal stores its history in a file. East is named '.bash_history'and is stored in the home directory, from where anyone can edit it. Since the terminal history file is stored in the user's directory, each will have a file.

Any user of the system can see the history of another with a simple command. So, for example, if we wanted take a look at the command line history, we would just have to write something like the following in the terminal (Ctrl + Alt + T):

cat bash history command

cat /home/usuario/.bash_history

Users will also be able to view the history of the current user with which we log in to the terminal by executing:

history

Since the history is just one file, we will be able to search inside as in a normal text file using the grep utility. For example, to find instances of 'touch'you should use something like the following:

cat grep touch

cat /home/user/.bash_history | grep 'touch'

We could also use the following command:

history grep curl command

history | grep 'termino-a-buscar'

For see possible uses of the history command, we can write:

history help command

history --help

Save terminal history to backup

As we have been saying, the 'history' for the terminal is just a hidden text file containing all the commands written by the user. Well, since it is just one file, that means that it is very easy to make a backup to save.

To do it we will use the cat command. With this command we will be able to see the entirety of a text file directly in the terminal. If we use this command in combination with the symbol '>'we can redirect the output of the visualization to a file, which we will use as a backup.

We will get our backup copy by typing the following in the terminal (Ctrl + Alt + T):

terminal history backup

cat ~/.bash_history > backup_historial

We can also run history command in combination with '>' to save the command output to a file:

history > backup_historial

Another possibility will be back up another user's history. It is important to make sure you change the 'username'as we are interested in:

cat /home/nombre_usuario/.bash_history > backup_historial

Make a backup of certain history items

If we only want to backup specific commands from history, we can do it by viewing the history file and combining it with the grep command, which will filter specific keywords.

In the following examples let's use '>>' instead of '>'. The reason for using '>>' is that it will not overwrite the contents of the log file backup and can be rerun multiple times to add to backup.

For example, if we wanted to make a backup of the commands in the history that contain the command gsettings, we can execute the following operation:

backup only some files history command

cat ~/.bash_history | grep 'gsettings' >> backup_historial

Or it would also be possible to use:

cat /home/nombre_usuario/.bash_history | grep 'gsettings' >> backup_historial

Filtering with grep can also be applied to the history command:

history | grep 'gsettings' >> backup_historial

To back up certain keywords from the history file, just replace 'gsettings'in the examples above. In addition, we can rerun this command as much as necessary.

Restore history backup

Restoring your history backup is as simple as delete the original file and put the backup copy in its place. To delete the original history file, we can use rm command to remove '.bash_history'.

Once the file is deleted from the user's home folder in which we want to restore the history, with the mv command we can change the name of 'backup_historial' to '.bash_history'.

mv backup_historial ~/.bash_history

Now that the new log file is in place, we must run the following command to reload the function.

history -rw

When we're done, we can run 'history'to see the restored commands in the terminal window.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.