Maciej A. Czyzewski Blog Talks About

History built-in, often forgotten command

Posted on

All modern shells have some mechanism of working with the history of commands.

It’s operates on history file and memory buffer with recent commands. History file is stored in a regular flat file. (generally .bash_history, .sh_history or .zsh_history)

To check which file are you using now, simply write in your terminal prompt:

$ echo $HISTFILE

To find all such files type the command below:

$ ls -f ~ | grep "_history"

Controls

Control of the behavior of the command history is provided jointly by bash and the readline library. In addition to classic variable $HISTFILE bash provides four other variables that control what information is written to history: $HISTTIMEFORMAT, $HISTSIZE, $HISTCONTROL and $HISTIGNORE.

Basics

To see which commands you type most often and convert some of them into aliases you can use:

$ cut -f2 -d";" $HISTFILE | sort | uniq -c | sort -nr | head -10

Yeah, for me the most popular are ls and cd!

Entering the history command without any switches displays the full history list with line numbers:

$ history

This list can be of course searched with grep:

$ history | grep "cd"

You can cut the search to the the last 100 commands entered:

$ history 100

That’s all you should know! Happy hacking…