This post was originally published on go2linux.org on December 20, 2008. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

history is one of those commands I use every day. Once you learn a few tricks around it, you will wonder how you ever worked without them.

Basic usage

Just type history and you get a numbered list of your recent commands:

  501  docker compose up -d
  502  git status
  503  grep -r "foo" .
  504  history

The number on the left is the command's position in the history — you will need it later.

List your most used commands

This is a one-liner I have been using for years. It tells you exactly which commands you rely on the most:

history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

It passes the output of history to awk, which extracts only the command name, then counts and sorts by frequency. The output looks something like this:

    142 git
     98 docker
     67 ls
     45 cd
     32 grep

Search your history

If you remember running a command but not the exact syntax, pipe history through grep:

history | grep mount

This is useful for long commands you do not want to type again, like:

mount 192.168.1.1:/home/user /media/nfs

Re-run commands

Once you find the command you want in the history list, you do not need to copy and paste it.

Run the last command again:

!!

Run a command by its history number:

!503

Run the last command that starts with a string:

!grep

That last one runs the most recent command that started with grep. Useful, but be careful — it runs immediately without confirmation, and that can cause damage if you are in a different directory. For example, say you ran rm *.log in /tmp earlier, and now you are in /etc. So be very careful.

Reverse search with Ctrl+R

This is probably my favorite shell shortcut. Press Ctrl+R and start typing any part of a command:

(reverse-i-search)`mount': mount 192.168.1.1:/home/user /media/nfs

The shell searches backwards through your history as you type. Press Ctrl+R again to go to the previous match, and Enter to run it. Press Ctrl+C to cancel without running anything.

Control the size of your history

Two environment variables control how much history is kept. Add them to your ~/.bashrc:

HISTSIZE=10000        # commands kept in memory during the session
HISTFILESIZE=20000    # commands saved to ~/.bash_history on disk

The defaults are usually 500 and 1000, which fill up fast. I keep mine much larger.

Avoid duplicates and noisy commands

HISTCONTROL lets you clean up what gets saved:

HISTCONTROL=ignoredups        # do not save duplicate consecutive commands
HISTCONTROL=ignorespace       # do not save commands that start with a space
HISTCONTROL=ignoreboth        # both of the above
HISTCONTROL=erasedups         # remove all previous duplicates, not just consecutive

The ignorespace trick is useful when you need to run a command with a password or sensitive argument — just add a space before it and it will not be saved to history.

Add timestamps to your history

By default history shows no dates. You can fix that:

HISTTIMEFORMAT="%Y-%m-%d %H:%M  "

After setting this, history output looks like:

  501  2026-05-20 09:14  docker compose up -d
  502  2026-05-21 14:33  git status
  503  2026-05-27 10:02  grep -r "foo" .

Note: timestamps only apply to commands run after you set this variable. Old history entries will show the epoch date.

Clear your history

If you need to wipe your session history:

history -c

To also clear the history file on disk:

history -c && history -w

This is a command I use a lot. I have a very poor memory — if you are like me, you will love history.