A Shell script to search for a command pattern

Today I have been looking for a command to compare two files simultaneously (the colour schemes of KDE) and used diff. But that did not suit my requirement of viewing them simultaneously. There are editors like vi which provide simultaneous viewing of the files and I tried

$vimdiff file_1 file_2

To search for some specific command, we can use ‘man’ and the pattern

$man -k diff

and it showed many commands like zdiff,vimdif,..

The advantage of this is that it searches for the pattern and a one line abstract of these commands (available in man pages) for the corresponding pattern

But the major disadvantage is that if you have some commands in your system which does not have a man page, it returns “nothing appropriate”

Here is a simple shell script which searches for command-pattern in the directories in the PATH variable

Just make a change in the PATH_DIR_COUNT depending on the number of directories in it.
(use echo $PATH to find this out)

#!/bin/sh
# A simple Command to search for a pattern in the commands available in the directories mentioned in PATH environment varible
# Usage: cmd_search pattern

PATH_DIR_COUNT=6 #Number of Directories in the PATH environment variable

if test $# -lt 1
then
echo 'Usage: cmd_search pattern'
exit
fi

for i in `seq 1 $PATH_DIR_COUNT`
do
    ls `echo $PATH | cut -d ":" -f $i`|grep $1
done

To use the shell script, save it as cmd_search and

$chmod u+x cmd_search
$ ./cmd_search command_pattern

For example

$./cmd_search diff

Output

bzdiff
combinediff
dehtmldiff
diff
diff3
diff-jars
diffpp
diffstat
editdiff
espdiff
filterdiff
fixcvsdiff
flipdiff
gendiff
grepdiff
hdifftopam
interdiff
lsdiff
pamtohdiff
pdiff
rcsdiff
recountdiff
rediff
sdiff
sgmldiff
splitdiff
tiffdiff
unwrapdiff
vimdiff
zdiff

Shell Script to find the most commonly used commands

Preparing for writing a blog on the commonly used commands by me,I came out with a unique challenge. How can I find out my most frequently used commands?
Then I thought writing a shell script for that, Though I need not have one. The following at the command prompt will suffice

$history | awk '{print $2}' |sort | uniq -c | sort -rg

Output

   24 vim
   23 gcc
    9 ./a.out
    5 ls

This will give all the commands along with their frequency of use

But still if I could find out the most commonly used n number of commands, that would be more advantageous. So here’s the shell script
So the usage of command is cmd_history [n]
n is an optional number

# To Display the frequency of commands so far used
#Usage: ./cmd_history [n]
# where n is a number(optional)

history | awk '{print $2}' | sort|uniq -c | sort -rg  >tmp.$$
if [ $# -ge 1 ]
then
   head -$1 tmp.$$
else
   cat tmp.$$
fi
rm -f tmp.$$

Output

$./cmd_history 3

$ 25 vim
23 gcc
9 ./a.out

NOTE: You can even use

$ history | awk '{print $2}' | sort | uniq -c | sort -rg | head -5

without using the shell script where instead of 5, you can specify your own number

Also, this uses history of commands, so you should not have cleared your history of commands before using.