I have already written a blog post about useful Bourne Again SHell key sequences, so today I will touch on another important aspect of daily routine - the history.
Where the history is stored
The location of the history file is stored inside the
$HISTFILE variable. By default it is
How to impose limits on 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 to be stored in history file [as it is updated on exit] is defined using
$HISTSIZE variable. By default the history list is limited to
$HISTFILESIZEand assign negative number to
How to ignore commands
$HISTIGNORE as a colon-separated list of patterns to be ignored.
How to add timestamp to each history entry
Define desired time format using
$HISTTIMEFORMAT variable to associate 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 command in history when line begins with space
do not store consecutive duplicates
store only one command occurrence in the whole history file
~/.bashrc personal initialization file for 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 overwritte it shopt -s histappend
num of commands.
$ history num
Delete whole history.
$ history -c
Delete history entry
$ history -d num
Append new entries to the history file.
$ history -a
Read history file and update history list.
$ history -n
Command-line history key sequences
|CTRL-p||get previous command from history|
|CTRL-n||get next command from history|
|ALT-<||get first command from history|
|ALT->||get currently entered command|
History reverse search
|CTRL-r||start search as you type|
|CTRL-o||execute current command|
|CTRL-x CTRL-e||edit and execute current command|
Command-line history reference
|!n||refer to the n command line|
|refer to the previous command|
|!str||refer to the most recent command in the history starting with str|
|!?str?||refer to the most recent command in the history that contains with str|
|^str1^str2^||repeat last command, replacing str1 with str2|
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.