Under Construction

Loading and Storing the History

By default, when starting an interactive bash, the history is initialized with entries from the ~/.bash_history file. When bash exits, the current history is written back to this file. But you can also read in history entries from files at any time later, or save the current history in an arbitrary file.

To show this, we have thought of a small task that should be done with the help of bash. It’s about manually rotating log files. For this purpose we have stored a few log files in the current directory:

[user01@aixe01 ~]$ ls -l log*
-rw-r--r--    1 user01 staff           279 Jun  1 18:23 log
-rw-r--r--    1 user01 staff           274 Jun  1 18:23 log1
-rw-r--r--    1 user01 staff           331 Jun  1 18:23 log2
-rw-r--r--    1 user01 staff            58 Jun  1 18:24 log3
[user01@aixe01 ~]$

The log file has already been rotated (at least) 3 times. The task is to rename (rotate) the files as follows:

log2 -> log3
log1 -> log2
log -> log1

The log file should then be created again. We imagine that this task should be carried out more frequently, possibly also on other systems. You could save the history with the necessary commands in a separate file and then access this saved history again if necessary.

Since we are only interested in the commands to carry out the task, we first delete the current history:

[user01@aixe01 ~]$ history -c
[user01@aixe01 ~]$

We then perform the log rotation using three mv commands:

[user01@aixe01 ~]$ mv log2 log3
[user01@aixe01 ~]$ mv log1 log2
[user01@aixe01 ~]$ mv log log1
[user01@aixe01 ~]$

and then finally create the log file again with the touch command:

[user01@aixe01 ~]$ touch log
[user01@aixe01 ~]$

All in all, the following commands are in the history:

[user01@aixe01 ~]$ history
    1  mv log2 log3
    2  mv log1 log2
    3  mv log log1
    4  touch log
[user01@aixe01 ~]$

Now the history only has to be saved in a separate file. The “-w” (write) option is available to do this. We save the history in the file logrotate_history:

[user01@aixe01 ~]$ history -w logrotate_history
[user01@aixe01 ~]$

Note: If the file logrotate_history already exists, its contents will be completely overwritten. If you use the “-a” (append) option instead of “-w” (write), the history is appended at the end of the file.

If the “-a” option is used, it should be noted that not the complete history is appended, only the history entries that have not yet been saved are written.

If you want to run the log rotation again, sometime later, e.g. a few days later, you could import the necessary commands from the logrotate_history file again. We start with the following history:

[user01@aixe01 ~]$ history
    1  pwd
    2  ls
    3  ls -l log*
[user01@aixe01 ~]$

The “-r” (read) option of the history command can be used to read the history from a file:

[user01@aixe01 ~]$ history -r logrotate_history
[user01@aixe01 ~]$ history
    1  pwd
    2  ls
    3  ls -l log*
    4  mv log2 log3
    5  mv log1 log2
    6  mv log log1
    7  touch log
[user01@aixe01 ~]$

The entries from the specified file are read and appended at the end of the current history. The commands are then available again via the history mechanism.

If you read the commands of the file (e.g. accidentally) a second time, the commands are then all duplicated in the current history:

[user01@aixe01 ~]$ history -r logrotate_history
[user01@aixe01 ~]$ history
    1  pwd
    2  ls
    3  ls -l log*
    4  mv log2 log3
    5  mv log1 log2
    6  mv log log1
    7  touch log
    8  mv log2 log3
    9  mv log1 log2
   10  mv log log1
   11  touch log
[user01@aixe01 ~]$

If you use the “-n” option instead of “-r” to read a file, only entries that have not yet been read are added to the current history. This means that the entries do not appear more than once.

Saving and loading the history is particularly useful when activities are repeated and exactly the same commands have to be executed over and over again. The command fc (fix command) is then very helpful, it allows selecting and executing several commands from the history in one operation.