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:

history
geektechstuff_history_1
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
geektechstuff_history_2
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

e.g.

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.

geektechstuff_history_5
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:

!LINENUMBER

e.g.

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

!24
geektechstuff_history_6
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.

geektechstuff_history_7
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).

geektechstuff_history_8
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.

geektechstuff_history_10
.bash_history

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

echo $HISTFILE
echo $HISTFILE
echo $HISTFILE

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:

echo $HISTSIZE

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

echo $HISTSIZE
echo $HISTSIZE

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.

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

This is because the ignoredups option is enabled. Entering:

echo $HISTCONTROL

should return

ignoredups

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

enter

 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:

echo $HISTCONTROL

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:

ignoreboth

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=NUMERICAL_VALUE
e.g.
export HISTSIZE=200

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

geektechstuff_history_15
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):

~/.bashrc

e.g.

nano ~/.bashrc
~/.bashrc
~/.bashrc

 

One thought on “The Power Of History (Linux)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.