The Power Of History (Linux)

history man page

When using the terminal or command line in a Linux operating system you can recall your previous commands using the up arrow but did you know that you can also potentially recall commands from a lot further back? This functionality is provided via a command called history.

“I go into my library and all history unrolls before me.” 

Alexander Smith

The Many Ways To View History

Rather than pressing up to cycle through the historical list of commands, you can also type:

The history command

Or if you want to view the command history page by page, use the less command in combination with history:

history | less
The history command piped via the less command

If you want to search through history for a certain string or command that has been entered then grep can help:

history | grep "string to search for"

for example, if I wanted to see which commands I had used to see when I had used sudo with a command I would enter:

history | grep "sudo"
history | grep "sudo"
history | grep “sudo”

If you want to save an output of History to another file you could just ask the command line to output to a file:

history > file_name


history > history.txt
history > history.txt
history > history.txt

Recalling History

Viewing History is great, but what about recalling it?

If you have entered a lengthy command and after hitting enter realise that it needed elevation (e.g. sudo) it would be a pain to have to retype the whole command. Instead use:

sudo !!

This one line recalls the previous command and runs that command with sudo elevation.

sudo !! to use the previous command with sudo elevation

If you want to enter the previous command again without sudo, then enter:


If the command was entered a while back then there are a few options. The first is searching for the history number of the command (i.e. using the history | grep solution above) and then entering:



If I wanted to re-run sudo apt-get update which I shows as line 24 in my History I would enter:

Using !LINENUMBER to recall historical commands

Another option is the the reverse search.

At the command line / terminal press CTRL R the command line will change into a reverse-search prompt.

reverse search prompt

Enter part of the command you are looking for (e.g. sudo) and the search will return the most recent command that used that text. Press CTRL R to cycle through results (most recent cycling through to oldest).

reverse search prompt

Once you have the command press enter to run it or CTRL J to copy it to the command line if you want to edit it (e.g. if there was a typo).

The File Where History Is Stored

The history file is stored under the users home directory in a file called .bash_history. Note: That is . (full stop, or a dot) in front of bash_history.


If .bash_history is not under ~/ or you want to check where the history is writing to, enter the command:


echo $HISTFILE will ask the terminal / command line to echo (in this case output to screen) the file path for the environmental variable $HISTFILE.

Typically this file writes when a users session is logged out, so if you have multiple terminal windows open as the same user then the history may not follow from one live terminal session to the next live terminal session until after logging out. Whilst the sessions are open their history is being stored in a temporary history buffer.

How Much History?

The command:


Will show you how many lines of commands that History is storing.


Controlling History

“History is Written by Victors.”

Viewing your command line history is great when you want to remember commands you once ran, but what happens if someone else gets access to your system?

There are a few changes that can be made to stop history from being created. I am going to look at the command line options first, then will discuss the file where some of the variables are stored, i.e. if you want to edit a file rather than use the command line then please scroll down a little.

Wiping History Before It Is Written

Before ending your session entering the command:

history -c

will clear the sessions history before it is written to HISTFILE.

No Duplicates a.k.a ignoredups

History does not remember duplicate commands i.e. if you run the ls command several times then History will only note it once.

several ls commands…(1/2) ls entry in History (2/2)
…one ls entry in History (2/2)

This is because the ignoredups option is enabled. Entering:


should return


If it does not, and if you want this option enabled then enter:

export HISTCONTROL=ignoredups

Spaces To Ignore a.k.a ignorespace

If a space is entered before a command then history will not remember the command. For example instead of:

sudo apt-get update


 sudo apt-get update

The commands look very similar, however the second one has a space at the beginning.

The ignorespace option controls if commands starting with space are ignored. To see if it is enabled enter:


and look for ignorespace. If it is not enabled and you want to enable it, enter:

export HISTCONTROL=ignoredups

Ignoring Spaces & Duplicates a.k.a ignoreboth

If echo $HISTCONTOL returns:


Then History is ignoring spaces and duplicates. If you want to enable both then enter:

export HISTCONTROL=ignoreboth

Changing How Much History Is Stored a.k.a. HISTSIZE

Earlier I mentioned $HISTSIZE, which for me stores 1000 entries. The number of entries recorded can be changed via the command:

export HISTSIZE=200

With the above I can set the number of historical commands recorded to 200.

Changing History Size

The History Settings File…

As I discussed at the beginning of the this section, all the History settings are stored in a file in the users profile (it is a per user file!). If you prefer to edit the this file, rather than entering commands in the command line then take a look at (via Less) or edit (via a text editor like Nano):



nano ~/.bashrc


One thought on “The Power Of History (Linux)

Comments are closed.