One of the major annoyances when moving to a new system is to make everything as comfortable as on the old one. The major work horse for many Linux programmers and system administrators is of course the shell – in particular the bash. While being extremely useful, I always need quite some time until I have it working just right, so that it doesn’t stand in the way of doing productive work. No Linux system I have installed so far had (for me) satisfactory default values of the bash resource file .bashrc.
As I am relying heavily on the bash, in particular on the history of commands typed, this is usually the first thing to adjust. bash has quite some options how to fine-tune the history:
HISTCONTROL:
By specifying one or more parameters you can define how the commands will be entered into the history file.
- ignorespace: every command that begins with a space is not saved in the history.
- ignoredups: if the command is the same as the previous one then it is not saved in the history.
- ignoreboth: ignorespace and ignoredups together.
- erasedups: removes all previous lines in the history that match the current command.
HISTSIZE:
The default value of HISTSIZE is set to 500. This is ridiculously low. The whole point of the history is to keep track of what you have typed. If you are using the bash as your work horse then 500 commands are used up before lunch.
HISTFILESIZE:
Specifies the maximal number of lines contained in the history file. The file is truncated to fit the defined value.
HISTTIMEFORMAT:
Specifies the format shown when typing ‘history‘.
You can fine-tune how the commands are saved to the history by using the shopt built in bash command:
The default is that whenever you exit from a bash session, it will overwrite the previous bash history with the current one. This is extremely inconvenient, when working with several bash sessions, as I do all the time.
Thus, you can use
shopt -s histappend
which will append the history of the particular session to the history file.
There are some more options relating to the history in bash, check out the man-page.
As sensible setup, which has worked fine for me and will save your typed commands for months is the following one. Simply add these lines to your .bashrc:
export HISTCONTROL=ignoredups export HISTFILESIZE=1000000000 export HISTSIZE=1000000 export HISTTIMEFORMAT="%F %T " shopt -s histappend
After letting the bash history grow for a couple of weeks or months you can start doing some fancy statistics on your command usage:
cut -f1 -d" " .bash_history | sort | uniq -c | sort -nr | head -n 100
or if you are interested also in the arguments (full commands):
cat .bash_history | sort | uniq -c | sort -nr | head -n 100
This will print you a list of your commands, sorted by their frequency of use. For example with me:
2326 ll 1205 less 1016 cd 975 make 475 cp 413 cat 383 sudo 368 grep 355 exit 354 .. 328 rm 310 vim 301 hl 281 v 249 ./run.sh 231 okular 230 locate 201 gcc 188 mv 187 lh ...
In case you are wondering: ll is an alias for ‘ls -l’.
If you find longer commands that you use frequently, this would be a good time to think of introducing some shortcut aliases.
Pingback: Accessing the bash history with arrow keys | Heuristos
Pingback: States and complexity in programming – why functional programming helps to improve your code | Heuristos