History will shows the list of commands which you have executed previously. Would you like to view information on when you executed these commands,
A few different uses of history command as below,
Here, have different types of time format for displaying History command,
%d - Day
%m - Month
%y - Year
%T - Time
%H - Hours
%M - Minutes
%S - Seconds
%F for setting year/month/day
Basic History command:
history
18 exim -bpc
19 exim -bp
20 exim -bp | awk '/^ *[0-9]+[mhd]/{print "exim -Mrm " $3}' | bash
21 service exim restart
22 pgrep -f httpd
23 pgrep -f proftpd
Few types of time format for history command,
export HISTTIMEFORMAT="%F %T "
history
1002 2014-06-24 18:27:49 ls -al
1003 2014-06-24 18:27:51 free -m
1004 2014-06-24 18:27:52 df -h
1005 2014-06-24 18:27:56 fdisk -l
1006 2014-06-24 18:28:03 ls
1007 2014-06-24 18:28:10 du -sch *
export HISTTIMEFORMAT="%h/%d - %H:%M:%S"
history
1002 Jun/24 - 18:32:25 free -m
1003 Jun/24 - 18:32:27 top -c
1004 Jun/24 - 18:32:33 fdisk -l
1005 Jun/24 - 18:32:44 netstat -anp | grep httpd
1006 Jun/24 - 18:32:50 ps aux | grep php
1007 Jun/24 - 18:33:04 du -sch *
export HISTTIMEFORMAT='%r-%d-%b-%Y '
history
1017 06:49:09 PM IST-24-Jun-2014 history 20
1018 06:51:56 PM IST-24-Jun-2014 ls
1019 06:52:59 PM IST-24-Jun-2014 df -h
1020 06:54:48 PM IST-24-Jun-2014 ps aux | grep java
1021 06:57:27 PM IST-24-Jun-2014 history | tail -5
1022 06:58:48 PM IST-24-Jun-2014 du -sch *
export HISTTIMEFORMAT="%T "
history
1017 18:49:09 history 20
1018 18:51:56 ls
1019 18:52:59 df -h
1020 18:54:48 ps aux | grep java
1021 18:57:27 history | tail -5
1022 18:58:48 du -sch *
1023 19:01:56 man strftime
Open the /etc/bashrc file in a text editor and add the above export HISTTIMEFORMAT , Once you have updated to that file check on new console.
Search command from history using grep command :
history | grep httpd
266 2014-06-24 17:28:46 /etc/init.d/httpd restart
753 2014-06-24 17:28:46 /etc/init.d/httpd stop
812 2014-06-24 17:28:46 vim /etc/httpd/conf/httpd.conf
Get command using head and tail :
You probably do not need to see all of the last 1000 commands, would you like to see first and previous 20 commands you typed are displayed
history 10 or
history | tail -50
1003 2014-06-24 18:39:49 df -h
1004 2014-06-24 18:40:15 export HISTIGNORE='ls -l:pwd:date:'
1005 2014-06-24 18:40:17 history
1006 2014-06-24 18:42:13 history | grep httpd
1007 2014-06-24 18:44:18 du -sch *
history | head -5
11 2014-06-24 17:28:46 ls
12 2014-06-24 17:28:46 cd -
13 2014-06-24 17:28:46 ls
14 2014-06-24 17:28:46 rm -rf hioxindia/
15 2014-06-24 17:28:46 ls
Restore default format:
export HISTTIMEFORMAT=""
history
1017 history 20
1018 ls
1019 df -h
1020 ps aux | grep java
1021 history | tail -5
1022 du -sch *
Execute command from the history list :
!! is executes the last command in the history.
# !!
du -sch *
du -sch *
Executes the command from the history file which is given number with !
# !1003
df -h
df -h
executes a command which is most recent matching string from the history.
# !ps
ps aux | grep java
ps aux | grep java
If you press CTRL-R key to perform a “reverse-i-search”.
Then press right Arrow key just displaying command not execute or if press enter command it will be executed.
Manage total number of History size stored :
Add the following line to the .bash_profile and relogin to the bash shell, this configuration only 700 command only stores.
HISTSIZE=700
HISTFILESIZE=700
HISTFILESIZE=700
HISTSIZE - Manage how many history commands to stores current session in the history file.
HISTFILESIZE - Manage how many history commands to keep in HISTFILE
List out history command for root :
# cat ~/.bash_history
Delete or Clear History of Commands:
# history -c
Comments (0)