• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

History Command in Linux With Examples

70

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
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 192.168.1.21 tell 192.168.1.12, length 46
12:28:35.553385 IP 192.168.5.83.50388 >  google-public-dns-a.google.com.domain: 46478+ PTR? 21.5.168.192.in-addr.arpa. (43)
12:28:35.609092 IP google-public-dns-a.google.com.domain >  192.168.1.83.50388: 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
ls
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 &ndash 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

bash

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

user_commands

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 &ndash %m &ndash %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 &ndash 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 ‘ &ndash c’ .

[root@linuxhelp ~]# history &ndash 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

Tags:
lucas
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

what is the package need to install for this command?

A

this command is a feature of bash, so you have to change your shell to /bin/bash if you don’t find bash install it yum install bash or apt-get install bash

Q

any other way to see commad history?

A

similarly you can open .bash_history to track of commands

Q

I got no command on my history, do I need to turn it on?, how to do it

A

to check history command turned on set -o | grep history if it is turned off then set -o history and then use below commands echo $HISTFILE echo $HISTSIZE echo $HISTFILESIZE If the first one

Q

how to save history command output to a file

A

use like this, history > file_name

Q

What is the history command denotes?

A

It dedicates the history of the file and it can view the history of the file or document.

Related Tutorials in History Command in Linux With Examples

Related Tutorials in History Command in Linux With Examples

History Command in Linux With Examples
History Command in Linux With Examples
Apr 27, 2016

Related Forums in History Command in Linux With Examples

Related Forums in History Command in Linux With Examples

SQLite
caden class=
How to find the Manually Installed Packages On CentOS 7.6
Aug 26, 2019
History command
sebastian class=
how to run 251th command from history in linux
May 18, 2017
History command
albiejames class=
How to check history command with time in Linux?
Jan 10, 2019
History command
jacob class=
How to set History Display Date And Time For Each Command
Dec 19, 2019
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Jayce ?
What are the types of table used in IPtables

What are the various types of table used in IPtables and how to use that for my server security?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.