Untitled Document

Chapter - 2 Command Structure and Usage

Locating Commands -

We all know that Unix system is command based and known for commands which are very strong and helpful to work on files/ directories. . All our unix commands are single words commands like cat , find, mkdir etc . and all are in lowercase . whenever we run the command , shell runs the program files of those commands stored in directores . Most of our command files are stored in directory /bin . In simple words a command is nothing but a program whose file is stored in /bin; mostly written in language c.

The easiest way of knowing the location of command file is to use type command.

$ type cal

cal is /bin/cal

Path Variable -

We run the commands on Unix / Linux terminal but the question arise is How shell searches / remembers the path of directories when we run any command .? The answer is PATH(All are in capital letters) variable which stores the sequence of directories that shell searches when a command is run.

echo $PATH

When we run a command , the shell searches the list obtain from above path in the sequence specified to locate and execute it.

The essential UNIX commands for general use are located in the directories /bin and /usr/bin .

Internal and External Commands

Command like - cat , ls , wc have the independent existance in /bin directory hence they are known as External Commands . Most of the commands are external in nature , but there are few commands which are not found anywhere and some which are normaally not executed from the PATH variable location even though they are located there like echo command .

$ type echo

echo is a shell builtin

when you run echo command shell doesn't look in its PATH to locate it . Rather it executes it from its own set of built in commands that are not stored as separate files. These built in commands , of which echo is a member is called as Internal commands . If you see the output of above type command you will get an idea what we are trying to explain here .

Its a shell who does all this work and it starts running when you log in and dies when you log out .The shell is an external command with a difference ; it posses its own set of internal commands . So if a command exists both as an internal command and as an external command , then shell will accord top priority to its own internal command of teh same name . This is exactly the case with echo . We will see shell in more details in our upcoming chapters.

Commands and Its structure -

To use the UNIX to its ultimate power , one need to understand and learn the syntax of important UNIX commands . We have already seen few commands , where we have given few words e.g tput clear , cat mangesh.txt . The first tput or cat is aUNIX command and the additional word supplied to command is called arguments.So in above example clear and mangesh.txt are nothing but arguments . Similarly we have Options for the commands ,e.g ls -l , wc -c in this case is a command and -l is the option supplied to it ,same for wc - it is a command supplied with option -c. We will see what exactly the difference between the arguments and option in our further discussion .

Running command with arguments and Options -

Lets try to explore the commands with arguments - In our prevous exmple we have run the cal command without any argument and options.
If i want to get the calendar for current month , i will run cal 08 it will display the calendar for the month of august

cal 08

In above exampple cal is the command and 08 is argument given to the command.

e.g lets try to display the long listing of files with ls commnad . run ls -l , here ls is the command and -l is the option given to command

ls -l

Difference between Options and Argumnets -

Options Argumnets
Options are predetermined to the command can be anything that user can supply
usually start with (-) can be file , directory , word
e.g ls -l , ps -ef cp test.txt, mv test.txt , tput clear
Not all commands need arguments /options but yes some commnads need specifically options/arguments e.g cut .

Exceptions -

There are commnads which doest take any argument and option like pwd . And there are commands like who ,ls who runs with and with out arguments and options . It is nearly impossible for a user to remember all the commands and their option , UNIX is very well documented and have manuals for all his commands we will the man command and how helpful it is .

Combining Commands -

Unix/Linux provides great flexibility of running muultiple commnads on same command line each command has to be separated by ; . So far we have run the commands seperately . lets combine two commands in same command line.

cal 08 ; ls -l mangesh

when a command line sees a ; shell understands that the command on each side should be executed seperately . The ; is known as metacharacter which has special meaning to shell .
we can even redirect or store the output of above execution in a seprate files .

(cal 08 ; ls -l ) > test.txt

The o/p redirection was done by > operator we will see the details about the operator and how this works in our upcoming chapters

In Unix/Linux we have a facilty to enter the multiple command even before completing the execution of your long running command .Just enter the commands that you want to run , shell stores the input into its buffer and runs it when the execution of previous gets completed

Man Command & Its usage -

Its really impossible for a user to know all the options of command he/she uses . Even its difficult to know the name of all the commands , here is the document that help us to know the usage , details and options available for the command .

man <command _name>

replace the command name with command , the man will display the NAME, SYNOPSIS and DESCRIPTION along with the different options the command has -
The entire man page for the command given will be dispalyed on your screen page wise .To dispaly the contents man uses three types of pager.

  • more -Berkely's pager which is available in almost all unix an dlinux system
  • less- which is advanced pager than more and has good search and replace features like vi
  • to navigate throug man page press f - to move forward one page , b - to move one page back , q - to quit man page , to search content from man page type / and press n to search forward
  • man -k will display the one liner description of command
On Some systems , echo $PAGER not show you any value at all , in which cas man is using a default pager. Some systems set this variable in the file /etc/default/man instead.

Note - We have Covered Detailed Information about man command in our list of Linux Command Page - Please see man command page for more information

similar commands to man -

apropos <command name> will dispaly the commands and files associated to command given to apropos
whatis <command name>. will display similar one liner display of command like man -k
Some commands have too many options and sometimes quick lookup facility is what you need . Most Linux commnads offer the --help option that displays compact listing of all options . you can spot the vi option you are looking for this .A Linux command invariably offers far more options than its UNIX counterpart . You will find this lookup facility quite useful when you know the usage of the options but can't recollect the one you require. vi --help

Key Strokes when things go wrong -

While running commands and working on UNIX/Linux terminal it may haapen that , you need to recover from your current state to orignal or you may need to kill or stop the command execution on terminal , so here are the most common key strokes that can help you to recover when things go wrong.

Key Strokes Utility
[ctrl-h] Erases Text
[ctrl-c] or [delete] Interupts a command
[ctrl-d] terminates login session or a programm.
[ctrl-s] Stops Scrolling of Screen output and locks keyboard
[ctrl-q] Resumes Scrolling of screen output and unlocks keyboard
[ctrl-u] kills command line without executing it
[ctrl-z] Suspends Process and returns shell prompt;use fg to resume job
Untitled Document Scroll To Top Untitled Document