Linux history command - Bash history tutorial for beginner

Linux bash history keeps every command a user typed in the command line terminal into a file named .bash_history. What good about this is that user can re-used the command in case they are lazy to type a very long argument presented with the command. It also faster to use bash command history rather than typing the same command all over again. That makes working in Linux command line virtual terminal a bit fun.

Here is a description from the bash history manual page:

Many programs read input from the user a line at a time. The GNU His-
tory library is able to keep track of those lines, associate arbitrary
data with each line, and utilize information from previous lines in
composing new ones.

Linux bash history command examples

Linux bash history command available in all Linux distributions including Slackware Linux and Ubuntu server. What you need to do to view previously typed commands is just invoke history from Linux command prompt. Here is an example of Linux history command with no option running in Ubuntu server system command line terminal:

[email protected]:~$ history
1 ping
2 clear
3 ping -c6
4 ping -c3
5 ifconfig
6 ping -c3
7 route ---help
8 man route
9 route
10 history

In the example above, number 1 is the oldest command and number 10 is the latest command that a current user has typed. If you never clear the history file, you might see hundreds of commands that you typed including the latest 'history'. The bash history command would print only the latest page of the command history. However you can view bash commands history from the first page with pipe, like an example below:

[email protected]:~$ history | less

Let's see another example of bash history options. You can also use event designators to run command in the history using the ! key. See an example below:

[email protected]:~$ !8
man route
[email protected]:~$

The example above run number 8 command in the history file, which is the man route command. Here is a complete options available with event designators:

Event Designators
An event designator is a reference to a command line entry in the hisâ
tory list.

! Start a history substitution, except when followed by a blank,
newline, = or (.
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command. This is a synonym for â!-1â.
Refer to the most recent command starting with string.
Refer to the most recent command containing string. The trailâ
ing ? may be omitted if string is followed immediately by a newâ
Quick substitution. Repeat the last command, replacing string1
with string2. Equivalent to ââ!!:s/string1/string2/ââ (see Modâ
ifiers below).
!# The entire command line typed so far.

There are many options available if you want to create a script using bash history. You can check other history options in the info page. Just type info history to open it.

You can clear Linux bash history if it is too large or you don't want other people see what you were doing in the command line terminal. If you want to clear the bash history, use history command with -c option. See example below:

[email protected]:~$ history -c

Linux history command can be really helpful when we are working in command line terminal. We can save a lot of times not typing regularly used command such as ls, cd and many more. However, keeping all bash history commands also might cause security threat to the system. For example, consider the root bash history file was read by unauthorised person. What you can do is always clear history after root session.

You can also delete line in history file. Let's look at the example Linux history file below:

1 updatedb
2 sudo updatedb
3 updatedb
4 sudo updatedb
5 locate mysql
6 cd /var/www/
7 ls
8 cat index.html
9 ls -l
10 cd ..

If I want to delete the history line number 4, what I have to do is to use history command with '-d 4' option. The -d option deletes history entry while the 4 argument is history entry line 4. See the example below:

[email protected]:~$ history -d 4

Now let's look at the Linux history file again:

1 updatedb
2 sudo updatedb
3 updatedb
4 locate mysql
5 cd /var/www/
6 ls
7 cat index.html
8 ls -l
9 cd ..
10 ls -l

The history entry number 4 has been deleted and the previous number 5 entry is now at number 4.

Here are other history command options that you can try:

  • -a — Appends to history file.
  • -n — Loads from history file.
  • -p — Performs history lookup/substitution.
  • -r — Reads history from a file.
  • -s — Adds new history entries.

That is all the Linux history command options regularly used in Linux administration.

Back to Linux basic commands main page.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.