It's a good habit that experienced Linux system engineers like to set up Bash's histsize/histfilesize so that they can log more history commands for later review, but one small problem is that the large amount of information that history records is Load into the memory, and has been kept in memory, so waste a lot of memory, according to the statistics of 100,000 historical records probably occupy about 10MB of memory. You know, today's it operational systems have tended to cloud computing and virtualization, especially if you tailor your allocation to the needs of your hardware resources. Therefore, the memory occupied by history is completely unnecessary, 10MB of usable memory can do many things, such as enable a MySQL service, open a syslogd, etc.
So how do you keep as many historical records as possible without wasting memory? One way to do this is to periodically save the history to your hard disk, and bash's current history is kept in. bash_history, as long as you regularly clean up the records on this file:
#!/bin/bash
# Archive Linux Command history files
Umask 077
maxlines=2000
lines=$ (Wc-l < ~/.bash_history)
if (($lines > $maxlines)); Then
cut=$ (($lines-$maxlines))
head-$cut ~/.bash_history >> ~/.bash_history.sav
Sed-e "1,${cut}d" ~/.bash_history > ~/.bash_history.tmp
MV ~/.bash_history.tmp ~/.bash_history
Fi
What the above script does is simple, check the. bash_history file, and if the number of rows exceeds 2000 rows, trim 2000 rows of records (this value can be customized, modify Maxlines parameters) and add to the. Bash_history.sav this file, So we can save all the history and the current history is no more than 2000 lines, and it takes up only a small amount of resources.