History Command in Linux With Examples

History Command in Linux

History command is used to list the previously used commands. This tutorial explains the uses of the history command. 



1. Method to list all executed command in Linux.

Run the following command to list the previously executed command.

[root@linuxhelp Desktop]# history
1  history
2  ls -l
3  time
4  export HISTIGNORE='ls -l:w:'
5  history
6  export HISTIGNORE='ls -l:w:'
7  history | tail -2
8  history | tail -3
9  history | tail -5
10  history | tail -7
28  uptime
29  history | tail -3
30  history

2. To reverse search the previously executed command.

Method I
To know your previously executed command hit ‘ctrl+r’ and type the command name starting letter and it will displays the command on the terminal.

[root@linuxhelp Desktop]#
(reverse-i-search)`tcp': tcpdump
[root@linuxhelp Desktop]# tcpdump

Method II
Press ctrl+p to check your previously executed command. Use up arrow and down arrow to view  the previously executed commands.
The below command will display the last executed command with an output as shown below.

[root@linuxhelp Desktop]# !!
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
12:28:35.542106 STP 802.1w, Rapid STP, Flags [Proposal, Learn, Forward, Agreement], bridge-id 6000.00:17:b0:74:a0:80.8014, length 47
12:28:35.552948 ARP, Request who-has tell, length 46
12:28:35.553385 IP > google-public-dns-a.google.com.domain: 46478+ PTR? (43)
12:28:35.609092 IP google-public-dns-a.google.com.domain > 46478 NXDomain 0/0/0 (43)

3. To list only specific command history by using ‘grep’.

Run the following command to list only the specific commands that you have used before.

[root@linuxhelp Desktop]# history | grep ls
5  ls -l
6  cd VMware Tools/
7  ls -l
8  cp VMwareTools-9.6.0-1294478.tar.gz  /home/linuxhelp/Downloads/
10  ls -l
11  tar -xvf VMwareTools-9.6.0-1294478.tar.gz
13  ls -l
14  cd vmwa

4. To Recall the command history by using command number.

Run the following command to recall the previous commands and tail option is used to limit the history commands to be viewed.

[root@linuxhelp Desktop]# history | tail -5
298  history | grep ls
299  history
300  ls
301  nmon  -V
302  history | tail -5

The factorial ‘!’symbol is used to execute a command in a particular line. For example, the following command will give the output by executing the 300th line.

[root@linuxhelp Desktop]#!300
demo                  rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm  sam
easy-stands.odt       rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm  sample.c
easytag-2.2.6.tar.xz  RPM-GPG-KEY.dag.txt                         switch.c

5. Recall the command by using command name with factorial symbol ‘!’

Run the following command to view the history by using the starting name of a particular command.

[root@linuxhelp Desktop]# !nmon –V
nmon  -V 
nmon verion 14i

6. Limit the total number of line in history

Go to the history configuration file and enter the ‘HISTSIZE’ to limit the storage of history in your system.

[root@linuxhelp Desktop]# vim ~/.bash_history


7. To list specific user executed commands

The history of the particular user which will be stored under the ~/.bash_history which can be viewed as follows.

[root@linuxhelp Desktop]# vim ~/.bash_history





8. To display your history commands with time stamps.

The below command will show the history along with the time stamps of the command.

%F Equivalent to %Y – %m – %d
%T Replaced by the time ( %H : %M : %S )

[root@linuxhelp Desktop]# export HISTTIMEFORMAT='%F %T '
[root@linuxhelp Desktop]# history | tail -5

317  2016-04-25 19:26:26 vim ~/.bash_history
318  2016-04-25 19:41:31 export HISTFORMAT='%F %T '
319  2016-04-25 19:42:12 history | tail -5
320  2016-04-25 19:42:51 export HISTTIMEFORMAT='%F %T '
321  2016-04-25 19:43:07 history | tail -5

9. Filter the specific command in history

The below command will shows the usage of the filtered commands such as ls,ps and w in history. Retype ls ,w and ps it will execute and it will not be stored in command history as it was a temporary command. If you want to make it permanent go to this configuration file ~/.bash_profile and enter the command inside it.

[root@linuxhelp Desktop]# export HISTIGNORE='ls:ps:w: '
[root@linuxhelp Desktop]#ls
[root@linuxhelp Desktop]#ps
[root@linuxhelp Desktop]#w
[root@linuxhelp Desktop]#uptime
[root@linuxhelp Desktop]#uname -r
[root@linuxhelp Desktop]# history | tail -5
325  2016-04-25 20:32:23 export HISTIGNORE='ls:ps:w: '
326  2016-04-25 20:32:43 uname -r
327  2016-04-25 20:32:47 uptime
328  2016-04-25 20:32:59 history
329  2016-04-25 20:35:40 history | tail -5

10. Using history command with option –c.

Use the following command to check the total number of commands available in the history.

[root@linuxhelp ~]# history | tail -5
35  rm -rf index.php
36  service httpd restart
37  cd
38  history
39  history | tail -5

To clear or delete your command history by using option ‘–c’

[root@linuxhelp ~]# history –c

After clearing all the data check the history using the following command.

[root@linuxhelp ~]# history
1  history

11. Command to disable the storage of history.

To disable storage of history commands first check your history commands.

[root@linuxhelp ~]# history
1  history
2  ls
3  ls -l
4  pwd
5  uptime
6  date
7  w
8  ps
9  did
10  dig
11  ifconfig
12  ip a
13  history

After checking the history, disable the storage of history command by using the following command.

[root@linuxhelp ~]# export HISTSIZE=0

To verify this process type 'history'.

[root@linuxhelp ~]# history

It is only a temporary process. To make permanent changes go to history configuration file ~/. bash_profile  and type unset before HISTSIZE.


12. Deactivate the assigned application in history.

To de-activate the active process like HISTSIZE, HISTIGNORE and HISTTIMEFORMAT,  just add unset command with that process name and the process will be deactivated.

[root@linuxhelp Desktop]# unset HISTSIZE
[root@linuxhelp Desktop]#pwd
[root@linuxhelp Desktop]#uname -r
[root@linuxhelp Desktop]# history
334  2016-04-25 20:53:05 pwd
335  2016-04-25 20:53:09 uname -r
336  2016-04-25 20:53:13 history

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: History command
user image Author :  Lucas