Pages

Tuesday, December 15, 2015

history command one liners


Display timestamp using HISTTIMEFORMAT

Typically when you type history from command line, it displays the command$ and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.


# history |head -10
    1  ifconfig -a
    2  \sbin\ifconfig -a
    3  /sbin/ifconfig -a
    4  /sbin/ifconfig -a|more
    5  pwd
    6  ls -ltr
    7  ?
    8  man
    9  echo $shell
   10  ls -ltr
# export HISTTIMEFORMAT='%F %T '
# history |head -10
    1  2010-12-12 09:39:06 ifconfig -a
    2  2010-12-12 09:39:06 \sbin\ifconfig -a
    3  2010-12-12 09:39:06 /sbin/ifconfig -a
    4  2010-12-12 09:39:06 /sbin/ifconfig -a|more
    5  2010-12-12 09:39:06 pwd
    6  2010-12-12 09:39:06 ls -ltr
    7  2010-12-12 09:39:06 ?
    8  2010-12-12 09:39:06 man
    9  2010-12-12 09:39:06 echo $shell
   10  2010-12-12 09:39:06 ls -ltr


Search the history using Control+R

When you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully.
Press Control+R and type the keyword.

# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]
(reverse-i-search)`red': cat /etc/redhat-release
[Note: Press enter when you see your command, which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)

Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd, which will display service httpd stop from the command history, select this command and change the stop to start and re-execute it again as shown below.

# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt]
(reverse-i-search)`httpd': service httpd stop
[Note: Press either left arrow or right arrow key when you see your command, which will display the command for you to edit, before executing it]
# service httpd start

Repeat previous command quickly using 4 different methods

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.

up arrow  -to view the previous command and press enter to execute it.
!!   -to execute last executed command
!-1  -to execute second last executed command
Control+P  -will display the previous command, press enter to execute it

Execute a specific command from history

In the following example, If you want to repeat the command $4, you can do !4 as shown below.

# history | tail
   78  2010-12-12 10:50:34 crontab -l
   79  2010-12-12 10:50:51 crontab cron-file.txt
   80  2010-12-12 10:50:54 crontab -l
   81  2010-12-12 10:55:05 clear
   82  2010-12-12 10:55:08 history
   83  2010-12-12 10:55:15 export HISTTIMEFORMAT='%F %T '
   84  2010-12-12 10:55:17 history
   85  2010-12-12 11:08:44 history | more
   86  2010-12-12 11:09:12 /sbin/ifconfig -a|more
   87  2010-12-12 11:10:42 history | tail
You have new mail in /var/spool/mail/sandeep

# !78
crontab -l
*/10 * * * * /home/sandeep/cron-file

Execute previous command that starts with a specific word

Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example,
typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep yp’.

# !ps
ps
  PID TTY          TIME CMD
 4951 pts/1    00:00:00 bash
 8124 pts/1    00:00:00 ps

Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.

# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

Change the history file name using HISTFILE

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file.

# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

If you have a good reason to change the name of the history file, please share it with me, as I’m interested in finding out how you are using this feature.

Eliminate the continuous repeated entry from history using HISTCONTROL

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.

# pwd
# pwd
# pwd
# history | tail -4
44  pwd
45  pwd
46  pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
47  history | tail -4

# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd [Note that there is only one pwd command in the history, even after
executing pwd 3 times as shown above]
58  history | tail -4

Erase duplicates across the whole history using HISTCONTROL

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.

# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38  pwd
39  service httpd stop
40  history | tail -3
# ls -ltr
# service httpd stop
# history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -ltr
39  service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40  history | tail -6

Force history not to remember a particular command using HISTCONTROL

We can instruct history to ignore the command by setting HISTCONTROL
Example is to ignorespace AND typing a space in front of the command as shown below.
As a best practice, don’t hide purposefully anything from history.

# cat temp-file.txt
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
#  cat temp-file.txt[Note that there is a space at the beginning of cat,to ignore this command from history]
# history | tail -3
67  ls -ltr
68  pwd
69  history | tail -3

Clear all the previous history using option -c

Sometime you may want to clear all the previous history, but want to keep the history moving forward.

# history -c

Substitute words from history commands

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.

In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.

# ls temp-file.txt
temp-file.txt
You have new mail in /var/spool/mail/sandeep
# vi !!:$
vi temp-file.txt


Substitute a specific argument for a specific command.

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.

# ls -l temp-file.txt
-rw-rw-r-- 1 sandeep sandeep 12 Dec 12 09:37 temp-file.txt
# cp temp-file.txt temp-file.txt.txt
# ls -l !cp:2
ls -l temp-file.txt.txt
-rw-rw-r-- 1 sandeep sandeep 12 Dec 16 21:04 temp-file.txt.txt
# ls -l !cp:1
ls -l temp-file.txt
-rw-rw-r-- 1 sandeep sandeep 12 Dec 12 09:37 temp-file.txt

In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the first and second argument as shown above) of cp and substitutes it for the ls -l command as shown below.

# ls -l !cp:$
ls -l temp-file.txt.txt
-rw-rw-r-- 1 sandeep sandeep 12 Dec 16 21:04 temp-file.txt.txt


Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.

# export HISTSIZE=0
# history
# [Note that history did not display anything]

Ignore specific commands from the history using HISTIGNORE

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.

# export HISTIGNORE="pwd:ls:ls -ltr:"
# pwd
# ls
# ls -ltr
# cd /

# history | tail -3
79  export HISTIGNORE="pwd:ls:ls -ltr:"
80  cd /
81  history
[Note that history did not record pwd, ls and ls -ltr]


Back To Top
Home

No comments:

Post a Comment