Sunday, 17 April 2016

Mastering Linux Command line history


Search the history using Control+R

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

# [Press Ctrl+R from the command prompt]
It displays the reverse-i-search prompt, like :
(reverse-i-search) : 

Type some part of command and it will display the most related command from history, Like if you type 'red' - 
(reverse-i-search)`shaan': cat /etc/shaan123.txt

Press enter when you see your command, which will execute the command from the history
# cat /etc/shaan123.txt


Repeat previous command quickly

4 different ways to repeat the last executed command :
   a. Use the up arrow to view the previous command and press enter to execute it.
   b. Type !! and press enter from the command line.
   c. Type !-1 and press enter from the command line.
   d. Press Control+P will display the previous command, press enter to execute it.


Execute a specific command from history

If you want to repeat the command #4, you can do !4 as shown below.
# history | more
1 service network restart
2 exit
3 id
4 cat /etc/shaan123.txt

# !4
# cat /etc/shaan123.txt


Display timestamp using HISTTIMEFORMAT

When you type history from command line, it displays the command prompt and the command.
For auditing purpose, display the timepstamp along with the command, Like :
# export HISTTIMEFORMAT='%F %T '
# history | more
1 2008-08-05 19:02:39 service network restart
2 2008-08-05 19:02:39 exit
3 2008-08-05 19:02:39 id
4 2008-08-05 19:02:39 cat /etc/shaan123.txt


Clear history

Clear all the previous command history using option -c
You may want to clear all the previous history, but want to keep the history moving forward.
# history -c



Some advanced history options

Ignore specific commands from the history using HISTIGNORE
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.
Note : 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
# service httpd stop
# history | tail -3
79  export HISTIGNORE="pwd:ls:ls -ltr:"
80  service httpd stop
81  history
Note : history did not record pwd, ls and ls -ltr


Force history not to remember a particular command using HISTCONTROL
When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace.
After this setting, if you type a space in front of the command, it will not be recorded in history.
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# service httpd stop

Note : There is a space at the beginning of service, to ignore this command from history

# history | tail -3
67  ls -ltr
68  pwd
69  history | tail -3


Erase duplicates across the whole history using HISTCONTROL
The ignoredups removes duplicates only if they are consecutive commands.
To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups.
# 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 : The previous service httpd stop after pwd got erased
40  history | tail -6


Eliminate the continuous repeated entry from history using HISTCONTROL
In the following example pwd was typed 3 times, when you do history, you can see all the 3 continuous occurrences of it.
To eliminate duplicates, set HISTCONTROL to ignoredups.
# pwd
# pwd
# pwd
# history | tail -4
44  pwd
45  pwd
46  pwd
Note : 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 : There is only one pwd command in the history, even after executing pwd 3 times as shown above.
58  history | tail -4


Control the total number of lines in the history using HISTSIZE
Append the following two lines to the .bash_profile and re-login 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

No comments:

Post a Comment

Note: only a member of this blog may post a comment.