Commands: Simple text-based terminal tools

There was a time, long before the ubiquitous windows management on our desktops and desktops, when people used commands to get things done from the computer. These commands were entered on the terminal. There were no beautiful interfaces involved like the menus or the buttons. To see the contents of the directory, they go to the terminal and enter the words ‘ls’ and they see a list of names of files and directories displayed on their terminal with no fancy figures. Similar for every action we now do on the computer, there was an associated command.

Command means to order somebody to do somethings. In the world of computers, there are a fixed number of commands that we give to the computer and the computer can understand these commands. Though there are many research works to make computer understand a large number of commands. But as of 2012, the computers can only understand a limited set of commands.

A command is a software. But normally the word ‘command’ corresponds to the software that run on the terminal. These commands take as input textual information from the user (like the name of a file or an image) and generates textual information as output.

Some commonly used commands in Linux
• ls (to list the contents of a directory)
• cd (change directory)
• pwd (prints current working directory)
• grep (to search for a pattern in a file)

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.