I know that Bash has both HISTSIZE
and HISTFILESIZE
variables that control how long history is saved for, and how much of it is saved. I would like to keep an archive of my history. However, if I set either of the two variables mentioned above to a very large number, it makes searching for old commands very difficult, and after enough time they might get deleted anyway.
How can I automatically archive my bash history files once they get to a certain size, and is this method applicable to other log files (such as /var/log/auth.log
)?
Taken from Never lost your bash history again on "https://lukas.zapletalovi.com".
You can use logrotate to backup your
~/.bash_history
file.Create a config file for logrotate in
/etc/logrotate.d/bash_history
.You can check if it works using this command:
To see the files:
I found it on this webpage https://kowalcj0.github.io/2019/05/13/logrotate-bash-history/
This solution saves with date-time of execution:
add this to your .bashrc or .bash_profile:
to search in history type:
taken from https://spin.atomicobject.com/2016/05/28/log-bash-history/
There is a bash script recommendation here
To answer your second question first:
The Ubuntu log files are already processed by
logrotate
to keep them managable and within size limits.You can possibly even "abuse" it for your history files, it is quite convenient.