I have already written a blog post about useful Bourne Again SHell key sequences, so today, I will touch on another important aspect of the daily routine – the history.
Where history is stored
The location of the history file is stored inside the
$HISTFILE variable. By default, it is
How to impose limits on the history file
The maximum number of lines stored in a history file is defined using
$HISTFILESIZE variable. By default, the history length is limited to
The number of commands stored in the history file [as it is updated on exit] is defined using
$HISTSIZE variable. By default, the history list is limited to
$HISTFILESIZEand assign a negative number to
How to ignore commands
$HISTIGNORE as a colon-separated list of patterns to be ignored.
How to add a timestamp to each history entry
Define wanted time format using
$HISTTIMEFORMAT variable to associate a timestamp with each history entry.
strftimemanual page to learn about the format specification.
How to control which commands are saved
$HISTCONTROL variable as a colon-separated list of additional options controlling which commands are saved.
do not store the command in history when the line begins with space
do not store consecutive duplicates
store only one command occurrence in the whole history file
~/.bashrc personal initialization file for an interactive shell.
# disable history limits export HISTSIZE=-1 unset HISTFILESIZE # define date format export HISTTIMEFORMAT="%F %T " # ignore the following commands export HISTIGNORE="history*:pwd:ls*" # space to ignore, do not store consecutive duplicates export HISTCONTROL="ignoreboth" # append to the history file instead of overwriting it shopt -s histappend
num of commands.
$ history num
Delete the whole history.
$ history -c
Delete history entry
$ history -d num
Append new entries to the history file.
$ history -a
Read the history file and update the history list.
$ history -n
Command-line history key sequences
History reverse search
Command-line history reference
Print six last commands.
$ history 6 354 2014-10-20 23:18:23 ps ax 355 2014-10-20 23:18:35 mc 356 2014-10-20 23:18:41 top 357 2014-10-20 23:18:48 cat /etc/passwd | wc -l 358 2014-10-20 23:19:08 date +"%T %F" 359 2014-10-20 23:20:38 dir /etc /bin /usr
Execute command number
$ !354 ps ax PID TTY STAT TIME COMMAND 1 ? Ss 0:01 /sbin/init 2 ? S 0:00 [kthreadd] 3 ? S 0:00 [ksoftirqd/0] [..]
$ !355:p mc
echo command using second and following parameters from command number
$ echo !359:2* echo /bin /usr /bin /usr
Perform simple substitution.
$ !357:s/passwd/group/ cat /etc/group | wc -l 68
$ !359:1-:gs/\//\/usr\/local\//:p /usr/local/etc /usr/local/bin
history manual pages.
Try BASH History Suggest Box utility that aims to make completion easier and more efficient.