You are on page 1of 61

Key To Unix

Version:2

Revised Edition:November 2009

INDEX
1. Introduction to UNIX.............................................................................................................................. 1
1.1. Single-User Systems............................................................................................................................................ 1
1.2. Multi-user Systems............................................................................................................................................... 1
1.3. Hardware - Multi-user Systems............................................................................................................................ 1
1.4. Classes of Multi-User Systems............................................................................................................................ 1
1.5. Multi-User Operating System - UNIX................................................................................................................... 2
1.6. STALLMAN AND TORVALDS: GNU AND LINUX................................................................................................2
1.7. History of UNIX..................................................................................................................................................... 2
1.8. Features and Benefits of UNIX............................................................................................................................. 3
1.9. Versions of UNIX.................................................................................................................................................. 3
1.10. THE KERNEL AND SHELL................................................................................................................................ 4
1.11. Security for the Unix User................................................................................................................................... 4
1.12. Creating a Password.......................................................................................................................................... 5
1.13. Rules for Passwords.......................................................................................................................................... 5
1.14. Changing the Password..................................................................................................................................... 6
1.15. Check your current Directory the pwd command............................................................................................6

2. The UNIX File System............................................................................................................................ 7


2.1. The FILE in UNIX................................................................................................................................................. 7
2.2. THE STRUCTURE OF THE FILE SYSTEM.........................................................................................................7
2.3. Displaying the file Type......................................................................................................................................... 8
2.4. Creating Directories.............................................................................................................................................. 8
2.5.Changing Directories............................................................................................................................................. 9
2.6. The ls command................................................................................................................................................... 9
2.7. Listing the contents of the file:............................................................................................................................10
2.8. Removing Directories......................................................................................................................................... 10
2.9. Relative PathNames........................................................................................................................................... 10
2.10. Copying files:.................................................................................................................................................... 11
2.11. Moving or Renaming files:................................................................................................................................ 11
2.12. Removing Files:................................................................................................................................................ 11
2.13. File Security...................................................................................................................................................... 12
2.14. The File Owner, Group Owner and Other Users..............................................................................................12
File Owner................................................................................................................................................................. 12
Group Owner............................................................................................................................................................. 12
Other Users............................................................................................................................................................... 13
2.15. FILE SECURITY THROUGH FILE ACCESS PERMISIONS (FAP)..................................................................13
2.16. Changing the FAP for the File Owner, Group Owner and other users:............................................................14

3. Unix Utilities......................................................................................................................................... 15
3.1. The wc Command.............................................................................................................................................. 16
3.2. The pg Filter:...................................................................................................................................................... 16
3.3. The more Filters:................................................................................................................................................ 16
3.4. Pipes.................................................................................................................................................................. 17
3.5. The man command displays pages from the UNIX reference manual...............................................................17
3.6. Comparing two FILES The CMP Command....................................................................................................17
3.7. The comm-command further comparison..........................................................................................................17
3.8. The diff command............................................................................................................................................... 18
3.9. The banner command........................................................................................................................................ 18
3.10. The calendar command.................................................................................................................................... 18
3.11. Displaying the SYSTEM DATE with Date Command.......................................................................................18

3. 12. The who command.......................................................................................................................................... 19


3.13. Wild-Card Matching.......................................................................................................................................... 19
3.14. The * Wild-Card:............................................................................................................................................... 19
3.15. The ? Wild-Card:.............................................................................................................................................. 20
3.16. The [ ] Wild-Card.............................................................................................................................................. 20
3.17. Redirection....................................................................................................................................................... 21
3.18. Standard Error.................................................................................................................................................. 22
3.19. The two special files- /dev/null and /dev/tty......................................................................................................22

4. Simple Filters....................................................................................................................................... 22
4.1. The Sort Filter:.................................................................................................................................................... 23
4.2. Options of the sort Filter:.................................................................................................................................... 23
4.3. The Cut Filter...................................................................................................................................................... 25
4.4. The head Filter................................................................................................................................................... 26
4.5. The tail Filter:...................................................................................................................................................... 26
4.6. paste command:................................................................................................................................................. 26
4.7. The uniq command:............................................................................................................................................ 27
4.8. Translating Characters - the tr command...........................................................................................................27
4.9. Sending the output to some other file using tee command................................................................................28

5. The Vi Editor......................................................................................................................................... 28
The three modes of vi................................................................................................................................................ 29
5.2. Getting started with vi:........................................................................................................................................ 29
5.3. Cursor Movement Commands of vi:................................................................................................................... 29
5.4. Commands to Copy Lines:................................................................................................................................. 31
5.5. Saving and Exiting Commands:.........................................................................................................................31
5.6. Pattern Finding Commands:...............................................................................................................................31
5.7. Paging and scrolling:.......................................................................................................................................... 31
5.8. Customizing vi.................................................................................................................................................... 32
5.9. The set command............................................................................................................................................... 32
5.10. Options used by vi............................................................................................................................................ 32

6. Advanced Filters.................................................................................................................................. 33
6.1. The grep Filter.................................................................................................................................................... 33
6.2. Options with grep............................................................................................................................................... 33
6.3. sed Filter............................................................................................................................................................. 35
6.4. Reading and Writing to files (r,w):......................................................................................................................35
6.5. Bracketed regular expression:............................................................................................................................ 36
6.6. awk Filter :.......................................................................................................................................................... 36
6.7. Locating files using the find command:..............................................................................................................37
6.8. Options of the find command.............................................................................................................................37
6.9. The ln command................................................................................................................................................. 39

7. Process................................................................................................................................................. 39
7.1.Ps command....................................................................................................................................................... 39
7.2. Background Processing:.................................................................................................................................... 39
7.3. Requesting a Background Process:...................................................................................................................39
7.4. Checking on Background Process :...................................................................................................................40
7.5. Terminating a Background Process :.................................................................................................................. 40

8. Communications.................................................................................................................................. 41
8.1. To-way communication-The Write Command.....................................................................................................41
8.2. The mesg Command.......................................................................................................................................... 41
8.3. The mail command- The Mail Box...................................................................................................................... 42

8.4. The wall command:............................................................................................................................................ 42

9. Introduction to Shell............................................................................................................................ 43
9.1. Login process..................................................................................................................................................... 43
9.2. Types of shells :.................................................................................................................................................. 43
9.3. expr Command................................................................................................................................................... 44
9.4. Environment Variables:...................................................................................................................................... 44
9.5. The HOME variable:........................................................................................................................................... 44
9.6. The PATH variable:............................................................................................................................................. 44
9.7. Shell Script......................................................................................................................................................... 45
9.8. EXIT statement:.................................................................................................................................................. 45
9.9. Break AND continue:.......................................................................................................................................... 45
9.10. Exit status of the last command- the $?...........................................................................................................45
9.11. if..then..else condition....................................................................................................................................... 46
9.12. String test Operators........................................................................................................................................ 48
9.13. Logical operators.............................................................................................................................................. 48
9.14. The case...esac construct................................................................................................................................ 49
9.15. for..do..done..................................................................................................................................................... 50
9.16. while..do..done................................................................................................................................................. 51
9.17. Parameter handling in shell scripts..................................................................................................................52

APPENDIX A............................................................................................................................................. 53
Block commands in vi................................................................................................................................................ 53
Set Commands :........................................................................................................................................................ 53
Map command:.......................................................................................................................................................... 53

APPENDIX B............................................................................................................................................. 54
Shell Scripts and Shell Functions :............................................................................................................................ 55

Key To Unix

Page 1 of 57

1. Introduction to UNIX
1.1. Single-User Systems
The personal computer (PC) is a small, general-purpose system that can execute programs to perform a wide variety
of tasks. The PC, however, was designed for use by one person at a time; that is, it is Single-User oriented with MSDOS as the de facto standard operating system for this range of machines. Single user systems became very popular
due to the low cost hardware and wide range of software available for these machines.

1.2. Multi-user Systems


As opposed to single-user systems there are also larger systems, which more than one person can use at any time.
Such systems are referred to as multi-user systems.
Multi-user systems would be required when a number of applications have to be run simultaneously, or common
resources, like printers and disks, are to be shared by a number of users.

1.3. Hardware - Multi-user Systems.


While the hardware components of a multi-user system are similar to that of a single-user system, the following
differences should be noted:
The CPU of a multi-user system is more powerful and has capabilities to support multi-programming and multitasking, two features essential for multi-user systems.
The hard disk of a multi-user system is bigger in capacity.
Most multi-user systems use magnetic tape as external storage for backup of software. Single-user systems use
floppies as the backup device. This is because multi-user systems have large hard disks, which have to be backed up
quickly and conveniently.
Each user communicates with the system through a keyboard and a VDU - these are together referred to as the user
terminal or node. A terminal may also be a PC or PC-XT. The terminal is connected to the system unit by cables
through RS232 ports. The number of terminals varies between systems.
In multi-user systems, there is one terminal designated as the console.
The console consists of a Keyboard and VDU connected to the CPU directly through cables. From the console, the
operation of the system is controlled. When power is switched on for the system unit, messages regarding the boot
procedure appear on the console only. After the boot procedure is over, all the users can start working at their
terminals.
The console is also used to send messages to the users of the system and for other operations such as checking the
external storage media or taking backups of data.
A UNIX system may have one or more printers. The printers are generally high - speed printers capable of printing
over 300 lines per minute.
In multi-user systems, which have only one printer, there may be situations where more than one user has given a
print command at the same time. In such cases, the files that have to be printed out are queued up by temporary
storage in one disk file, from which each file is printed out in sequence. This operation is called "Spooling"
(Simultaneous Peripheral Operations On-Line).
If a terminal is a PC or a PC-XT, it may have a local printer connected to it.
The system unit may be connected to user terminals located at remote sites or to remote system units through
"Communications lines". Thus, a terminal of one multi-user system can communicate with any other multi-user
system for sharing a database or sending important information.
Just as a PC has serial ports to connect to a serial printer, so also multi-user systems will have special
communication ports to link one system with another.

1.4. Classes of Multi-User Systems.


Multi-user systems are classified into two broad categories:

Mainframes

Key To Unix

Page 2 of 57

Minicomputers

1.5. Multi-User Operating System - UNIX


Multi-user operating system is expected to perform various functions, which can be broadly categorized as:
Command Interpretation - involves accepting and getting executed user Instructions one by one.
Peripheral Management - deals with handling all the input-output operations performed by the peripheral devices
of the system.
Memory Management - is the extremely important job of allocating memory for various jobs being performed on
the system and even disposing of useless data in memory after it has been processed and used.
Process Management - is important if several programs have to run concurrently in the memory. In that case, the
OS has to ensure that all programs are rescheduled properly and no one program gets more than its fair share of
CPU time.
Multi-user operating systems are expected to handle multiple users as well as multiple peripheral devices
concurrently. Such operating systems have to be more efficient and sophisticated than simpler single-user operating
systems.
MS-DOS was designed for the PC with the assumption of a single end user. It was intended to be functional and
friendly. UNIX was designed for a multi-user system. It was intended to be an environment for programmers and was
therefore by design made powerful and terse. While programmers find UNIX very friendly, end-users find UNIX
commands very cryptic and unfriendly.

1.6. STALLMAN AND TORVALDS: GNU AND LINUX


Unix has finally turned commercial, but then two gentlemen, Richard Stallman and Linus Torvalds has different ideas.
Both of them strongly felt that Unix was so good that it must be given any free. Stallman is the founder and prime
mover of the Free Software Foundation (formerly known as GNU, a recursive acronym which stands for GNUs Not
Unix!). GNU has practically every Unix utility of this own, but is best known for the emacs editor, the gzip compression
utility and the gcc C compiler. It also has xFree86, its own X Windows system that is used by practically all
implementations of Linux. Many of the GNU tools have been ported to the major flavours of Unix.
Linus Torvalds is the father of Linux, the free UNIX that is now making significant inroads into the commercial world.
It came into being in 1991 when Torvalds, then a student at the University of Helsinki (Finland), made the entire
software available on the Internet. This offering also included the complete source code that encouraged hundreds of
programmers to add to it and make changes. Linux is distributed under the GNU General Public License, which
makes it mandatory for developers and sellers to make the source code public. The license also lets you buy one
copy of Linux and load it on a thousand machines (or more). Linux is particularly strong in networking and Internet
features, and is an extremely cost-effective solution in setting up a web site or a local intranet.

1.7. History of UNIX


UNIX is the most popular operating system on multi-user systems. This operating system originated as a single-user
system. It started off on a cast-off DEC PDP-7 at Bell laboratories in 1969. Ken Thompson, with ideas and help from
Dennis Ritchie, and others, wrote a small, general-purpose operating system, which included utilities to manage files
and processes on the PDP-7, among other utilities. The system developed was so good that it attracted a large
number of enthusiastic users and eventually enough credibility for transferring the system on to a larger machine, the
PDP-11/20.
In 1973, Thompson and Ritche rewrote the UNIX operating system in C, breaking away from the traditional system
software language, Assembly.
Around 1974, it was licensed to universities for educational purposes and a few years later UNIX was made
commercially available.
It is important to note that MS-DOS was created much after UNIX, by which time the industry had begun to accept
UNIX as the standard operating system. UNIX features have therefore influenced the design of MS-DOS.
All multi-user systems do not operate on UNIX, by which time the industry had begun to accept UNIX as the standard
operating system. UNIX features have therefore influenced the design of MS-DOS.
All multi-user systems do not operate on UNIX. Examples of other multi-user operating systems are:
- VMS (Virtual Memory System) for VAX machines.

Key To Unix

Page 3 of 57

- MVS for IBM mainframes..etc.

1.8. Features and Benefits of UNIX


The UNIX operating system is available on machines with wide range of computing power, from microprocessors to
mainframes, and on machines across different manufacturers product lines. No other operating system can make
this claim. The popularity and success of this is due to the following reasons.

Portability
The system is written in a high level language making it easier to read, understand, change, and move to
other machines. Customers can now choose from over a wide variety of hardware vendors without being
locked in with a particular vendor.

Machine Independent
The system hides the machine architecture from the user, making it easier to write applications that can run
or micros, minis, and mainframes.

Multi-user operations
UNIX is a multi-user system designed to support a group of users simultaneously. The system allows for the
sharing of process power and peripheral resources while at the same time providing excellent security
features.

Hierarchical file system


UNIX uses a hierarchical structure to store information. This structure has the maximum flexibility in
grouping information in a way that reflects its natural state. It allows for easy maintenance and efficient
implementation.

Unix shell
UNIX has a simple user interface called the shell that has the power to provide the services that the user
wants. It protects the user from having to know the intricate hardware details.

Pipes and filters


UNIX has facilities called pipes and filters, which permit the user to create complex programs from simpler
programs.

Utilities
UNIX has over 200 utility programs for various functions. These utilities form a powerful set of tools to cater
to user requirements without the necessity of writing new programs.

Background Processing
UNIX has facility which the user can start a task and then proceed to work on other tasks while the system
runs the first task in the background and the second task in the foreground. Background processing helps
the user in effective utilization of time.

Software Development Tools


UNIX offers an excellent variety of tools for software development for all phases, from program editing to
maintenance of software.

Maturity
UNIX is a time-tested operating system. It offers a bug free environment and high level of reliability.

The UNIX SYSTEM supports a wide variety of languages - C, Fortan, Basic, PALCAL, Ada, COBOL, Lisp and Prolog.

1.9. Versions of UNIX


One of the major attractions of UNIX has always been the capability to shape it to meet a variety of needs. This
flexibility led to a proliferation of different versions of UNIX. In addition to several versions of UNIX from AT&T,
hardware and software venders have adapted UNIX from microcomputers to mainframes.

Some of the popular versions of UNIX are:


Version

Developed

Key To Unix

Page 4 of 57

AT & T

UNIX System
UNIX BSD(4.xx)

Berkeley Software Distribution of


Berkeley University
Microsoft for micros
DEC for minis

XENIX
ULTRIX
AIX

IBM

1.10. THE KERNEL AND SHELL


The entire UNIX system is supported by a handful of essentially simple concepts. Foremost amongst is kernel and
shell
The kernel is the heart of the system- the core of the UNIX system is the Kernel- the operating system program. The
kernel controls the computers resources, allotting them to different users and to different users, decides process
priorities and performs all the tasks, which the user wouldnt like to bother about. It interacts directly with the system
hardware. There is only one kernel for any system. Kernel, in traditional parlance, is often called the operating
system.

The Structure Of The Unix System


The shell, on the other hand, is technically a UNIX command. It is the interpreter of user request. Computers dont
have inherent capability of translating commands into action. An interpreter is required, and that job in the UNIX
system is handled by the shell. It takes the commands from the user, deciphers it, and by exchanging information,
and sends that command to the kernel. It is actually interface between user and kernel. It has a programming
capability of its own.

1.11. Security for the Unix User


The UNIX system can be used only by those persons who maintain an account with the computer. This list of
accounts is maintained separately in the computer. You cant simply sit down at any terminal and start banging away
if your name doesnt feature in the list. That would be both inadvisable and an impossible thing to do.
When the system prompts you with Login: message, it expects a response from you. Curiously enough, you can
say that the Login message indicates that the previous user is logged out, i.e. has terminated his session. You
are now expected to enter a special user name or account, which has been allotted to you by your system

Key To Unix

Page 5 of 57

administrator. This user account is maintained by the system to check for illegal responses. So, you have to ask your
administrator for your account name.
NOTE:The system never indicates what went wrong- the login name or the password.
This procedure followed by users to start a UNIX session- the login procedure-has been explained earlier. The login
procedure ensures that only authorized users can try to access the system.
However, it is easy for anyone to find out a users login name since this is displayed on screen when being entered.
Thus the login procedure is not a foolproof system.
To overcome this problem, UNIX allows a user an additional measure of security by allowing him to have a password
associated with his name.
To login using password-protected user names, the user has to enter not only the user name but also the password.
UNIX to see if they are authorized entries checks the combination of the two. The $ prompt appears only if the user
has entered both the name and password correctly, otherwise he is asked to re-enter the user name followed by the
password. Passwords are not displayed on the screen while they are being entered. Sample login sequence is show
below:

(Password is entered correctly)

(Password is entered incorrectly)


Note that the user name and password are checked only after both have been entered.

1.12. Creating a Password


A user can add a password for has login name by the passwd command. Shown below the passwd command
executed by the user Gopal:
$ passwd <RET>
Changing password for gopal
(user enters the desired password)
New password:
(to double-check the new password)
Re-enter new password:
The user is asked to re-enter the new password for confirmation. If this entry differs form the earlier entry, and then
UNIX does not accept the password.
If the user entries match, then the password is associated with the users name. Subsequent attempts to login will
require that the user enter his password

1.13. Rules for Passwords


A user password in UNIX

Cannot be less than 6 and more than 14 characters long


Can contain any character in the keyboard character set
Must contain at least 2 alphabetic characters and at least 1 numeric or special character.

It is desirable to have slightly complex passwords so that they are not easily deciphered. The use of simple
passwords has in fact become a major cause for concern in the U. S. A, where there have been many incidents of

Key To Unix

Page 6 of 57

unauthorized access to a UNIX system simple because users used very simple and obvious passwords such as their
own names, or birth dates or age.
However, passwords should not be so complex that they cannot be remembered.

1.14. Changing the Password.


A user can also change his password with the passwd command. The steps followed by the user Gopal to change
his password are depicted below:
$passwd <RET>
Changing password for Gopal
Old password:
New password:
Re-enter new password:
$_
The passwd command asks for the old password to ensure that the authorized user (and not just anyone) is trying to
change the password.
In case UNIX does not recognize the old password, it displays a sorry message and the $ prompt appears on the screen.

$passwd <RET>
Changing password for gopal
Old password:
Sorry.
$
If the old password is entered correctly, then the user is asked to enter the new password and re-enter it to confirm. If
the two entries do not match, then the user is given another chance to enter the new password.
$passwd
<RET>
Old password:
New password:
Re-enter new password:
They do not match, try again.
New password:
Re-enter new password:
$
When changing the password, the new password should differ from the old password by at least 3 positions.

1.15. Check your current Directory the pwd command


COMMAND: pwd (present working directory)
FUNCTION: The pwd command gives the absolute path name of the users current directory place in the
current place in the structure.
Ex:
$pwd /usr/user1$_
Here, /usr/user1 is the directory in which the user user1 is presently working.

Key To Unix

Page 7 of 57

2. The UNIX File System


2.1. The FILE in UNIX
A Unix file a storehouse of information- it is simply a sequence of characters. UNIX places no restriction on the
structure of a file, and you dont need to assume a predefined structure to work with it. A file contains exactly those
bytes that you put into it, whether it represents a source program, other text or executable code. Unix file does not
contain the end-0f-file mark.
In UNIX, all information is treated as a file. So, besides a users program files and data files, there are also special
files such as files that contain information about directory contents or files that store information about various inputoutput devices connected to the system. In UNIX, devices is also treated as a file, i.e., all information going to, say
the VDU, is treated as if it were being set to a file.
a. Ordinary files - All files created by a user come under this category of files. These include all data files, files
containing programs, complied and executable program files. A user can make changes to such files.
b. Directory files - A directory is storehouse of information about the file and subdirectories in that directory. For
each directory there is file, by the same name as the directory, which contains information about files under that
directory. For example, for the directory /user/gopal, there will be a directory file called gopal in the directory /user,
which contains certain information on all the files and directories under the directory gopal.
c. Device files - Most of the system files in UNIX are special files. The device file is special in the sense that any
output directed to it will be reflected onto the respective I/O (input/output) device associated with the filename.
Special files are typically associated with input-output devices. Such files are found in the standard UNIX directories
such as /dev and /etc. Users cannot alter special files.

2.2. THE STRUCTURE OF THE FILE SYSTEM


All files in UNIX are related to one another. The file system in UNIX is a collection of all these related files (ordinary,
directory and device files) organized in a hierarchical (an inverted tree) structured
Root(/)

bin

dev

etc

mnt

bin

tmp

unix

kumar

lib

var

sharma u1001p

The UNIX file system


The directories shown directly under root are normally found in very UNIX system, in-addition to others
Root is actually a directory file, and it has a number of sub-directories (or branches) under it. These subdirectories, in turn, have more sub-directories and other files (leaves) under them. For instance, bin and usr are two
directories directly under root, while a second bin and kumar are sub-directories under UNIX.

Key To Unix

Page 8 of 57

NOTE: -

It is possible for two directories to have the same name. Every file, apart from root, must
have a parent, and it should be possible to trace the ultimate parentage of a file to root. It is
not possible that the two directories or files have the same name in one level.

2.3. Displaying the file Type


File-names and directory names are displayed using the ls command.
For example:
$pwd

<RET>

/user/asha
$ls <RET>
Z.c
alpha-doc
asha1
asha2
tempdir
x.c
$
From this listing, it is impossible to tell whether asha1 is a directory file or an ordinary file or a special file; whether
tempdir is actually a directory file as its name indicates.
UNIX provides a way of doing so by using the l option of ls command this is shown below:
$ pwd <RET>
/user/asha
$ ls l <RET>
total 6
This ls l command shows a long listing for files in the current working directory with:

The file-names on the extreme right.


The file type as the first character on the extreme left.
a hyphen in this position indicates an ordinary file
d in this position indicates a directory file
Special files will be indicated by either b,c or p in this position. These indicate different
categories of special files
a total maintained by the system.

2.4. Creating Directories


COMMAND: mkdir (make directory)
FUNCTION: It creates a new directory specified as argument, under the current directory.
Ex:

$pwd
/usr/user1
$mkdir dir1
$_
$pwd
$/usr/user1
$_

Key To Unix

Page 9 of 57

The new directory dir1 is created under the current directory called user1. Creating a directory does not change the
users position in the tree structure.

2.5.Changing Directories
You can move around the file system by using the CD (Change Directory) command.
COMMAND: cd (change directory)
FUNCTION: The command changes the directory to the directory specified as argument.
Ex:

$pwd
/usr/user1
$cd dir1
$ pwd
/usr/user1/dir1
$_

Here, the cd command changes the directory /user/user1 in to the sub-directory called dir1.The cd command used
without arguments gets the user back to his HOME directory from wherever he is. To get back to the parent directory,
the special argument . . (dot-dot) is used with cd. The symbol . always means the parent of the current directory .

2.6. The ls command


COMMAND: ls
FUNCTION: Lists names of files in the current directory. You can list out the files and subdirectories in different
directories by specifying the absolute path after ls command.
Ex: $ls
ls stands for lists . It lists the names of files and sub-directories in the current directory. It has number of options,
which can be given as arguments. It also sorts the names of the files alphabetically by default.

Some Options with ls:


Options

Description

-a

Listing all matching entries including hidden files

-l

Provides a long listing showing seven attributes of a


file.

-r

Sorts files in reverse order.

-R

Recursive listing of all files in sub-directories.

-s

Displays the number of blocks used by a file.

-t

Sorts files by modification time.

-d

Displays information about a particular directory.

-F

It differentiates a file and directories by appending a


/ at the end of directory names

-i

It displays the inode number of a file. (inode number

Key To Unix

Page 10 of 57

is the unique number given to a file)


-c

Sorts by time of change of the I-node

2.7. Listing the contents of the file:


COMMAND: cat (concatenate files)
FUNCTION

: Displays the contents of one or more files.

SYNTAX

: cat <filename>

Ex: $cat file1

A sample file.
Good bye.
$_
Here, the contents of the file file1 are displayed on the screen using cat command. You can concatenate two files
using cat command
$cat file1 file2 >file3
$_

Here, the contents of file1 and file2 are displayed and they are redirected to the file3. File3 contains the contents of
file1 and file2. If file3 is already exists it simply over writes the file.
Using cat command you can create file
$cat>file4

Here `cat command takes the input from the keyboard and redirects the output to the file4. After entering the text you
have to press `ctr+d` to save and come out of that file.
NOTE: The [Control-d] OPTION is used to terminate all input not only with cat, but with several other commands as
well.

2.8. Removing Directories


COMMAND: rmdir (remove directory)
FUNCTION: This command removes the empty directory.
Ex: $ rmdir dir1
Here, dir1 is the directory under the current working directory. It should not be the current working directory. rmdir
command removes the directory only if the directory is empty.
NOTE:-A sub-directory cannot be removed with rmdir unless it is empty and one is positioned in its parent
directory.

2.9. Relative PathNames


Suppose you have just changed your directory to /unix2/u1002/abc. To switch to your home directory, you can always
use the absolute pathname:
$pwd
/unix2/u1002a/abc

Key To Unix

Page 11 of 57

$cd /usr/kumar
$ pwd
/usr/kumar

$
Every absolute pathname must trace its origin to root, as discussed earlier in this chapter. The first /indicated root
directory, while subsequent ones act as delimiters to sub-directories. This method can be inconvenient and tedious,
especially when the absolute pathname is quite long i.e., when you located a number of generations away from
root. If you have a long chain of sub-directories, then the use of an absolute pathname is an unsatisfactory solution
to the problem of moving a single generation up the hierarchy.
UNIX also permits use of a relative pathname, which locates a filename with respect to the current directory. In ls
command you discovered two hidden directories--. and.. Indicating the current and parent directory, respectively.
Using. . As an argument to cd, you can issue an instruction to move to the parent directory /usr/kumar/dir1/data/text,
i.e. the directory /usr/kumar/dir1/data:
$ pwd
/usr/kumar/dir1/data/text
$cd ..
/usr/kumar/dir1/data
$_
This method is compact and more useful when ascending the hierarchy.

2.10. Copying files:


COMMAND

: cp (copy)

FUNCTION

: This command duplicates the file.

SYNTAX

: cp <source file> <target file >

Ex

: $ cp file1 file2

The cp command copies the source file to the target file if the target file is already exist in that directory.
Ex: cp i file1 file2
Here i, option allows you to copy the files interactively. If file2 exist than it will prompt you to over write the file.

Note: using cp command you cannot copy multiple files to a single file.

2.11. Moving or Renaming files:


COMMAND: mv
FUNCTION: Renames or Moves a file/directory.
SYNTAX

: mv

<source file> <target file >

Ex: $ mv file1 file2


Here, file1 is renamed with file2
In UNIX, a file can also be moved to another directory as shown below. Like cp, mv also overwrites the file, if the
target file is exist.
Ex:

$ mv file1 /usr/user1/dir1

Key To Unix

Page 12 of 57

2.12. Removing Files:


COMMAND: rm (remove)
FUNCTION: Removes the files from current directory.
SYNTAX: rm <filename
Ex:

>

$ rm file1
$_

The rm command removes files from current directory. It can remove more than one file on a single command line.
There are three options with rm :
Options

Significance

-I

Interactive rm prompts for confirmation before


each file is removed.

-f

Forcibly removes files


permission on them.

-r

Recursive rm searches for files in sub-directories


also.

that

dont

have

write

2.13. File Security


Password security protects a system from access by an unauthorized person. In a multi-user system, each
authorized user can have access to files that belong to other users or to the system files and utilities. In such an
environment, it becomes essential that files should be protected so that another user does not misuse one users files
and system files are not detected or modified.
UNIX allows users to protect their own files from other users working on the system. It also provides protection for
system files.
Before an explanation of the method of protecting their own files from other users working on the system. It also
provides protection for system files.
Before an explanation of the method of protecting files, the following concepts will be discussed:

The types of files in UNX


The concept of File Owner, Group Owner and Other Users.
Files access permissions.

2.14. The File Owner, Group Owner and Other Users


File Owner
The user who creates a file is said to be the owner of that file. The owner of a file can perform any operation
on that file, e.g. copying, deleting, and editing (through some editor) etc.

Group Owner
Consider the following situation:
A project team of 5 people of the SMARTEST CONSULTANTS PVT. LTD. is working on a software development
project for a private detective agency. The team is headed by an analyst. The other 4 members are all programmers.
The team is working on a UNIX system. Each programmer has been given a few programs to develop. The data,
provided by the detective agency, is of a highly confidential nature, and so the data file has been created in the
analysts HOME directory.
One programmer may have to link his program to another programmers program in order to test his program.

Key To Unix

Page 13 of 57

In this situation, each programmer is the File Owner of his own program files. Each program, however, would also
belong to the other programmers, so that they could use the programs for linking. The project team (of 5 users) is
said to be the Group Owner for the file.
In UNIX, it is possible to define which user of the system is member of a group. A group of users are also given a
name, just as a user is given a name.
So, for the project team, the programmer Anita may be the owner of her program files. The group of users in the
project team may be called PROJECT-ABC, which becomes the Group Owner of Anitas program files.

Other Users
In the example of the SMARTEST CONSULTANTS PVT. LTD., all users of the system who are not members of the
group PROJECT-ABC are referred to as Other Users for all files that belong to that group. Their Other Users are
thus those users who do not have any common needs for data or programs with the Group Owner. For e.g. people of
the accounts department who use the system to generate reports, or data entry operators entering data for payroll
can be referred to as Other Users for the files that belong to the group PROJECT-ABC.
To summarize:
Associated with each file in UNIX is:
-

a File Owner
a Group Owner which consists of users who need to share that file
Other users who do not belong to the group.

UNIX stores this information for each type of file on disk.

2.15. FILE SECURITY THROUGH FILE ACCESS PERMISIONS (FAP)


UNIX follows three-tired file protection system, which determines the access rights that you have for a file.
Each tier represents a category, and consists of rs,ws and xs to represent three types of permissions.
UNIX allows the owner of a file to define how the file can be accessed by:
-

The file owner himself


The group owner of the file
Other users

The file Owner can define the following kinds of permissions for each of the above user categories:
a) Read access

If a user has read access permission for a file then he can display the
file, copy it compile it etc.

b) Write access

If a user has write access for a file then he can write on to it, change its
contents or delete it.

c) Execute access

The user can execute the file if he is allowed execute access.

The permissions of each file are stored as follows:


1.
2.
3.

The permissions are given in the first column from the second position
The first three characters show the read, write and execute permissions for the file owner the next
three are the permissions of the Group owner and the last three are the permissions of the Other
users.
If a permission is allowed to a user, then r (read), w (write) or x (execute) is displayed otherwise a
hyphen appears in the place.

Directory Permissions
for groups and others

Cd

r--

-w-

ls

Cp

cp

Into

from

Key To Unix

Page 14 of 57

--x

rw-

r-x

-wx

rwx

The FAP for a directory is interpreted in the following way:


No read permission

Does not allow the user :


To list the contents
Remove the directory using rm r.

No write permission

Does not allow the user :


Copy the files to the directory.
Remove the files in the directory.
Rename files in the directory.
Make a sub-directory.
Remove a sub-directory from the directory.
Move files to and from the directory.

No execute permission

Does not allow the user:


Display the contents of directory file
from within the directory
Change to that directory.
Display a file in that directory.
Copy a file to or from a directory.

2.16. Changing the FAP for the File Owner, Group Owner and other users:
To control the access permissions for a file for different users, the File Owner can use chmod command.
The following example shows how the file access permissions for the file file1 can be changed. The permissions for
this file are shown below:
-rwxrwxrwx 1 vara unix2 20

jan 31

10:40

file1

To change permissions for all the three categories of users, the file owner has to indicate:
-

Which permission is to be changed?


Whether the permission is to be given or removed.
The file for which the permissions are to be changed.

SYNTAX: $chmod

<permissions>

filename

For example: The file Owner can remove the write permission for all users for the file file1 by the following command:
$chmod

-w filename

<RET>

Key To Unix

Page 15 of 57

$_
The FAP for the file file1 will now appear as follows:
-r-xr-xr-x 1 vara unix2 20 jan 31

10:40

file1

Note that in place of all Ws, hyphens have appeared.


To add back the write permission, the File Owner can give the following command:
$chmod

+w

file1

<RET>

$_
More than one-file access permissions can be changed in one command (instead of using two or more commands).
For example:

$chmod

+wx

file1 <RET>

$_
This is done by specifying one of the following before the + or sign in the command.
u

for changing FAP for File Owner only


example : chmod u+r x.c

for changing FAP for Group Owner only


Example : chmod g-r x.c

for changing FAP for all Other Users only


Example: chmod o+x x.c

Key To Unix

Page 16 of 57

3. Unix Utilities
3.1. The wc Command
The wc filter counts the number of lines, words and characters in the specified file or files. It is capable of accepting
input directly from the keyboard. By entering wc without any arguments, it waits for the user to type in the input. By
default, wc prints all the three counts lines, words, bytes. It comes with three options, which allow user to obtain the
number of lines, words or characters individually or in any desired combination.

Options

SYNTAX : wc

Ex:

[options]

Significance

-c

Counts the number of characters in the given file

-w

Counts the number of words in the given file

-l

Counts the number of lines in the given file.

<filename>

Assume the file file1


$cat

file1

Raju
Rani
Vara
Sai
$ wc

file1
4

19 file1

In the above example, it displays the number of lines, words and number of characters along with the filename.

Ex:

$ wc

-l

file1

file1

$_
Ex:

$ wc

-c
16

file1
file1

$_

3.2. The pg Filter:


This Filter is used to display a large file, one screen full at a time. The format of the pg command is :
SYNTAX: $pg filename
Ex: $pg file1

Key To Unix

Page 17 of 57

3.3. The more Filters:


While viewing large files, you often find the output scrolling off your screen. This sometimes happens so fast that,
before you hit [control-s] to stop it, quite a bit of the output would have scrolled off. Using [Control-s] and [Control-q]
alternately is certainly not a comfortable method of halting the output. It also doesnt let you see what has already
been seen before. UNIX offers the more command as a paging tool, so that you can view one page at a time.
The Syntax of the more command is as follows:
$more <filename>
Here, more allows the user to view a file, one screen at a time.

3.4. Pipes
UNIX has a feature by which filters and other commands can be combined in such a way that the standard output of
one filter/command can be sent as standard input to other filter/command. The vertical bar (|) is the pipe character.
Let us now see some examples of piping:
1)

$ls | wc

Here, the output of ls command becomes the input to wc which promptly counts the number of lines it receives as
input and displays this count on the screen.
1)

$ who | sort
Here, instead of displaying the output of who to the screen it is piped to sort. Sort sorts whatever it receives as
input and displays the sorted output.

3.5. The man command displays pages from the UNIX reference manual
For Example, if the user wants detailed information about the ls command, the command to be given is:
$man ls
NAME
: Command/file name, along with a brief description
SYNOPSIS
: How to use the command, it contains the optional and non-optional arguments.
DESCRIPTION : An explanation of how to use the command, and also an explanation of each option.
EXAMPLE
: gives example of how to use a command.
FILES
: files which have to be available for this command to work
SEE ALSO
: commands similar in purpose
DIAGNOSTICS : Explanation of error messages
WARNINGS
: Things to be careful about when using the command
BUGS
: Known problems and suggested improvements.

3.6. Comparing two FILES The CMP Command


Frequently, you may require knowing whether two files are identical in all respects so that one of them can be
deleted. The cmp (compare) command is used to achieve this task. When used without options it uses two
filenames as arguments, and displays the differences on the terminal:
$cmp file1 file2
file1 file2 differ: char 9, line1
The two files are compared byte by byte, and the location of the first mismatch is echoed to the screen. If two file are
identical then cmp displays no message, but simply returns the $ prompt follows the UNIX tradition of quiet
behaviour.
The cmp l option gives a detailed list of the byte number and the differing bytes in octal for each character that
differs in both the files.

3.7. The comm-command further comparison


Comm. Compares two sorted files and compares each line of the first file with its corresponding line in the second. It
displays a three-columnar output. The first column contains lines unique to the first file, while the second column
shows the lines unique to the second file. The third column displays lines common to the both files. Try using it on
the two files file1 and file2, but have a look at their contents first:

Key To Unix

Page 18 of 57

$ cat file1
c. s .das
singhvi chanchal
sumitha roy
Krishna murthy
Satish kumar
$$cat file 2
barun gupta
c. s. das
anil kumar
smitha chowdary
sumitha roy
$$ comm. File1 file2

anil kumar
barun gupta
Krishna murthy

c.s. das

Satish kumar
Singhvi chanchal
Sunitha chowdary

sunitha roy

3.8. The diff command


Diff is the third command, which can be used to display file differences. But like its fellow mechanism cmp and
comm., it also tells you which lines in one file have to be changed to make the two files identical. It has a similar
syntax and has a few options. When used with the same files, it produces a detailed output.
Diff uses certain special symbols to indicate the changes that are required to make two files identical.

3.9. The banner command


The banner command creates posters by blowing up its argument on the screen. On each line it can display a
maximum of ten characters. If you are to display the word UNIX on the screen, then this is how you can use this
command:
$banner UNIX
This command is placed in /usr/games directory.

3.10. The calendar command


The cal command is quite useful in printing the calendar of any particular month or the year. Any calendar from the
year 1 to 9999 can be displayed with this command:
$ cal 2001
This command displays the calendar of the year 2001.
$ cal 01 2001
This command displays the calendar for the particular month of January 2001

Key To Unix

Page 19 of 57

The cal command is accurate, and also takes into account the leap year adjustments that took place in the year
1752.

3.11. Displaying the SYSTEM DATE with Date Command


The UNIX system maintains an internal clock, which is meant to run perpetually. This is possible because, when the
system is shut down, a battery backup keeps the clock ticking. This clock actually stores the number of seconds
elapsed since January1, 1970. It is used to time stamp a file. You can simply display the current date with the date
command, which shows the date and time to the nearest second.
$date
Tue sep 19 10:13:52 BST 1995
You can use different options with date command to display the date in different formats.
Date +%m

Displays only the month of year

Date+%h

Displays only the month in character

Date+%y

Displays only the year

3. 12. The who command


UNIX maintains an account of all the current users of the system. It is a good idea to know the people working on the
various terminals so that you can send them messages directly. A list of them is displayed by the who command,
which, by default, produces a three-columnar output :
$who
root

console

jan

30

10:32

kumar

tty01

jan

30

14:09

tiwary

tty02

jan

30

14:15

Sharma tty05

jan

30

13:17

-H option with who command displays the heading for each column
The who command, when used with the arguments am I, displays a single line of output only, i.e., the login
details pertaining to the user who invoked this command:
$ who am I
kumar

tty01

jan 30

14:09

3.13. Wild-Card Matching


Unix, offers the facility to perform an operation on a set of files without having to list out the names of all the files on
which the operation has to be performed.
This is made possible by the use of certain Special characters in the command in the place of the actual filenames.
UNIX interprets these special characters as a specific pattern of characters. UNIX then compares all filenames under
the directory specified in the command to find out which filenames match that pattern. The command is executed on
files whose names match that pattern.
Wild-Card

Significance

Matches one or more character or a string of more


than one character.

Matches exactly one character.

Key To Unix

Page 20 of 57

[]

Matches exactly one of a specified set of characters.

3.14. The * Wild-Card:


This Wild Card is interpreted as a string of one or more characters (or no characters). It can be used with other
characters. It can also be repeated in the command line.
Ex: $ls file*
file1

file2

file3

file4

fileabc

file

$_
When * (asterisk) is appended to the string file, the pattern file* expands in to all files, in which the first four constitute
the string file and after this four characters it can be anything or nothing i.e. the above command lists all files in the
current directory as shown above.
The * Wild-Card can also be repeated as shown below:

Ex: Assume that files, file1.txt, file2.txt, file3.abc already exists and
gives the command
$ls

file*.*

file1.txt

file2.txt

file3.abc

$_
This displays the files starting with file and containing any sequence of characters or no characters followed by a .
(dot) and then followed by any sequence of characters or no characters .

3.15. The ? Wild-Card:


The ? Wild Card matches exactly one occurrence of any character. It can be used with other Wild Cards. It can be in
the command line.
Ex: Assume that the files a, b, c, file1, file2, file1.txt, file2.c already exists in the current directory.
1) $ls ?
a

$_
The ? matches exactly any one character. Here all the files whose names are of single character are
displayed.
2) $ls file?
file1

file2

$_

3)$ls *.???
file1.txt
$_
4) $ls file2.?
file2.c
$_
In the second example, it is listing all files with a pattern file and followed by any single character i.e. file1 and file2. In
the third example, it is displaying for any file with a. (dot) extension of three characters. In fourth example, it displays
the files with pattern file2 and with a . extension of a single character.

Key To Unix

Page 21 of 57

3.16. The [ ] Wild-Card


This [ ] Wild-Card matches any one of a specified set of characters which are given within the brackets. It can be
used with other Wild Cards.
A single character expression, taking the values 1 or 2 or 3 can be represented by the expression [123]. This can be
combined with any string or other Wild-Card expression.

Ex: Assume that the files a, b, c, d, file1, file2, file3 exists in the
current directory.
1) $ls

[abc]
a

$_
It displays the files with single characters of either a or b or c.
2) $ls file[13]
file1

file3

$_
All the above Wild-Cards can be combined with each other.
Ex: 1)$ls *.???
It will display files with pattern anything with the . (dot) extension of three characters.
2)$ls [a-z]*
It displays all files starting with a to z and anything or nothing afterwards.
3)$ls [!a-c]*
It displays all the files which do not start with a or b or c.

3.17. Redirection
Most UNIX commands perform one simple function, which consists of the following steps :
- They take some data as input from some source
- They process the data
- They produce the results as output and send it to some destination
In UNIX, when a user executes a command, the shell runs the command and assigns to the command:
- The terminal keyboard as the source of input
- The terminal VDU as the destination of output

In UNIX keyboard and VDU are referred by special names - the keyboard is the Standard input file and
the VDU is the Standard output file.
Consider the cat command. When followed by the file name, this command displays line by line from that
file, tills the end of the file. Without the file name it takes its input from the standard input file and writes its
output to the standard output file.
The cat command waits for the input from the keyboard. When the user presses the <RET> key after
entering a line, the cat command does what it is supposed to do - simply displays the entire line on the
screen.
The user can keep on inputting lines. To indicate the end of input, the user has to press <Ctrl> d
Not all commands use the Standard input file for input and the Standard output file for output. Given
below

Key To Unix

Page 22 of 57

are examples of the commands that use one or neither of these files.
a. Command that uses neither the Standard input file nor the Standard output file.
E.g. cd - does not except input from the keyboard
- does not write anything onto the VDU.
b. Command that uses the Standard output file but not the Standard input file.
E.g. pwd - does not expect input from the keyboard
- displays its output ( the name of the current working directory) on the VDU.

3.18. Standard Error


When an invalid command (e.g. a command entered incorrectly or with invalid options) is given, UNIX displays
appropriate error messages on the VDU. The VDU is thus also the Standard error file.
file descriptor

Device Files

Redirection operator

Stdin

<

Stdout

>

Stderr

>

Ex:
$ls>abc
Redirects the output of ls command to abc file
$ls -l 2> abc

Redirects the error messages of the command to abc file


$ls -l > abc 2> err
Redirects the output if any to abc and the error messages of the command to err file.

3.19. The two special files- /dev/null and /dev/tty


Quite often, you may like to test whether a program runs successfully without seeing the output or the error
messages. You make not like to save them in files either. You have a special file which simple accepts any stream
without growing in size; it is the file /dev/null.
$ cal 1995 >/dev/null
$ cat /dev/null
$_
The device file /dev/null simple incinerates all output directed towards it. This facility is useful in redirecting error
messages away from the terminal so that they dont appear on the screen. /dev/null is actually a pseudo-device
because, unlike all other device files, it is not associated with any physical device.
The second special file that you can hope to find in the UNIX system is the one indicating ones terminal, /dev/tty.
Consider, for instance, that kumar is working on the terminal /dev/tty01, and Sharma on /dev/tty02. But both kumar
and Sharma can refer to their own terminals as /dev/tty. Thus, if kumar issues the command
$who >/dev/tty
The list of current users is sent to the terminal kumar is currently using i.e. /dev/tty01. Similarly, Sharma can use an
identical command to see the output on this terminal.
NOTE: THE SIZE OF /dev/null is ALWAYS ZERO

Key To Unix

Page 23 of 57

4. Simple Filters
4.0. Filter:
A filter is a command or a user program that takes its input from standard input, filters the input and sends output to
the standard output. These are file-related commands.

4.1. The Sort Filter:


The sort filter arranges each line of input in ASCII order. As the name suggests the sort command can be used for
sorting the contents of a file. While sorting, the sort command bases its comparisons on the first character on each
line in the file. If the first character of two lines is same then the second character in each line is compared and so on.
That is it sorts the spaces and the tabs first, then the punctuation marks followed by numbers, uppercase letters and
lowercase letters.
SYNTAX: sort [options] <filename>
Ex: when the sort command is invoked without options, it sorts the entire line. Consider the following
example:

$sort

<RET>

Anitha
Swapna
Madhu
<ctrl> d
Anitha
Madhu
Swapna
$_
The user enters names, each on one line. When the user indicates end of input by pressing <ctrl>d, sort compares
the corresponding characters of each line and displays the lines in proper sequence.
The following are the options with sort:
Options

Significance

-t

Specifies the field separator

-n

Sorts in numeric order

-r

Sorts in reverse order.

-f

Ignores case while sorting.

+n[-m]

Skips n fields before sorting and


then sorts through field m.
Sorts in dictionary order.

-d
Stores
default

output
is to

in
file.
The
send output to

Key To Unix

Page 24 of 57

-o file

standard output.

4.2. Options of the sort Filter:


1) The t option:
Sort assumes that a space or a tab character separates fields. In this case, the -t option is used before filename to
specify the field separator. For example, if the filenames contains fields separated by a hyphen (-) and the file is to
be sorted on the third field, then the following command would be used:
$ sort

-t -

+2 -3

file1

Note that the field separator is enclosed in double-quotes.


2) The -n option:
Since sort arranges its input in ASCII sequence, numbers would also be arranged so. To indicate to sort that the
input is to be sorted according to numeric value of the numbers and not in ASCII sequence, the n option must be
given in the command.
Ex:

$sort

-n

<RET>

6
5
8
2
9
<ctrl>d
2
5
6
8
9
3) The r option:
This changes the output of sort to reverse order. Consider the following example:
Ex:

$sort

-r <RET>

Anitha
Laitha
Madhu
<ctrl>d
Madhu
Lalitha
Anitha

$_
4) The -f option:
In case the input is to sort consists of digits and alphabets and other characters, sort arranges data in ASCII
sequence. In the ASCII sequence of characters, A-Z has lower values than the corresponding lowercase alphabets.
So, sort would place anitha after Anitha since a has higher ASCII value than A. The f option folds all lowercase

Key To Unix

Page 25 of 57

characters into uppercase characters, i.e. it considers all lowercase characters as uppercase characters without
actually changing them.
Ex:

$sort

-f

<RET>

Anitha
asha
rama
Rama
Prasad
<ctrl>d
asha
Anitha
Prasad
rama
Rama
$_

5) The +pos1

-pos2 option:

By default sort assumes that fields are separated by a blank space or a tab character. To sort a file on one field, the
position of the field in the file has to be specified. This is done as follows:

The number of field separators that have to be ignored to reach that field must be specified as +pos1
where the pos1 the appropriate number.
The field separator at which sort is to stop must be specified as pos2 where the pos2 is the appropriate
number.

For example, to specify sorting on third field, the user will have to specify +2 and -3 i.e. ignore the first two field
separators and stop at the third separator.
The sort command would then be as follows:
$sort

+2 -3

filename

6) The -o option:
The output of sort always appears on the VDU (standard output file). In case, this output is to be saved on a disk file,
the o option should be used. Consider the following example:
$sort

-o file2

file1

Here, the o option indicates that the output is to be sent to the file specified after it. The name of the file to be sorted
is entered at the end of the command. o is followed by the name of the output to which the sorted data should be
written.

4.3. The Cut Filter


The cut filter of UNIX is useful when a file has to be queried to display selective fields from a file. It cuts or picks up a
given number of character or fields from the specified file. The cut assumes that a tab separates the fields.
SYNTAX: cut [options] <character or field list > <filename>
Options of the cut filter are:
Option

Significance

Key To Unix

1)

Page 26 of 57

-f

To indicate the fields to be displayed.

-d

To specify the field separator.

-c

To cut a particular range of characters.

The -f option:

This option is used to specify the fields to be listed out. Consider the example:
Ex: cut

-f 1, 2

filename

Here, the name of the file whose fields are to be displayed is specified at the end of the command. The f option is
followed by the number of each field to be displayed. The numbers are separated a comma. So, this command
displays the first and second fields from the given file.
A range of fields can also be specified with cut as shown in the following examples:

2)

cut -f 1,4 filename --

Displays first and fourth fields in the given file.

cut -f1-4 filename --

Displays all the fields up to fourth field.

The -d option:

The cut command assumes that a tab character separates the fields. If some character other than default tab
character delimits the fields, cut supports an option -d, which allows to set the delimiter. The field separator must be
enclosed in double-quotes.

Ex: Consider the file file1 may have the information for each student stored in the following format:
Name: rollno: total: grade
Each piece of information is separated by a colon, hence we require the field delimiter to be recognized as :. The
command for listing the name and grade fields would now be:
$cut -d : -f 1, 4 filename
3) The -c option:
The option is used to cut the specified columns from a file. For example,
UNIX
$cut -c 2,4 filename
As a result, the second column and the fourth column from each line in the given file would be displayed.

4.4. The head Filter


The head command, as the name implies, displays the top of the file. The simplest use of the command involves
specification of the filename, without specifying the number of lines to be displayed. When used in this way, it
displays the first ten records of the file.
SYNTAX: $head

filename

We can specify a line count and display say, the first three lines of the file. Use the - symbol, followed by a numeric
argument as follows:
Ex: $head - 3 filename
Here, the first three lines of a file would be displayed.

4.5. The tail Filter:


Complementing its head counterpart, the tail command displays the end of the file. If no line count is given, tail
displays the last ten lines of the file.
SYNTAX: $tail

filename

Key To Unix

Page 27 of 57

Ex: $tail -3 filename

Here, the last three lines from the given file will be displayed.

4.6. paste command:


This command prints the files side by side (i.e. horizontal merging as opposed to cat which does vertical merging.).
SYNTAX: $paste

Ex:

file1 file2

$cat file1 <RET>


Akanksha
Krishna
$cat

file2 <RET>
6556688
7612214

$ paste file1 file2 <RET>


Akanksha

6556688

Krishna

7612214

4.7. The uniq command:


In any EDP environment, there is often a problem of duplicate entries creeping in due to faulty data entry. You just
saw how sort removes them with the u option. UNIX offers a special tool to handle these records-the uniq
command. The command is most useful when placed in pipelines, and can be used as an SQL-type query tool.
$cat dept.lst
01|kalyani|accounts|6213
02|satish|office|4214
01|kalyani|accounts|6213
03|murthy|office|4214
03|murthy|office|4214
$_
uniq simple fetches one copy of the redundant records, writing them to the standard output:
$sort dept.lst|uniq
01|kalyani|accounts|6213
02|satish|office|4214
03|murthy|office|4214
uniq requires a sorted file as input, the general procedure is to sort a file and pipe the process to uniq.
If uniq is to merely select unique lines, it is preferable to use sort u which does this job with a single command. But
uniq has a couple of useful options; they can be used to make simple database queries. Uniq u option selects only
the non-repeated lines:
And the -c option displays the frequency of occurrence of all lines, along with lines:
$ sort dept.lst | uniq c
2

01|kalyani|accounts|6213

02|satish|office|4214

03|murthy|office|4214

Key To Unix

Page 28 of 57

4.8. Translating Characters - the tr command


tr is another important UNIX filter which maps character using one or two compact expressions. The syntax of this
command is noted in the following.
-tr <options> <expression1> <expression2> <standard input>
The first difference that should strike you is that this command takes only the standard input as the source of its data;
it doesnt take a filename as its argument. When invoked without options, it translates each character in expression1
to its mapped counterpart in expression2. The first character in the first expression is replaced by the first character in
the second expression, and similarly for the other characters.
You can use tr to replace | with a ~, and the / with a -, in the database file.
$tr |/ <empl.lst |tee empl1.lst
Here tr doesnt accept a filename as an argument, the input has to be redirected from a file. Alternately, it can be
supplied through a pipe. The following example translates the characters from lower case to upper case
$cat names.lst
satish kumar
raghu
kalyani
murthy
radha Krishna
$ tr a-z A-Z <names.lst
SATISH KUMAR
RAGHU
KALYANI
MURTHY
RADHA KRISHNA
tr-command options
Option

Description

-d

Deletes specified character from file or input

-s

Squeezes
multiple
occurrence

occurrences

in

to

single

4.9. Sending the output to some other file using tee command
SYNTAX: cat names.lst |sort |tee names1.lst
Here, cat command displays the contents of file called names.lst, sort command uses that out put and sorts the file
in ascending order, tee command divides that output into two parts. One part will be sent to the file name specified
as argument, and second part will be displayed on the screen.

Key To Unix

Page 29 of 57

5. The Vi Editor
Vi is visual editor used to enter and edit text files containing data or documents or programs. It displays the contents
of files on the screen and allows the user to add, insert, delete or change parts of the text.
5.1. The VI program has three modes of operation:

a) Command Mode: In this mode all the keys pressed by the user are interpreted to be editor command mode. In
command mode the keys that are hit are not displayed on the screen.

b) Insert Mode: This mode permits the insertion of new text, editing of existing text or replacement of existing text.
c)

Each of these operations can be performed only after changing over from the command mode to insertion mode
using appropriate commands. The insertion mode is also known as input-text mode.
The ex Mode: This mode permits us to give commands at the command line. The bottom line of the VI editor is
called the command line. All commands entered in the ex mode are displayed in the command line.
COMMAND
MODE

i,I,a,A,r,R,o,O,s,S

<ESC>
INPUT
MODE

<control-d>

:sh <enter>
ex
MODE

The three modes of vi


5.2. Getting started with vi:
The vi editor is invoked by the following command at the UNIX $ prompt:
$vi

filename

<RET>

If the give file does not exist then vi displays a blank screen with a ( ~) tilde character at the beginning of
each line as follows:
~
~
~
filename

[ New

file

If the file exists, then its contents are read in to the memory and also displayed on the screen.Assume that the given
file is a new file and the user wishes to enter text into his file.
In vi to add, change, delete text etc., commands have to be given. These commands are summarized in the following
pages.

Key To Unix

Page 30 of 57

Command

Action

Insert text at current text position

Append text after cursor position.

The above command allows a user to start entering text. The user has to press i. Key or the a key and then start
entering the text. To end insertion or appending of text, the user has to press <ESC> key. The user thus keeps
changing between command mode and edit mode.
Assume that the entered some text in the file and presses the <ESC> key after entering the second line. This takes
him out of edit mode and command mode meaning that he cannot make the changes to lines but can only give
another command. The <ESC> key toggles between the command mode and the edit mode.

5.3. Cursor Movement Commands of vi:


The basic cursor movement commands are:
Command

Action

Moves the cursor one position to the left.

Moves the cursor one character to the right.

Moves up one line in the same column.

Moves down one line in the same column.

Moves to the beginning of previous line.

^D

Scroll down half screen.

^U

Scroll up half screen.

^F

Page forward.

^B

Page backward.

Ng

Go to nth line.

0(zero)

Beginning of the line.

Insert and Replace Commands :


Command

Action

Append after current character.

Append at the end of the line.

Insert before current character.

Inserts a blank line below and allows insertion.

Inserts a blank line above and allows insertion.

r-

Replaces current character.

Replaces characters with text.

Word Movement Commands :


Command
W

Action
Moves to the first character of the next word.

Key To Unix

Page 31 of 57

B
E

Moves to the first character of the previous word.


Moves to the end of the current word.

Deletion and Modification commands :


Command

Action

Dw

Delete current word.

Dd

Delete a line.

Cw

Change word.

Cc

Change line.

x-

Delete character at cursor position.

Delete
character
position.

before

cursor

Join lines.

Undo last change.

u-

Restore last change.

U
. (dot)

Repeat last change.

5.4. Commands to Copy Lines:


Command

Action

Yy

Yank or anchor on current line


(which is to be copied)

Nyy
P
P

Yank n number of lines.


Paste yanked line after cursor
position.
Paste yanked text before current
position.

5.5. Saving and Exiting Commands:


Command

Action

:w

Save all changes made so far.

:wq

Save all changes and quit.

:q

Quit vi

:q!

Quit without saving changes.

Key To Unix

Page 32 of 57

5.6. Pattern Finding Commands:


Command

Action

f.x

Find character x on current


line after the cursor position.
Find character x on current
line before the cursor Position.

Fx
/pattern

Find next
pattern.

?pattern

Find previous
the pattern.

line

containing
line

the

containing

For additional commands on VI refer APPENDIX A

5.7. Paging and scrolling:


You can use the control keys to change the displayed text on your window. To, use
<Contrl f>

page forward one full screen

<control-b>

and to page backward, use

<control-d>

to Scroll the window forward, use

<control-u>

and to scroll backward, use

Paging functions
Command

Function

Control f

Full page forward

Control b

Full page backward

Control d

Half page forward

Control u

Half page backward

5.8. Customizing vi
VI can be tailored to behave in a way desirable to the user. There are a number of commands available in the ex
mode which make it suitable for writing programs as well as preparing documents. You make like to set a pleasant
and comfortable environment for yourself so that the editor behaves and responds to your directives exactly in the
way you would like it to.
There is also the facility to store all the user-set options in a separate file, which is used by the editor to take its
startup instructions from. This is the file .exrc containing all user-defined settings conceptually similar to the .profile
which is maintained by each user.

5.9. The set command


There are a large number of set commands available, though you hardly need to know to use more than a couple of
them. To display all the set options, you have to issue the following instruction:
:set all
This displays all the options, and also indicates whether they are operative or not. When the string no is prefixed to
any of these options, it indicates that the operation is inoperative. Some of them also use abbreviations, though at
this stage you neednt take the trouble to know.

Key To Unix

Page 33 of 57

5.10. Options used by vi


Option

Abbreviation

Significance

Autoindent

ai

Net line starts at the previous indented level

Autowrite

aw

Writes current file automatically whenever switching


files with :n and escaping to the shell with :sh

Ignorecase

Ic

Ignores case while searching for patterns

Showmode
Number

Displays a message when vi is in input mode


Nu

Displays line number on screen

Key To Unix

Page 34 of 57

6. Advanced Filters
6.1. The grep Filter
Grep stands for Global regular expression print. The grep filter is used to search for a particular pattern of
characters in a file and displays all the records that contain that pattern.
grep cannot be used without specifying a regular expression. The format of the grep filter is:
Syntax: grep [options] <pattern >

<filename(s)>

Ex: $grep picture file1


This would search for the pattern picture in the given file file1 and if found, the lines containing would be displayed
on the screen. Note that regular expression is always given in double-quotes.We can use grep to search a pattern in
several files. For example,
Ex: $grep picture file1 file2
Here, the word picture would be searched in the files, file1 and file2 and if found, the lines containing it would be
displayed along with the name of the file where it occurred.

6.2. Options with grep


Significance

Option
-c

Displays
matches,
text.

only
the
number
of
without
quoting
the

-n

Displays line number of matched


text, including the text.

-i.

Ignores case while searching.

-v

Displays lines that do not match


the text

-l

Displays
only
filenames
containing
a
match,
without
quoting the text.

-E

Searches multiple patterns in a


file.
This is equal to egrep
command

The options must be specified before the regular expressions. Options can also be combined (ex -n and v can be
used together as nv).
Note that regular expressions are always given in double-quotes.
While forming the patterns to be searched, we can use shell met meta-characters also.
Character

Use

Key To Unix

Page 35 of 57

[]

To specify a pattern this consists of any one of a set of characters.


Ex: [abc] specifies the pattern either a or b or c.
So, the command :
$grep

new[abc]

specifies the search patterns as newa or


newb or newc.
[ ] with hyphen

To specify a pattern which consists of any one of a range of characters .


Ex: [a-c] specifies the pattern either a or b or c.
So, the command
$grep new[a-c]
Also specifies the searches for either a or b or c.
Ex: [a-zA-Z] specifies a pattern of any one alphabet ,either uppercase
or lowercase.

^character

To specify that the pattern following must occur at the beginning of each
line.
Ex: ^[abc] specifies the pattern either a or b or c at the beginning of
each line.

^[ ]

To specify that the pattern must not contain any character in the set/range
specified within [ ]
Ex: [^abc] specifies a pattern which does not contain either a or b or
c.
So, the command ;
$grep new[^abc]
Specifies the search patterns as any pattern containing the word new
followed by any character but a or b or c.

6.3. sed Filter


Sed is a stream -oriented editor. Like many UNIX programs, input flows through the program and is directed to the
standard output. It is used to edit text from a single file or group of files. Most of the actions can be done manually
from within vi, but advantage of sed is that you can specify all editing instructions in one place and then execute them
on a single pass through a file.
Syntax: sed '<address><action>' <filename>
action :
p : print
d : delete
s : substitute
Examples:

sed 'p' myfile

Sed by default prints all lines in addition to lines affected by the action. So, the addressed lines are printed twice. Use
of, -n option disables this default printing
sed -n 'p' myfile
sed -n '10p' myfile

#print 10th line from myfile

sed -n '5,$p' Mayfield #print all lines from 5th to the last line

Key To Unix

Page 36 of 57

sed '/abc/p' Mayfield #print the line containing expression 'abc'


sed -n '3,$!p' Mayfield

#Lines from 3 to end are not displayed


when !(negation) is used

Delete(d):
d- Instead of p in above examples would delete the lines.
sed '1,/^$/d' Mayfield
Above example may be used to delete a mail header from an Internet mail message that user have saved in a file
Substitute(s):
Syntax: sed '[address]s/pattern/replacement/[flags]' file
Flags :
:n : replacement should be made for the nth occurrence of the
pattern.
g: replaces all occurrences of a pattern in each line
Example:

.
.

$cat Mayfield

col1<tab>col2<tab>col3<tab>col4
.
.
.
.

.
.

sed 's/<tab>/~/' Mayfield

#replaces first tab in each line with '~'

sed 's/<tab>/~/g' Mayfield

#replaces all tabs of each line with '~'

sed 's/<tab>/~/2' Mayfield

#replaces second tab with '~'

sed '/^$/!s/^/<tab>/' Mayfield

#replaces the beginning of all non-blank lines with tab

6.4. Reading and Writing to files (r,w):


Syntax :Sed [address]w file
Sed [address]r file
Example:
Assume 'Mayfield' contains names of sales people of 4 regions. The following
$sed '/Northeast$/w region.northeast' Mayfield
writes the names of salespeople of Northeast region to a file 'region.northeast'.
Read command can be useful for inserting the contents of one file at a particular place in another file. For instance,
let's say there are set of files that should close with same statement(s). A sed script would allow the user to maintain
the closing separately and append it to all files later.

$sed '$r close' *

6.5. Bracketed regular expression:


Bracketed regular expression let you store in registers the text that matches a regular expression. Nine registers are
maintained that can hold what is matched by a regular expression. Characters are put into registers by quoted
parentheses \(...\). To refer to what is held in a register, use backslash in front of the register's number: \1 for fist
register and so on.

Key To Unix

Page 37 of 57

Suppose you want to abbreviate everyone's first name to just their first initial and a period in the phone.list file (refer
exercise). Storing the first character after a comma and a space in register 1 and then substituting that for entire first
name can do it.

sed 's/, \(.\).*<tab>/, \1<tab>/' phone.list


To Swap 2 columns with colon(:) as delimiter

sed 's/\(.*\):\(.*\)/\2:\1/' Mayfield

6.6. awk Filter :


awk: It is a programming tool used to format the output. It can make searches on columns unlike grep.
Syntax:awk '[cond]{command}' [fname]
command: print
awk has some in-built variables :
FS
OFS
NR
NF

:
:
:
:

filed separator
output field separator
current record number
number of fields in current record

NOTE: awk assumes by default space as the delimiter. If you have any delimiter other than space you have to
specify the delimiter using F option.
Examples:
awk '{print $1,$2}' Mayfield
awk '{print NR,$1,$2}' Mayfield #print record numbers in the output
awk '$1~/vara/{print}' Mayfield
~: match operator for regular expressions. It searches for expression vara in the first column
awk 'NR==5{print}' Mayfield

#displays all columns from 5th record

statements of awk can be divided into 2 sections BEGIN and END.


BEGIN: contains statements that get executed before processing the lines
Syntax:
awk BEGIN{statements}{statements}END{statements} filename
awk 'BEGIN{FS=":";OFS="\t";print "headings"}{print}'
Illustration of END section:
awk '/^$/{print x++}'

#Gives the running count of blank lines

awk '/^$/{x++}END{print x}' Mayfield #displays the number of blank lines


awk program to find the total number of bytes occupied by all files of current directory
awk '
BEGIN{print "BYTES\tFILE"}
{ sum += $5
++files
print $5"\t"$9
}
END{print "Total : ",sum," bytes" "Files : ",files}'

Key To Unix

Page 38 of 57

Option
-f
-F

Description
It allows another filename.
i.e. the file contains the procedure for the
awk command
It allows you to specify delimiter
If you have any delimiter other than space

6.7. Locating files using the find command:


The find command is used to locate the files in a directory and in all sub-directories of that directory.
SYNTAX: $find <path> [options]
Here, find is followed by the pathname, which is to be searched, and whose files and sub-directories are to list out.
The pathname . indicates the current working directory.

6.8. Options of the find command


1) The -type option:
This option lists out the names of all files in the named directory and its sub-directories, which are of specific
file types:
-type f for Ordinary files
-type d for Directory files
2) The -name option:
This option lists out specific named files in all directories beginning from the named directory.
Ex: $ find. name aaa print
Here, it searches for the file aaa from current directory downward and prints it.Note that unless we use print
option the file gets located but does not get printed.
Ex: $find . name [ab]* print
Here, it finds all files which begin with a or b from current directory downwards and print them.
3) The -mtime option:
This option displays the names of all files that have been modified within a specific number of days, before the
current date.
-mtime is followed by number as follows:
-mtime n - Specifies that the files have been modified in exactly n days
Ex: $find. mtime 2 -print
Searches in current directory downwards all files, which are modified exactly 2 days back. So, if the current date
is 29/01/2001, then this would indicate that the files have been modified on 27/01/2001.
-mtime +n - Specifies that the files have been modified more than n days ago.
Ex: $find. mtime +2 -print
Searches in current directory downwards all files, which have not been modified since last 2 days i.e. which have
been modified more than 2 days ago. So, if the current date is 29/01/2001, then this would display the files that have
been modified before 27/01/2001.
-mtime -n - Specifies that the files have been modified in less than n days.
Ex: $find. mtime -2 print
Searches in current directory downwards all files, which have been modified from the past 2 days. So, if the current
date is 29/01/2001, then this would display the files that have been modified between 26/01/2001 and 28/01/2001.
4)

The -links option :

Key To Unix

Page 39 of 57

This option lists out the names of the files and directories in the named directory and its sub-directories, which have
the number of links specified.
Ex:

$find . links 2 -print

Here, it lists all the files and directories from the current directory downwards which have exactly 2 links.
5) The -size option:
This option lists out all the files and directories from the current directory downwards which are of mentioned size.
Ex: $find . size 10 print
Searches in current directory downwards all files whose size is 10 blocks.
Ex: $find .

size 10c -print

Searches in current directory downwards all files whose size 10 bytes.


6) The -perm option :
This option lists out all the files and the directories
permissions.

from the current directory downwards with specified

Ex: $find . perm 777 print


Searches in the current directory downwards all files and directories with permissions 777.
7) The -inum option:
This option lists the file or a directory existing with the specified inode number. If the inode number of one file is
known then all the hard links to that file can be identified using the following command.
Ex: $find . inum 1234 -print
Searches in the current directory downwards for a file with an inode number 1234 .
8) The -exec option :
The -exec option allows a user to perform file operations on the files that are located by the find command.
This option is specified and the end of the find command. The operation is always specified after exec.
Ex: $find . name abc -print -exec rm {} \;
Here, the command rm is to be executed on each file called abc that is located by the find command . The {} must
be specified by the exec command . The find command replaces the {} with the pathname of the files located by it
and the action rm is executed on each file.
The back-slash followed by a semi-colon is required after the exec option.
We execute any command using exec option in find command.
9) The ok option :
The exec option always execute the command on each file located by find. Another option, -ok, also allows a user to
operate on the files located. But for each file it asks for confirmation (yes/no) from the user before executing the
command.
Ex: $find . name abc -ok rm {} \;
These options, if used, must be specified immediately after the pathname in the find command and before the -print
option. More than one of these options also can be used in the same command in any order.

6.9. The ln command


UNIX system allows you to have a file known by more than one name, while maintaining a single copy in the disk.
Files are linked with ln command , which takes two filename as arguments. So, to link the tow files emp.lst with, say
employee, you would use
Ex:$ln emp.lst employee
After this command if you can observe from ls li command , that the two files have the same I-node number. The
irony is that the ln command itself is a link to the other files cp and mv. This means that there is only one file which
performs all the three functions, depending on the invocation filename

Key To Unix

Page 40 of 57

7. Process
7.1.Ps command
The ps command is used to display attributes of a process. It is one of the few command of the UNIX system, that
has knowledge of the kernel built into it. It reads through the kernel data structures or process tables to fetch the
process characteristics.
Ps is also a highly variant command, the actual output depending on the version of UNIX
$ps
PID

TTY

TIME

CMD

476

tty03

00:00:01

login

659

tty03

00:00:01

sh

684

tty03

00:00:00

ps

ps f is used of ancestry of a process

7.2. Background Processing:


While working with UNIX commands, it is a common experience to find that some commands take a long time to
complete execution. This is generally the case when a large file is being sorted or a word count is performed on a text
file. In such cases, no further commands can be entered, resulting in the user idling at the terminal. To overcome this
problem, UNIX provides a method of running time consuming commands in the background while the continues
working in the foreground. This feature of UNIX, that distinguishes it from DOS, is its capacity for muti-tasking. Multitasking makes it possible for each user to have more than one process active in the system at any one moment. For
example, the user could initiate sorting of a large file and then immediately begin editing another file. Sorting would
take place as a background process, while editing would proceed as a foreground process.

7.3. Requesting a Background Process:


The symbol used to request background is the ampersand ( & ) , which is typed at the end of the command line.
Following is an example of its usage:
$ sort

filename & <RET>

278
$ vi

filename2

Here, the user starts sorting the file with command sort and then immediately begins editing new file, without waiting
for the completion of the previous process.
The shell initiates the sorting process, the kernel assigns a process number (278) and then it returns control of the
screen to the user.
When performing background processing, the user has to be careful that the background process and the foreground
process do not interfere with each other. Any screen output from the background process could not disturb the screen
in the middle of the users work with the foreground process.
In the above example, redirecting the standard output and standard error of the sort command can solve this
problem.

Key To Unix

Page 41 of 57

Ex: $ sort file1 >file2 2>temp & <RET>


279
$ vi newfile

7.4. Checking on Background Process :


Sometimes the user will need to check on a background process to determine its current status. Is it still running?
Has it completed? Or did it run into problems?
The ps (process status) command generates a one-line entry for each of the process that is currently active.
The ps entries display the process identifier (process number assigned by the kernel). Users terminal number, the
length of time each process has been active, in minutes and seconds and the name of the command.
If the background process is not listed in the output of the ps command, the process may be completed already.

7.5. Terminating a Background Process :


There may arise a need to stop a process from executing any further after a point in time, for the following reasons:

An urgent job needs to be completed quickly and so all unimportant process need to be stopped immediately.
A command/shell script did not terminate as desired and the only way to stop it from executing is to terminate it.
The terminal went into hang because the program being executed did not function as desired.
The kill command can be shown below
$kill

279 <RET>

$_
Note the process identifier (279 in this case) which is first displayed when a background process is started,
is given as an argument with the kill command.

Key To Unix

Page 42 of 57

8. Communications
In

Multi-user system, it is often necessary for one user to know what the other is doing. When there are a hundred
odd users sharing the system resources, some of them are invariably located quite a distance apart. Communication
through the system seems quite natural and necessary the system administrator also requires to send messages to
some, and sometimes to all of them. Since UNIX was invented at Bell Laboratories, its not surprising that the
electronic mail facilities are simple and quite elaborate. Its rich communication features can be exploited by the user,
both to communicate with others, as well as with himself. These features are so useful that for some people UNIX
means electronic mail.

8.1. To-way communication-The Write Command


The write command lets you have a two-way communication with any person who is currently logged in. One user
writes his message and then waits for the reply from the other. In this way it is possible to continue a conversation
until such time as one or both the users decide to terminate it. write uses the login-id of the recipient as an artument,
and the text of the message from the standard input.

$ write u1003p
have you completed your program ?
I have completed mine- kumar
o

Write is used interactively in this manner, both users need to invoke the command individually. This establishes two
write channels, one for the receiver, and the other for the sender. However, if there is need for an extended
communication, then one user can simply wait after he has keyed in this message, without pressing [control-d]. write
is so designed that it enables the user to send and receive messages at the same time, without his having to quit to
the shell.
NOTE: For two users to communicate with each other, both must invoke the write command. if two users have the
same login name, then the terminal name has to be used along with the login name.

8.2. The mesg Command


Communication, single or two-way, can be disconcerting to a user who might be watching the output of a very
important program on his terminal at that instant. He obviously wouldnt like the screen to be disturbed by such
unexpected intrusions. In that case he can use the mesg command to insulate himself. The command is
$ mesg n
prevents other people from writing to this terminal, while
$ mesg y
Enables receipt of such messages. By default, the terminal behaves as if it is in this mode though this can vary
across systems.
If you want to know the status your terminal is in, then simple use mesg without arguments. The y indicates that the
terminal is receptive to others messages:

Key To Unix

Page 43 of 57

$mesg
is y
$_

8.3. The mail command- The Mail Box


The mail command is one of the most popular in the UNIX repertoire of electronic mail. Unlike write, it enables
sending of mail to a user even if he is not logged in. It is mainly used for non-interactive communication, especially
when there is no urgency. Even though it has a host of options and internal commands, few of them are really
complex.
Like write, mail uses standard input, and a primitive way of using this program is to take the input from the keyboard:
$ mail <user id>
subject : new system
the new system will start functioning from next month.
[Control d]
EOT #to indicate the end of text
$_
mail is essentially a single-channel communication command, and the sender doesnt use it for conversation. The
message doesnt directly appear on the receivers terminal either. If the receiver is running a program, then mail
waits for program execution to finish before flashing the following message. New mails are stored in /var/spool/mail
The advantage of mail is that you neednt handle the moment you are informed of it, and can reply to it whenever you
want (if at all). However, it is good practice to see incoming mail immediately upon arrival, rather than defer it for
future viewing. To do that, received user also has to invoke the mail command (but without any argument) to see the
mail, that has sent by his friend.
Note: You can send mail to a user even if he is not logged onto the machine.

8.4. The wall command:


The wall command has more urgency than the others, as it addresses all the users. Though the command is
available in /etc. it can be invoked by any user provided he has the authorization, by employing the absolute
pathname of the command. UNIX by default does not permit users to use this command, and reserves it for the sole
use of the system administrator. There are no arguments, and the standard input is used for text input.
$wall
The machine will be shut down in a few minutes
[control-d]
$
Every user currently logged in will receive this message on his terminal, duly preceded by a similar header line that
accompanies the write command. this command is routinely executed by the system administrator.
The characteristic feature of write, mail and wall commands is that they dont use a filename as an argument. The
input to these programs always comes from the standard input through keyboard. Redirection and piping are thus
possible.

Key To Unix

Page 44 of 57

9. Introduction to Shell
The shell is an intermediary program, which interprets the commands that are typed at the terminal, and translates
them into commands that the kernel understands.

9.1. Login process


On switching on a UNIX machine, the kernel program is first loaded into memory from the hard disk. This program
triggers a chain of initialization processes at the end of which login prompt appears. On entering a valid login and
password the process named Bourne shell or the C shell (called login shell) is activated.

9.2. Types of shells :


sh(bourne) : Uses test and expr to perform condition check and arithmetic operations
ksh :
Korn shell supports history, aliasing and arrays
Creating arrays in Ksh
Planets[0]=mercury
Planets[1]=venus
Planets[2]=earth
.
.
.
Or
set -A Planets mercury venus earth mars jupiter Saturn Neptune pluto
variable in a single step
print $Planets
print ${Planets[3]}

#to set all values to a

#to print all values


#print a particular value

C shell :
C shell doesn't use .profile instead it uses ~/.login file for storing instructions that are to be executed during login time.
Variable assignment in C shell
set var=value
or
@ var=value
Values can be put into an array with set
set num=(1 2 3 4)
echo $num[1]
C shell can handle integers for computation
set c=$x+$y
#bourne(sh) uses expr for arithmetic operations
syntax ( if ) :
if (condition) then
.
.
endif

Key To Unix

Page 45 of 57

for loop in C shell


Example:
for each x (1 2 3 4 )
#for each is used instead of 'for' in bourne(sh)
echo "The value of x is $x"
end
In C shell exit status of previous command is stored in variable status ($? in bourne).
ALIASES
Alias l ls -l

#No = symbol

9.3. expr Command


expr is one of the few UNIX commands which can handle computations. But like the shell this computation is
restricted to integers only. This is certainly a major limitation of this command, but it can be useful in handling simple
arithmetic tasks, in addition to handling strings. expr takes as its arguments the expressions separated by the
operand. expr does not permit division by zero. Any attempt to do so causes it to retort in the following manner:
$ expr 4 / 0
illegal instruction core dumped
$
As a computation tool, expr can perform the basic four arithmetic operations, as well as the modulus (remainder)
functions. It is invoked very simply :
UNIX does not allow direct arithmetic on variables. It uses expr to perform arithmetic.
$a=10
$b=5
$echo `expr $a + $b`
NOTE: There must be a space on either side of the colon

9.4. Environment Variables:


These variables define the environment of a shell. Setting a new value to these variables can change the values of
these variables. The contents of environment variables can be displayed using echo.

9.5. The HOME variable:


HOME directory is defined in a variable called HOME. This variable is set to full path name of the login directory.

9.6. The PATH variable:


It contains the list of all paths of directories that are to be searched for executable programs. A colon separates these
names.
For example, the following command
$PATH=/usr/bin:/bin <RET>
$
Specifies that the directories to be searched for any executable file are /usr/bin and /bin - in this order.
The PS1 variable: This variable contains the system prompt
HISTSIZE variable: Defines a value, which stores the previous commands in buffer

Key To Unix

Page 46 of 57

The SHELL variable : Shell variable determines the type of shell that a user sees on logging in. Today, there are a
host of shells that accompany any UNIX system, and you can select the one you like the most. The C shell is known
by the program csh, and the Korn shell by ksh. In linux default shell is bash.
The TERM variable: TERM indicates the terminal type being used. There are some utilities which are terminaldependent, and they require to know the type of terminal you are using. One of them is the Vi editor, which makes
use of a control file in a sub-directory under the directory /usr/lib/terminfo. If TERM is not set correctly, Vi wont
work, and the display will be faulty.

9.7. Shell Script


Making shell Interactive using read statement:
The echo Command: Th echo command, displays its (excluding the options) arguments. It is an in-built command of
UNIX.
The read statement is the shells internal tool for taking input from the standard input. It can be used with one or
more variables to make shell scripts interactive. Input supplied through the standard input during interaction is read
into these variables.
Ex :

$echo hellow
hellow
$
A Sequence or a set of UNIX commands to do a particular task can be stored in a file and executed. Such a file is
called shell script.
Example:
#Script to find the length of the string
echo "Enter the string"
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
echo " length of $str is $len"
Constructs used in shell scripts

9.8. EXIT statement:


Exit statement is used to prematurely terminate a program. When this statement is encountered in the script,
execution is halted and control is returned to the calling program, in most cases the shell.

9.9. Break AND continue:


They have their counterparts in the C language with the same names. The continue statement suspends execution
of all statements following it, and switches control to the top of the loop for the next iteration. The break statement,
on the other hand, causes control to break out of the loop.

9.10. Exit status of the last command- the $?


The parameter $? Stores the exit status of the last command. It has the value 0 if the command succeeds, and a
non-zero value if it fails. For example, if grep fails to find a pattern, then the return value is 1, and if the file scanned
is unreadable in the first place, the return value is any number other than 0.
$ grep director emp.lst >/dev/null; echo $?
0
$ grep manager emp.lst>/dev/null; echo $?
1
$ grep manager emp3.lst>/dev/null;echo $?
grep: cant open emp3.lst
2
grep is one command which returns these values; 1 if the pattern cant be found, and 2 if the file cannt be opened

Key To Unix

Page 47 of 57

Shell Programming Constructs

9.11. if..then..else condition


The if statement, like its counterpart in other programming languages, takes two-way decision depending on the
fulfillment of a certain condition. In the shell, the statement uses the following form, much like the one used in other
languages.
if test condition
then
command(s)
else
command(s)
fi
fi is used to indicate the end of the if construct
if evaluates a condition which accompanies its command line. If the condition is fulfilled (i.e. returns a true exit
status), then the sequence of commands following it is executed. Mark the key words then and fi which must
necessarily accompany every if conditional. The construct also provides for an alternate action using the optional
keyword else.
Note: every if must have an accompanying then and fi, and optionally an else
# display the contents of the file page by page
echo give the file which u want to see
read file
lines=`wc -l < $file`
echo $file has $lines lines
if test $lines -gt 24
then
cat $file | less
else
cat $file
fi
#Script demonstrating if
echo Enter a number
read num
r=`expr $num % 2`
if test $r -eq 0
then
echo Number is Even
else
echo It is Odd
fi
To test file status using test
Test -f filename

True if file exists and is an ordinary file

Test d filename

True if file exists and is a directory

Test r filename

True if file exists and is a readable

Test w filename

True if file exists and is writeable

Key To Unix

Page 48 of 57

Test x filename

True if file exists and is executable

Test s filename

True if file exists and is not empty

Operator
-eq
-ne
-gt
-ge
-lt
-le

Meaning
Equal to
Not equal to
Greater than
Greater than or equal
Less than
Less than or equal to

When you utilize if to evaluate expressions, the test statement is invariably used as its control command. On most
UNIX system, it is an internal feature of the shell, test evaluates the condition place on its right, and returns either a
true or false exist status. This return value is used by if for taking decisions.
#Illustrating test option
if test -f abc
then
echo "File is an ordinary file"
else
echo "File is not an ordinary file"
fi
Arithmetic tests and string tests
#finding the percentages and grades of the students
echo enter the maths marks
read maths
echo enter the science marks
read science
echo enter the social marks
read social
total=`expr $maths + $science + $social`
echo the total of the three subjects is $total
percentage=`expr $total / 3`
echo the percentage of the subjects is $percentage
if test $maths -lt 35 -o $science -lt 35 -o $social -lt 35
then
echo student is failed
else
if test $percentage -lt 35
then
echo sorry the candidate is failed
else
if test $percentage -ge 35 -a $percentage -lt 60
then
echo congratulations u got "c" grade
else

Key To Unix

Page 49 of 57

if test $percentage -ge 60 -a $percentage -lt 80


then
echo congratulations u got "B" grade
else
echo congratulations u got "A" grade
fi
fi
fi
fi

9.12. String test Operators


$1 = $2

True if strings S1 and S2 are identical

$1 != $2

True if strings are not identical

-z S1

True if S1 contains no characters

# SCRIPT TO SEARCH FOR A STRING IN A FILE USING STRING OPERATORS


$cat emp.sh
echo enter the string to be searched :
read
pname
if [ -z $pname ]
then
echo you have not entered the string
exit 1
fi
echo enter the file name to be used:
read flname
if [ ! n $fname ]
then
echo you have not entered the file name
exit 2
else
echo seach for $pname from $fname?:
read answer
if [ $answer = y ]
then
grep $fname $flname || echo pattern not found
else
exit 3
fi
fi
$

9.13. Logical operators


-o
-a

Or
And

#Script to find the greatest of two


clear

Key To Unix

Page 50 of 57

echo "Enter 2 numbers"


read a
read b
if test $a -eq $b
then
echo "A & B Are equal "
elif test $a -gt $b
then
echo A is greater
else
echo B is greater
fi

9.14. The case...esac construct


The case statement is the second conditional offered by the shell. It doesnt have its parallel in most languages. The
statement matches on expression for more than one alternative, and uses a compact construct to permit multi-way
branching. It also handles string tests, but in a more efficient manner than if. The general syntax of case statement
is as follows:
syntax:
case $variablename in
value1) command
command
:
:
;;
value2) command
command
:
: ;;
:
:
esac
case..esac construct evaluates the value of the variable and compares it with each value specified(value1,value2,...)
When the value of the variable matches one of the values specified, the set of commands specified under the value
is/are executed. The last command to be executed for any value of the variable must be followed by a pair of semicolons.
For example using this construct. You can devise a script, which accepts values from 1 to 5, and performs some
action depending on the number keyed in. the code for this menu is stored in the file menu.sh :
$ cat menu.sh
echo menu
1. list of files
2. process of user
3. todays date
4. users of system
5. quit to system
echo Enter your option :\c
read choice
case $choice in
1)
2)
3)
4)
5)

ls l ;;
ps f ;;
date ;;
who ;;
exit

esac
The five menu choices are displayed with a multi-line echo statement. The meanings attached to these choices are
obvious enough. The user response from the keyboard is accepted to the variable choice. The case statement then

Key To Unix

Page 51 of 57

matches the value of this variable for the strings 1,2,3,4, and 5, as shown in the five lines below the key word case. It
then relates each value to a command that has to be executed.
Note : The last case option need not have the ;; though you can provide them if you want.
#Script to check if a given character is Vowel or not
echo "Enter a character"
read a
case $a in
[aA])echo $a is Vowel;;
[eE])echo $a is Vowel;;
[iI])echo $a is Vowel;;
[oO])echo $a is Vowel;;
[uU])echo $a is Vowel;;
[0-9]*)echo $a is a number;;
*)echo $a is consonant;;
esac
* is interpreted as any value other than those listed earlier with case

9.15. for..do..done
The for loop is different in structure from the ones used in other programming languages. There is no next statement
here, neither can a step be specified. Unlike while and until, it doesnt test a condition but uses as list instead. The
syntax is as follows:
syntax :
for variable name in list of values
do
command
command
:
:
done
Example
for x in 1 2 3 4
do
echo $x
done
#Demonstrating a for loop
#To rename all .c files to .cpp
for fname in *.c
do
nname=`echo $fname | sed 's/.c$/.cpp/'`
mv $fname $nname
done

9.16. while..do..done
The pattern scanning scripts developed so far still suffer from serious defects. You have no way of giving the user
another chance when he has made a faulty response. And you are able to scan only one pattern at a time. Loops let
you perform a set of instructions repeatedly. The shell features three types of loops while, until and for. The first
two are basically complementary to each other. All of them repeat the instruction set enclosed by certain keywords
as often as the control command permits.
The while statement should be quite familiar to most programmers. It repeatedly performs a set of instructions till the
control command returns a true exist status. The general syntax of this command is as follows:

Key To Unix

Page 52 of 57

while condition
do
command
command
:
:
done
# sample script using while loop
$ cat menu.sh
while :
do
echo menu
6. list of files
7. process of user
8. todays date
9. users of system
10. quit to system
echo Enter your option :\c
read choice
case $choice in
6)
7)
8)
9)
10)

ls l ;;
ps f ;;
date ;;
who ;;
exit

esac
echo do you want to continue
read chc
if [ $chc = y o Y ]
then
continue
else
break
done
#Script to display "Hello" 10 times
i=1
while [ $i -le 10 ]
do
echo Hello
i=`expr $i + 1`
done
#Script demonstrating while true loop
while true
do
clear
cat<<MENU
1.list of files
2.Current directory
3.Users
4.exit
MENU
echo -e "Enter Your choice: \c"
read choice
case $choice in
1)ls;;
2)pwd;;
3)who;;

Key To Unix

Page 53 of 57

4)break;;
*)echo "Invalid Choice"
esac
read
done

9.17. Parameter handling in shell scripts


Shell Variables:
$?
$0
$1
$2
.
$9
$#
$*

#contains the exit status of last executed command


#contains the command on the command line
#contains the first argument on the command line
#contains the second argument on the command line
#contains number of arguments on the command line
#contains arguments of the command line
#SS1
#Script illustrating use of positional parameters
echo
echo
echo
echo
echo

"The name of the program is $0"


"The first argument is $1"
"The second argument is $2"
"The number of arguments are $#"
"They are $*"

$chmod u+x SS1


A sample run :
$SS one two three <RET>
The name of the program is SS
The first argument is one
The second argument is two
The number of arguments are 3
They are one two three
$_

#Script using Shell variables


#To check if a user has logged in or not. If yes, display the terminal on
which he has logged in
if [ $# -le 1 ]
then
echo "Syntax : <fname> <uname>
exit
fi
who | grep "$1">/dev/null
#
if [ $? -eq 0 ]
then
who|awk 'BEGIN{print "USERNAME\tTERMINAL}{print $1"\t"$2}'
else
echo user has not logged in
fi

Key To Unix

Page 54 of 57

APPENDIX A
Block commands in vi
Command
:nd
:m,nd
:n mo p
:m,n mo p
:m,n w filename
:m,n w >> filename

Function
Deletes nth line
Deletes lines from m to n
Moves line n after line p
Moves lines m to n after line p
Writes lines m to n to a file
Appends lines m to n to a file
Reads the contents of the file filename at current cursor position

:r filename
:r! Filename

Executes shell command and output of the command is read at


the current position.
:m co p
Copies line m after line p
:m,n co p
Copies lines m to n after line p
:abbr <abbreviation> <longform>
An abbreviation is defined for longform
:abbr
Lists currently defined abbreviations
:una <abbr>
Unabbreviates the abbreviation

Set Commands :
:se nu
:se ic
:set showmode
:set aw

Set display of line numbers on


Ignore case while searching a pattern
Display mode in which we are working
Automatically write buffer contents to disk before switching to next
file during multiple file editing

To disable the set commands prefix no


Example: se noic

Map command:
The map command lets you assign the undefined keys so that when a key is pressed it expands in to a command
sequence. The command :map is followed by the key, which needs mapping, and the key sequence, which is
mapped.
For example we can use one of the function keys say f2 and map it to the function.

Ex: :map #2 ^[:w^M


Lets you save the file in the command mode. The string ^[ is actually the <ESC> character which is entered in vi , by
pressing <ctrl-v><ESC>.

Key To Unix

Page 55 of 57

APPENDIX B
Logical operators && and ||
Above code in italics can simply be written as who|grep "^$1"||echo "$1 not logged in". Command after '||' get
executed only if the first one fails. Command after '&&' get executed only if the first one succeeds. These operators
have a limitation that they are used for making simple decisions.
Handling command line options using getopts
getopts reads the options given on the command line and decides whether they are valid or not. getopts accepts
single character options from the command line provided the option is preceded by a minus (-) sign. On encountering
an option getopts stores it in the variable at the end of getopts command. On encountering an illegal option getopts
stores a '?' in the variable at the end of getopts.The ':' tells getopts that an argument is expected along with an option.
The word immediately following the option on the command line is read as argument to that option. getopts stores
argument to an option in the variable OPTARG.
#Script illustrating getopts
while getopts abcd choice
do
case $choice in
a)echo "you entered a";;
b)echo "you entered b";;
c)echo "you entered c";;
d)echo "you entered d";;
?)echo "Wrong choice"
esac
done
#Script illustrating arguments for an option
while getopts a:c:d choice
do
case $choice in
a) echo "Argument for a is $OPTARG"
c) echo "Argument for c is $OPTARG"
d) echo "Argument for d is $OPTARG"
esac
done
Shell Functions:
Syntax:
funame()
{
.
.
}

Ex:
dated_fname()
{
set -- `date`
year=`expr substr $6 2 2`

Key To Unix

Page 56 of 57

echo "$2$3_$year"
}

Shell Scripts and Shell Functions :


A shell script is stored in a file. A shell function is stored in the shell's local environment. The current shell executes a
function where as the shell script is run in a child process. Thus any changes made to the environment by a function
are made to the current shell's environment. A shell script's changes are made to the environment of the child
process and have no effect on the current shell's environment. A function only exists in the shell where it is defined.
They cannot be exported from one process to another. Functions like shell script's can access the command line
arguments via the shell positional parameters.Functions can be exported in Linux to subshells (Syntax : export -f
<fname> ).
eval: It evaluates the command line to complete any shell substitutions and then executes the command.
(a) Consider the following :
y=x
z=y
echo $$z
Above echo displays 1324z instead of 'x'. Since $$ gives PID of current shell. This PID gets printed followed by 'z'. If
we wish to get 'x' as output we need to give the following command
$eval echo \$$z
eval is used when a single pass of shell substitution does not complete all the needed expansion. '\' is
necessary, when the statement is scanned '\' ensures that the first $ is preserved such that it can later
be used to extract the value of the variable y.
(b) Consider the following:
output="myfile>yourfile"
cat $output
If we execute this script it reports that file > does not exist. This is because I/O redirection
and piping is done before variable substitution. So once the variable has been substituted the shell cannot do
redirection and thus attempts to cat three files Mayfield, > and yourfile. Hence
eval cat $output
(c) Consider the following:
cat<ch*17
I/O redirection is done before filename substitution. Hence before the shell can replace ch*17 with matching files, it
would attempt to redirect the standard input to ch*17. And if such file does not exist an error results. Hence
eval cat \< ch*17
Commands built into the Shell
If a separate process executes the cd, it will change the directory of that new process and have no effect on the
current shell. For cd to work, it has to be built into the shell, so that a new process isn't required to execute it. Such
commands are called built-in shell commands. The echo and test commands are examples.Unix process creation
consists of 2 steps. First, a copy of current process is forked. Then the new process overwrites the command portion
of the process by executing a command.
When the shell executes a command, it forks and then a new process execs the command. You can exec
commands directly from the current process, using exec. If you exec a command from your login shell, you will be
logged out when the execed command is over.
$exec date

Key To Unix

Page 57 of 57

Fri Dec 29 09:45:18 PST 1989


Login:
When date was executed, the current process's command area was overwritten with date's executable code. After
date prints the current date and time, it terminates, ending your login session.
exec also enables you to change the current process's standard streams.
For example
exec Mayfield modifies the current process so that it takes input from Mayfield
#Script demonstrating exec
echo Enter filename
read fname
terminal='tty'
exec< $fname #exec ensures that input for 'read' comes from a file and not from the
keyboard
while read line
do
lines=`expr $lines + 1`
set -- $line
#set assigns the values to positional parameters
words=`expr $words + $#`
done
echo "Number of lines = $lines"
echo "Number of Words = $words"
exec< $terminal

You might also like