0% found this document useful (0 votes)
15 views43 pages

UNIT1UNIX

The document provides an overview of UNIX as an operating system, detailing its role as an interface between hardware and users, and its historical development from MULTICS to UNIX. It describes the architecture of UNIX, including the kernel, shell, and file management, as well as its features such as portability, multiuser capabilities, and multitasking. Additionally, it covers UNIX environments, command structure, and basic commands like date, cal, who, and passwd.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views43 pages

UNIT1UNIX

The document provides an overview of UNIX as an operating system, detailing its role as an interface between hardware and users, and its historical development from MULTICS to UNIX. It describes the architecture of UNIX, including the kernel, shell, and file management, as well as its features such as portability, multiuser capabilities, and multitasking. Additionally, it covers UNIX environments, command structure, and basic commands like date, cal, who, and passwd.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

UNIT-I INTRODUCTION-UNIX

WHAT IS AN OPERATING SYSTEM (OS)?


An operating system (OS) is an interface between hardware and user. It manages hardware and
software resource. It takes the form of a set of software routines that allow users and application
programs to access system resources (e.g. the CPU, memory, disks, modems, printers, network
cards etc.) in a safe, efficient and abstract way.
For example, an OS ensures safe access to a printer by allowing only one application program
to send data directly to the printer at any one time. An OS encourages efficient use of the CPU
by suspending programs that are waiting for I/O operations to complete to make way for
programs that can use the CPU more productively. An OS also provides convenient abstractions
(such as files rather than disk locations) which isolate application programmers and users from
the details of the underlying hardware.

BRIEF HISTORY
 In the late 1960s, researchers from General Electric, MIT and Bell Labs launched a joint
project to develop an ambitious multi-user, multi-tasking OS for mainframe computers
known as MULTICS (Multiplexed Information and Computing System).
 MULTICS failed, but it did inspire Ken Thompson, He wrote a simpler version of MULTICS
on a PDP7 in assembler and called his attempt UNICS (Uniplexed Information and
Computing System). Because memory and CPU power were at a premium in those days,
UNICS (eventually shortened to UNIX) used short commands to minimize the space
needed to store them and the time needed to decode them - hence the tradition of short.
 The limitation of UNICS was not portable. In oder to overcome the limitation , Ken
Thompson started to work on the development of system using higher level language
called B Language.
 As B language did not yield expected results,Dennis ritchie developed higher level
language called C .Ken Thompson then teamed up with Dennis Ritchie, the author of the
first C compiler in 1973. They rewrote the UNIX kernel in C - this was a big step
forwards in terms of the system's portability - and released the fifth Edition of UNIX to
universities in 1974.

UNIX ARCHITECTURE / UNIX communicates directly with the hardware. It


STRUCTURE is responsible for allocating the resources to
the processes, handling and coordinating the
multiple processes running together.
Shell: since the kernel is such an essential
program in the Operating System and
performs almost all of the major
tasks, it should not be directly accessible to
the user. So, there is a program between the
user and the kernel that helps the user
interact with the kernel. This program is
called the shell. It functions as command
interpreter i,e it receives and interprets the
command from user and interacts with the
hardware. There is only one kernel running
on the system, there could be several shells
in action- one for each user who is logged in.
Files and Process: File is an array of bytes
and it contain virtually anything. Unix
considers even the directories and devices
Kernel: The kernel is a program in the as members of file system. The dominant file
Operating System software that keeps on type is text and behavior of system is mainly
running in the background It is loaded into controlled by text files.
memory when the system is booted and
The second entity is the process, which is as a program.
the name given to a file when it is executed

Process is a program file under execution. Files and processes belongs to a separate hierarchical
structure.
System Calls: Though there are thousands of commands in the unix system, they all use a
handful of functions called system calls. User programs that need to access the hardware use
the services of the kernel, which performs the job on

users behalf. These programs access the kernel through a set of functions called system calls.
Ex: open()-- system call to access both file and device.
Write()- system call to write a file.

FEATURES OF UNIX
Several features of UNIX have made it popular. Some of them are:
 Portable: UNIX can be installed on many hardware platforms. Its widespread use can
be traced to the decision to develop it using the C language. Because C programs are
easily moved from one hardware environment to another, it is relatively simple to port it
to different environments.
 Multiuser: The UNIX design allows multiple users to concurrently share hardware and
software
 Multitasking: UNIX allows a user to run more than one program at a time. In fact more
than one program can be running in the background while a user is working foreground.
 Networking: While UNIX was developed to be an interactive, multiuser, multitasking
system, networking is also incorporated into the heart of the operating system. Access to
another system uses a standard communications protocol known as Transmission Control
Protocol/Internet Protocol (TCP/IP).
 Organized File System: UNIX has a very organized file and directory system that allows
users to organize and maintain files.
 Device Independence: UNIX treats input/output devices like ordinary files. Input or
output to a program can be from any device or file. The source or destination for file input
and output is easily controlled through a UNIX design feature called redirection.
 Utilities: UNIX provides a rich library of utilities that can be use to increase user
productivity.
 Services: UNIX also includes the support utilities for system administration and control.

THE UNIX ENVIRONMENTS AND UNIX STRUCTURE


There 3 different environments in UNIX
Personal environment
Timesharing environment: Many users connected to one computer.
Client/server environment: Computing split between a central computer (server) and users’
computers clients.
Personal environment originally unix designed as a multiuser environment, many user are
installed UNIX on their personal computers this tends to personal unix system environment.
Timesharing environment :

In a time-sharing environment, many users


are
connected to one or more computers. The
output devices (such as printers) and  It must manage the printing data and
auxiliary storage devices (such as disks) are resource.
shared by all users. In this environment all of  It must handle the computing all task.
the computing must be done by the central All of this work tends to keep the central
computer. The central computer has several computer
responsibilities busy so, user has to wait more time for get
 It must control the shared resources. done their work so, it is non-productive
 It must manage the shared data because of slow response.
The Client/Server Environment

In client server environment splits the computing function between a central computer and
users computers.
User computer are personal computer or workstation central computer assigned to the
workstations. In client server environment the users computer or workstation is called client and
central computer is called server.
The central computer , which may be a powerful microcomputer a minicomputer, a central
mainframe system is known as server.
Since work is shared between users computer and the central computer, response time and
monitor display are faster and users are more productive.

Unix Architecture
Have a look at the image shown below:

The Unix architecture has 4 layers. These layers are as shown below:
Hardware: Hardware is the most simple and least powerful layer in the Unix Architecture.
Hardware is the components that are humanly visible. Whatever hardware is connected to a
Unix operating system-based machine, comes in the hardware layer.
Kernel: This is the most powerful layer of the Unix architecture. The kernel is responsible for
acting as an interface between the user and the hardware for the effective utilization of the
hardware. The kernel handles the hardware effectively by using the device drivers. The kernel is
also responsible for process management. So, the main 2 features of the kernel are process
management and file management.
Process Management: The processes that execute within the operating system require
a lot of management in terms of memory being allocated to them, the resource allocation
to the process, process synchronization, etc. All this is done by the Kernel in Unix OS. This
is done using various Operating System Techniques like paging, framing, virtual memory,
swapping, context-switching, etc.
File Management: File management involves managing the data stored in the files. This
also includes the transmission of data stored in these files to the processes as and when
they request it.
Shell: Since the kernel is such an important program of the Unix Operating System, its direct
access to the users can be dangerous. Hence, the Shell comes into the picture. Shell is an
interpreter program that interprets the commands entered by the user and then sends the
requests to the kernel to execute those commands. When the execution of the process is
completed, the shell again sends a request to the kernel to display the program/information on
the screen to the user. So, Kernel is an interface between the user and the hardware and the
Shell is an interface between the user and the Kernel. The shell can be used for opening a file,
writing into the files, executing programs, etc. There are 3 types of shells in the Unix Operating
system.

Bourne Shell (sh): It is the most widely available shell on Unix OS devices across the world.
This was the first shell available in the Unix OS. It is simply called a shell.
C Shell (csh): The University of California (Berkeley) developed C Shell is another Unix shell
that removes some of the obsolete features or problems from the Bourne Shell. So, it enhances
the performance of the Bourne Shell.
Korn Shell (ksh): The name of the Korn Shell is based on its creator, David Korn. This shell
enhances the C shell further by removing the shortcomings of the C shell and also enhancing the
user interaction of the Bourne Shell.
Applications/Application Programs: The last layer of the Unix architecture is the Application
Program layer. As the name suggests, this outermost layer of Unix Architecture is responsible for
executing the application programs.

POSIX AND SINGLE UNIX SPECIFICATION

fragmentation and absence of a single conforming standard adversely affected the development
of portable applications. First ,AT &T created the System V Interface Definition(SVID). Later,
X/Guide(XPG). Products conforming to this specification were branded UNIX95, UNIX98 or
UNIX03 depending on the version of the specification.
Yet another group of standards, the portable operating system interface for computer
environments(POSIX), were developed at the behest of the Institution of Electrical and
Electronics Engineers(IEEE). POSIX refers to operating systems in general, but was based on
UNIX. Two of the most cited standards from the POSIX family are known as POSIX.1 and POSIX.2.
POSIX.1 specifies the C application program interface the system calls. POSIX.2 deals with the
shell and utilities.
In 2001, a joint initiative of X/Open and IEEE resulted in the unification of the two standard
approach to this development means that once software has been developed on any POSIX
compliant UNIX system, it can be easily ported to another POSIX- compliant UNIX machine with
minimum modifications. We make reference to POSIX throughout this text, but these references
should be Interpreted to mean the SUSV3 as well.
GENERAL FEATURES OF UNIX COMMANDS/ COMMAND STRUCTURE
Commands are entered at shell prompt.The components of the command line are: the verb;
any options required by the command the command's arguments (if required).
For example, the general form of a UNIX command is:

Verb: is the command name. The command indicates what action is to be taken. This action
concept gives us the name verb for action .
option: modifies how the action is applied.
argument: provides additional information to the command.
Note: Options MUST come after the command and before any command arguments. Options
SHOULD NOT appear after the main argument(s). However, some options can have their own
arguments
if options are enclosed within the [] then options are not mandatory else it is compulsory
if arguments are enclosed within the [] then options are not mandatory else it is compulsory
UNDERSTANDING OF SOME BASIC COMMANDS SUCH AS echo, printf, ls, who,
date, passwd, cal.
THE DATE COMMAND:

date: displays the system date and time. If the system is local that is one in your own area-it is
the current time.If the system is remote, such as across the country the reply will contain the
time where the system is physically located.
The input for the date is the system itself.The date is actually maintained in the computer as a
part of OS.The date command sends its response to monitor.
$date

Department of CSE, ATMECE, Mysuru 17


Format Explanation
code
A Abbreviated weekday name such as Mon
A Full weekday name such as Monday
B Full month name such as January
B Abbreviated month name such as jan
D Day of the month
E Day of the month with spaces replacing zeros (1,2,
D Date in the format (mm/dd/yy)
H Military time-two digit hour
I Civilian time two digit hour
J Julian date
M Two digit
M
P Display am or pm
R Time in format hour: minute: second with am/pm
R Time in format hour: minute
S Seconds as decimal number [00-61]
T Time in format hour: min: second
U Week number of the year
W Week of year[00-53], with Monday being first day of the
week.
Y Year as ccyy(4 digits)
Z Time zone name
cal: The CALENDAR COMMAND
A single parameter specifies the 4 digit year (1 - 9999) to be displayed.

Two parameters denote the Month (1 - 12) and Year (1 - 9999). If arguments are not specified,
the current month is displayed. A year starts on 01 Jan.
To display current month's calendar $ cal Output:

Department of CSE, ATMECE, Mysuru 17


To display feb 2015 calendar
$cal 2 2015 Output :

To display complete year calendar.


$ cal –y
who: The who is online command
The who command prints information about all users who are currently logged in.

who [ OPTION]…[FILE] [am i]

-u –Idle time: Print the idle time for each user, and the process ID.
-H –HEADING: Print a line of column headings.
$who
Displays the username, terminal, and time and date of all currently logged-in sessions.

$who am i
Displays the same information, but only for the terminal session where the command was

issued, for example:


$who u
Indicates how long it has been since there was any activity on the line. This is known as
Idle time. It also returns the process id for the user.

$who -uH
Displays "all" information, and headers above each column of data, for example:

passwd [OPTION] [USER]

Department of CSE, ATMECE, Mysuru 17

passwd command.: The change Password (PASSWD) COMMAND


The passwd command is used to change the password of a user account. A normal user can run
passwd to change their own password, and a system administrator (the superuser) can use passwd
$passwd
Running passwd with no options will change the password of the account running
the command. You will first be prompted to enter the account's current password:
(current) UNIX password:
If it is correct, you will then be asked to enter a new password: Enter new UNIX
password:
...and to enter the same password again, to verify it: Retype new UNIX password:
If the passwords match, the password will be changed.

The passwd command changes passwords for user accounts. A normal user can only change
the password for their own account, but the superuser can change the password for any
account. Passwd can also change or reset the account's validity period how much time can
pass before the password expires and must be changed.
Before a normal user can change their own password, they must first enter their current
password for verification. (The superuser can bypass this step when changing another user's
password.)
After the current password has been verified, passwd checks to see if the user is allowed to
change their password at this time. If not, passwd refuses to continue, and exits.
Otherwise, the user is then prompted twice for a replacement password. Both entries must
match for passwd to continue.

Rules for giving passwords


 must be >=6 characters long,
 must contain 2 out of 3 of upper-case letters,
 lower-case letters,
 non-letters (digits, punct)
 may not be a dictionary word or too similar to your name
echo: Print message command
The echo command used to display a message.

To display the diagnostic messages on the terminal or to issue prompts for taking user input
To evaluate shell variable.

$ echo [SHORT-OPTION]... [STRING]...


Options

-n Do not output a trailing newline.


-e Enable interpretation of backslash escape sequences (see below for a
list of these).
-E Disable interpretation of backslash escape sequences (this is the
default).
--help Display a help message and exit.
version Output version information and exit.

If you specify the -e option, the following escape sequences are recognized:

Department of CSE, ATMECE, Mysuru 17


\\ A literal backslash character ("\").
\ An alert (The BELL character).

\c Produce no further output after this.


\e The escape character; equivalent to pressing the escape key.
\f A form feed.

\r A carriage return.
\t A horizontal tab.
A vertical tab.
\v

Ex 3 : $echo -e Here\bthe\bspaces\bare\bbackspaced

Outputs the following text:


Output :Herthspacearbackspaced //one character before backslash is deleted.

Department of CSE, ATMECE, Mysuru 17


THE MAN COMMAND
The man command knowing more about Unix commands and using Unix online
manual pages
man is the system's manual viewer; it can be used to display manual pages, scroll up and down,
search for occurrences of specific text, and other useful functions.
Each argument given to man is normally the name of a program, utility or function. The manual
page associated with each of these arguments is then found and displayed. A section number,
if provided, will direct man to look only in that section of the manual. The default action is to
search in all of the available sections, following a pre-defined order and to show only the first

Department of CSE, ATMECE, Mysuru 17


page found, even if page exists in several sections.

have all sections, but the first three(NAME,SYNOPSIS and DESCRIPTION) are seen in all man pages.
NAME presents the online introduction to the command
SYNOPSIS shows the syntax used by the command
DESCRIPTION provides a detailed information.

man syntax
$man [option] command name

Options
-K,-- Search for text in all manual pages. This is a brute-force search, and is likely to take some time; if global- you can,
you should specify a section to reduce the number of pages that need to be searched. aproposSearch terms may be simple
strings (the default), or regular expressions if the --regex option is used

Section Numbers
The section numbers of the manual are listed below. While reading documentation, if you see
a command name followed by a number in parentheses, the number refers to one of these
sections. For example, man is the documentation of man found in section number 1. Some
commands may have documentation in more than one section, so the numbers after the
command name may direct you to the correct section to find a specific type of information. The
section numbers, and the topics they cover, are as follows:
Section number Description
Executable programs or shell commands
System calls (functions provided by the kernel)
Library calls (functions within program libraries)
Special files (usually found in /dev)
File formats and conventions eg /etc/passwd
Games
Miscellaneous (including macro packages and conventions),
e.g. man, groff
System administration commands (usually only for root)
Kernel routines [Non standard]

Department of CSE, ATMECE, Mysuru 17


searches the manual pages for a keyword or regular expression. Each manual page has a short descrip- tion
included with it. apropos searches these descriptions for instances of keyword.
$apropos find
aa_find_mountpoint (2) - find where the apparmor interface filesystem is mounted chkdupexe (1) - find
duplicate executables
ffs (3) - find first bit set in a word
The syntax for the ls command is:

Department of CSE, ATMECE, Mysuru 17


UNIX PROGRAMMING (18CS56)

Option Description
-a Displays all files.
-b Displays nonprinting characters in octal.
-c Displays files by file timestamp.
-C Displays files in a columnar format (default)
-d Displays only directories.
-f Interprets each name as a directory, not a file.
-F Flags filenames.
-g Displays the long format listing, but exclude the owner name.
-i Displays the inode for each file.
-l Displays the long format listing.
-L Displays the file or directory referenced by a symbolic link.
-m Displays the names as a comma-separated list.
-n Displays the long format listing, with GID and UID numbers.
-o Displays the long format listing, but excludes group name.
-p Displays directories with /
-q Displays all nonprinting characters as ?
-r Displays files in reverse order.
-R Displays subdirectories as well.
-t Displays newest files first. (based on timestamp)
-u Displays files by the file access time.
-x Displays files as rows across the screen.
To show long listing information about the
-1 Displays each entry on a line.
UNIX PROGRAMMING (18CS56)

a. Field 1: 1st Character File Type: First character specifies the type of the file.
In the example above the hyphen (-) in the 1st character indicates that this is a normal
file. Following are the possible file type options in the 1st character of the ls -l output.
Field Explanation normal file or d directory s socket file l link file

2nd to 9th character -- File Permissions: Next 9 character specifies the files permission.
Each 3 characters refers to the read, write, execute permissions for owner, group and other.

Field 7 File name: The last field is the name of the file.
h. Display- Directory Information Using ls -ld tent. But if you want the details
of direc- tory then you can use -d option as., For example, if you use ls -l /etc will display all the
files under etc directory. But, if you want to display the information about the /etc/ directory,
use -ld option as shown below.

$ ls -l /etc
total 3344
-rw-r--r-- 1 root root 15276 Oct 5 2004 a2ps.cfg
-rw-r--r-- 1 root root 2562 Oct 5 2004 a2ps-site.cfg
drwxr-xr-x 4 root root 4096 Feb 2 2007 acpi
-rw-r--r-- 1 root root 48 Feb 8 2008 adjtime
drwxr-xr-x 4 root root 4096 Feb 2 2007 alchemist

drwxr-xr-x 21 root root 4096 Jun 15 07:02 /etc


UNIX PROGRAMMING (18CS56)
FLEXIBILITY OF COMMAND USAGE COMBINING COMMANDS
UNIX allows you to specify more than one command in the command line. Each command has
to be separated from the other by a ; (semicolon).
Example
$wc note; ls -l note

tThe above command first it displays the line count, word cont and byte or character count along
with this it also display the details of note file.
When you learn to redirect the output of these commands you may even like to group them together
within parentheses .
Example :

The combined output of the two commands is now sent to the file newlist. Whitespace
is provided here only for better readability. You might reduce a few keystrokes like
this
$wc note;ls -l note)>newlist
When a command line contains a semicolon, the shell understands that the command on each
side of it needs to be processed separately.
The ; here is known as a metacharacter, and you'll come across several metacharacters that
have special meaning to the shell.
A command line can overflow or be split into multiple lines

A command is often keyed in. though the terminal width is restricted to 80 characters, that
doesn't prevent you from entering a command, or a sequence of them, in one line even though
the total width may exceed 80 characters. The command simply overflows to the next line
though it is still in a single logical line.
Sometimes, you'll find it necessary or desirable to split a long command line into multiple lines.
In that case, the shell issues a secondary prompt, usually >, to indicate to you that the
command line isn't complete. This is easily shown with the echo command:

MEANING OF INTERNAL AND EXTERNAL COMMANDS


UNIX commands are classified into two types Internal Commands - Ex: echo External
Commands - Ex: ls, cat
Internal Command:
Internal commands are something which is built into the shell. For the shell built in commands,
the execution speed is really high. It is because no process needs to be spawned for executing

Department of CSE, ATMECE, Mysuru 23


it.
For example, when using the "cd" command, no process is created. The current directory sim-
ply gets changed on

executing it.
External Command:
External commands are not built into the shell. These are executable present in a separate file.
When an external command has to be executed, a new process has to be spawned and the
command gets executed.
For example, when you execute the "cat" command, which usually is at /usr/bin, the
executable /usr/bin/cat gets executed.

How to find out whether a command is internal or external? type command:

For the internal commands, the type command will clearly say its shell built-in, however for the
external commands, it gives the path of the command from where it is executed.

THE TYPE COMMAND: knowing the type of a command and locating it.
type - Display information about command type.

The type command is a shell built-in that displays the kind of command the shell will
execute, given a particular command name. It works like this: type command where

$type type
Output: type is a shell built-in
$type ls

$type cp
Output: cp is /bin/cp
Here we see the results for three different commands. Notice that the one for ls (taken from a
--
utput from ls is displayed in color!

THE ROOT LOGIN

created but comes with every system. Its password is generally set at the time of installation of
the system and has to be used on logging in
Department of CSE, ATMECE, Mysuru 23
Login: root
Password: ******
The prompt of the root is # other users (non privileged user) either $ or %

Once you login as root


could be / or /root

BECOMING THE SUPER USER: SU COMMAND

Any user can acquire super user status with the su command if she knows the root password.
Example, the user GMIT becomes a super user in this way

$su
Password: ******
#pwd
/home/GMIT

Though the current directory does not change the # prompt indicates the GMIT now has
l.

: users often rush to the administrator with the complaint


that a pro- gram has stopped running. The administrator first tries running it in a simulated
environment.su, when used with a -

password route:

$su GMIT

This sequence .
Su runs a separate sub shell, so this mode is terminated by hitting [Ctrl-d] or using exit.

Department of CSE, ATMECE, Mysuru 23


UNIX FILES

UNIX system has thousands of files. If you write a program, you add one more file to the system.
When you compile it you add some more. Files grow rapidly, and if they are not organized
properly, you will find it difficult to locate them. So UNIX has a file system (UFS) to manage or
organizes its own files in directory.

NAMING FILES OR IN A (FILE) NAME


a. UNIX permits file names to use most characters, but avoid spaces, tabs and characters
that have a special meaning to the shell, such as:
& ; () | ? \ ' " ` [ ] { }< > $ - ! /
b. It is recommended that only the following characters be used in filenames.
c. Alphabetic characters and numerals
d. The period(.), the hyphen(-) and underscore(_)
e. Case Sensitivity: uppercase and lowercase are not the same! These are three different files:
NOVEMBER November november

f. Length: can be up to 256 characters

g. Extensions: may be used to identify types of files libc.a - archive, library file
program.c - C language source file alpha2.f - Fortran source file xwd2ps.o
- Object/executable code mygames.Z - Compressed file

BASIC FILE TYPES/CATEGORIES FILES


File is a collection of records. So, files are divided into three categories
a. Ordinary file
b. Directory file
c. Device file

The UNIX file system contains several different types of files:


a.Ordinary Files or regular files
It contains only data as a stream of characters. An ordinary files itself divided into 2 types
Text file: contains only printable characters, and you can often view the contents and make
sense out of them. All C and Java files are example of text file. A text file contains lines of
characters where every line is terminated with the newline character, also known as linefeed
(LF) when you press Enter while inserting
character normally, but there is
command (od) which can make it visible.
Binary file: it contains both printable and unprintable characters that cover the entire ASCII
range(0 to 255).most UNIX commands are example of binary files.

b. Directory files
 Contains no data, but keeps some details of the files and subdirectories that it contains.
 A directory file contains an entry for every file and sub directory that it houses. Each entry
has two components
 The filename
 A unique Identification number for the file or directory( called the inode number)
 A directory contains the filename but not the contents of file.
 When you create or remove a file the kernel automatically updates its corresponding

Department of CSE, ATMECE, Mysuru 25


directory by adding or removing the enter i.e inode number associated with that file.

c. Device files
 Used to represent a real physical device such as a printer, tape drive or terminal, used
for Input/Ouput (I/O) operations
 Unix considers any device attached to the system to be a file - including your terminal:
 By default, a command treats your terminal as the standard input file (stdin) from which
to read its input
 Your terminal is also treated as the standard output file (stdout) to which a command's
output is sent.
 Two types of I/O: character and block usually only found under directories named /dev

PARENT CHILD RELATIONSHIP/ ORGANIZATION OF FILES

All files in UNIX are related to one another. The file system in unix is a collection of all ordinary,
directory and device files and organized in a hierarchical structure as shown in below fig.

The implicit
all files.This feature
top of every
is called rootUNIX
& is file system is that
represented by there is a top
a /(front whichRoot
slash). serves as reference
is actually point for
a directory.
The root directory has a number of sub directories under it. These sub directories in turn have
more sub directories and others files under them.
For instance bin and usr are two directories directly under root, while a second bin and kumar
are sub directories under usr.
Every file apart from root must have a parent. Thus the home directory is the parent of kumar ,
while / is the parent of home and grandparent of kumar. If you create a file login.sql under the
kumar directory

Department of CSE, ATMECE, Mysuru 25


,then kumar will be the parent of this file.
The first group contains the files that are made available during system installation

/bin and /usr/bin: these are the directories where all the commonly used UNIX commands are
found.
/sbin and /usr/sbin: If can execute, it would be probably in one of these directories.
/etc: this directory contains the configuration files of the system. You can change a very
important aspect of system functioning by editing a text file in this directory. Your login name
and password are stored in files /etc/passwd and etc/shadow
/dev: This directory contains all device files.
be more sub directories like pts, dsk and rdsk in this directory
/lib and /usr/lib: Contains shared library files and sometimes other kernel-related files.
/usr and /include: contains the standard header files used by C programs. The statement
#include<stdio.h> used in most C programs referes to the file stdio.h in this directory.
/usr/share/man: this is where the man pages are stored. There are separate subdirectories
here(like man1,man2 etc) that contains the pages for each section. For instance, the man page
of ls can be found in /usr/share/man/man1
User also work with their own files, they write programs, send and receive mail and also create
temporary files. These files are available in the second group shown below
/tmp: the directory where users are allowed to create temporary files. These files are wiped
away regularly by the system
/var: The variable part of the file system. Contains all your print jobs and your outgoing and
incoming mail.
/home:On many systems users are housed here.Kumar would have his home directory in
/home/kumar

THE home directory and the HOME VARIABLE

HOME DIRECTORY : When log on to the system, UNIX automatically places you in a directory
called the home directory.
It is created by the system when user account is opened.
If you log in using the login name sharma , you will land up in a directory that could have the
pathname
/home/sharma
directory
$echo $HOME
/home/sharma
You will be doing much of your work in your home directory and subdirectories.
Home variable: it is also called environment variables. Environment variables are a set
of dynamic named values that can affect the way running processes will behave on a computer.
Here $HOME is a environment variable it indicates the home directory of the current user:
the default argument for the cd built-in command.

PATH VARIABLE:
The PATH environment variable is a colon-delimited list of directories that your shell searches
through when you enter a command.
Program files (executables) are kept in many different places on the Unix system. Your path tells
the Unix shell where to look on the system when you request a particular program.
To find out what your path is, at the Unix shell prompt echo $PATH Your path will look something
like the following.

Department of CSE, ATMECE, Mysuru 25


/usr2/username/bin:/usr/local/bin:/usr/bin:.
You will see your username in place of username. Using the above example path, if you enter
the ls command, your shell will look for the appropriate executable file in the following order:
first, it would look through the directory /usr2/username/bin, then /usr/local/bin, then /usr/bin,
and finally the local directory, indicated by the. (a period).

Department of CSE, ATMECE, Mysuru 25


RELATIVE AND ABSOLUTE PATHNAMES
THE dot(.) and double dots(..) notation to represent present and parent directories and
their usage in relative pathnames
RELATIVE PATHNAMES
directory. Uses either
the current or parent directory as reference and specifies path relative to it.
A relative pathname uses one of these cryptic symbols.
. (a single dot)- this represents the current directory.
.. (two dots) this represents the parent directory

Command Function
cd Returns you to your login directory
cd ~ Also returns you to your login directory
cd /

cd /root Takes you to the home directory of the root or


superuser,account created at installation, you must be root
user to access this directory.
cd /home home directory where user login directories are usually stored

cd .. Moves you up one directory


cd ~otheruser login directory
cd /dir/subdirfoo
Regardless of which directory you are in, the absolute path takes you
directly to subdirfoo, a subdirectory of dir.

Ex .1: Assume the current directory is /home/kumar/progs/data/text, using cd .. will move one
level up
$pwd
/home/kumar/progs/data/text
$ cd ..
$pwd
/home/kumar/progs/data

Ex 2 : To move two levels up


$pwd
/home/kumar/progs
$ cd ../..
$pwd
/home

Ex 3: My present location is /etc/samba and now I want to change directory to /etc. Using
relative path: $ cd ..
Using absolute path: $cd /etc

Ex 4: My present location is /var/ftp/ and I want to change the location to /var/log Using relative
path: cd ../log
Using absolute path: cd /var/log

Ex 5: My present location is /etc/lvm and I want to change my location to /opt/oradba Using

Department of CSE, ATMECE, Mysuru 33


relative path: cd ../../opt/oradba
Using absolute path: cd /opt/oradba

ABSOLUTE PATHNAMES:
If the first character of a pathname is / the files location must be determined with respect to
root(/)
. Such a pathname is called absolute pathname.

cat /home/kumar
When you have more than one / in a pathname for such / you have to descend one level in the
file system. Thus Kumar is one level below home and two levels below root.
When you specify a file y using frontslashes to demarcate the various levels,you have a
mechanism of identifying a file uniquely.No two files in a UNIX system can have same absolute
pathnames.
When you specify the date command, the system has to locate the file date from a list of
directories specified in the PATH variable and then execute it.
However if you know the location of a command in prior, for example date is usually located in
/bin or /usr/bin . Use absolute pathname i,e precede its name with complete path
$/bin/date
For example if you need to execute program less residing in /usr/local/bin you need to enter
the absolute pathname
$/usr/local/bin/less

DIRECTORY COMMANDS PWD, CD, MKDIR, RMDIR COMMANDS


pwd (PRINT WORKING DIRECTORY) (checking your current directory)
pwd the current working directory or simply the directory user
is, at present. It prints the current directory name with the complete path starting from root (/).
This command is built in shell command and is available on most of the shell bash, Bourne
shell, ksh,zsh, etc.
Basic syntax
$pwd [option]

Options Description
-L (logical) Use PWD from environment, even if it contains symbolic
links
-P (physical) Avoid all symbolic links
help Display this help and exit
version Output version information and exit

-L -P
-P

cd: CHANGING THE CURRENT DIRECTORY


The cd command, which stands for "change directory", changes the shell's current working
directory.
The cd command is one of the commands you will use the most at the command line in UNIX. It
allows you to change your working directory. You use it to move around within the hierarchy of
your file system.
Ex 1: When used with an argument it changes the current directory to the directory specified as
argument for example assume gmit is a directory under user directory Kumar.To change from
Kumar directory to gmit directory , issue the command as follows

Department of CSE, ATMECE, Mysuru 33


$pwd
/home/kumar
$cd gmit
$pwd
/home/kumar/gmit

Ex 2: When cd used without arguments: cd when used without arguments reverts to home
directory

$pwd
/home/kumar/gmit
$cd
cd without argument will change directory from gmit to its home directory Kumar
$pwd
/home/kumar

Ex 3: If your present working directory is /home/Kumar and you need to switch to /bin directory
directly, use absolute pathname i.e /bin wd cd command
$pwd
/home/kumar
$cd /bin
$pwd

mkdir: "making directory".


mkdir is used to create directories on a file system.
If the specified DIRECTORY does not already exist,mkdir creates it. More than one DIRECTORY
may be specified when calling mkdir.
mkdir syntax
mkdir [OPTION ...] DIRECTORY ...

Ex 1: To create a directory named gmit, issue the following command.


$mkdir gmit
gmit directory is created under present working directory.
Assume that pwd is /home/kumar , then gmit directory is created under kumar directory.

Ex 2: To create three directories at a time, named patch, dbs, doc, pass directory names as
argu- ments.
$mkdir patch dbs doc

Ex 3:To create a directory tree:


To create a directory named gmit and create two subdirectories named cse and ise under gmit,
issue the command. gmit is a parent directory.
$mkdir parent directory sub-directories
$mkdir gmit gmit/cse gmit/ise

Ex 4: Error while creating a directory tree


$mkdir gmit/cse gmit/ise
y
Error is due to the fact that the parent directory named gmit is not created before creating sub
directories cse and ise.

Ex 5: $mkdir test

Department of CSE, ATMECE, Mysuru 33


mkdir: Failed to make directory This can happen due to:
a. The directory named test may already exist
b. There may be an ordinary file by the same name in the current directory.
c. The permissions set for the current directory do not permit the creation of files and
directories by the user.

Department of CSE, ATMECE, Mysuru 33


rmdir: REMOVING DIRECTORIES
The rmdir utility removes the directory entry specified by each directory argument, provided
the directory is empty.
Ex 6.4.1: $rmdir progs removes the directory named progs
Arguments are processed in the order given. To remove both a parent directory and a
subdirectory of that parent, the subdirectory must be specified first, so the parent directory is
empty when rmdir tries to remove it.
The reverse logic of mkdir is applied.
$rmdir subdirectories parent directory
$rmdir gmit/cse gmit/ise gmit
You cant delete a directory with rmdir unless it is empty.In this example gmit directory cannot
be removed until the sub directories cse and ise are removed.
You cant remove a sub directory unless you are place in a directory which is hierarchically above
the one you have chosen to remove.

ls : listing directory contents:


To obtain a list of all filenames in the current directory.
Numerals first Uppercase next Lowercase Then

$ls Output:08_packets.html
calendar dept.lst emp.lst helpdir uskdsk06

ls options:
Output in multiple columns(-x):
$ls -x
08_packets.html calendar dept.lst emp.lst helpdirprogs usdsk07 usdsk07

Identifying directories and executables(-F)


$ ls Fx

08_packets.html calendar* cptodos.sh* dept.lst


emp.lst helpdir / progs / usdsk07
The * indicates the file contains the executable code and / refers to directory Showing
Hidden files also(-a):
$ls axF
.profile .exrc .kshrc .xinitrc 08_packets.html calendar*
cptodos.sh* dept.lst emp.lst
helpdir / progs / usdsk07
The hidden files are indicated by . (dot) displayed before filename.
Listing directory contents:

$ls x helpdir progs


helpdir:
forms.obd graphics.obd progs:
array.pl n2words.pl

If we specify two directories named helpdir and progs , the contents of the directory i,e
filenames are listed out.
Recursive listing(-R)
The recursive option lists all sub-directories and files in a directory tree structure.
$ls -xR

forms.hlp graphics.hlp
./progs
arrays.pl n2words.pl

08_packets.htm calendar cptodos.sh dept.lst


l
emp.lst helpdir progs usdsk07
./helpdir
FILE RELATED COMMANDS

Cat command : Displaying and creating files


cat is one of the most well known commands of UNIX system. Cat is useful for creating a file .
Its mainly used to display the contents of a small file on the terminal.

Using cat to create a file:


Enter the command cat, followed by >(right chevron) character and the filename. Example: take
a filename named foo
$ cat > foo
> Symbol following command means that the output goes to filename following it. [ctrl+d] /* to
terminate or to signify end of the input.
$
Using cat to display a file
Enter the cat command followed by filename
$ cat foo
Symbol following command means that the output goes to filename following it.

Cat options (-v and -n)


Displaying Non printing characters(-v)
cat is normally used for displaying text files only. If you have non-printing ASCII characters in
your input , you can see cat with -v option to display these characters
Numbering lines(-n)
The -n option numbers lines.
Cat with more than one filename as arguments:
cat filename1 filename2 .... cat chap01 chap02
The contents of second file are displayed immediately after the first file without any header
information.
cp: copying a file
cp command copies a file or a group of files.it creates an exact image of the file on the disk with
the different name.
The syntax requires atleast two filenames to be specified in the command line. When both are
ordinary files, the first is copied to second file.
cp source file destination file

cp chap01 unit1
if destination file i.e unit1 does not exist, first it will be created before copying.if not it will be
simply overwritten without any warning.
Copying a file to another directory

ex: assume there is a file named chap01 and it has to be copied to progs directory
cp chap01 progs
output: chap01 is now copied to directory named progs with the same name chap01.
Copying a file to another directory with different name
ex: assume there is a file named chap01 and it has to be copied to progs directory with chap01
file renamed as unit1
cp chap01 progs/unit1
output: chap01 is now copied to directory named progs with the same name unit1
Copy more than one file with a single command.
cp chap01 chap02 chap03 progs
chap01, chap02, chap03 files are copied to directory named progs.
Copy all files beginning with chap
cp chap* progs

cp options:
Interactive copying (-i): the -i option warns the user before overwriting the destination file.
Ex: $ cp -i chap01 unit1
cp: overwrite unit1(yes/no)? y
A y at this prompt will overwrite the file.

Copying directory structure(-R) : the -R command behaves recursively to copy an entire


directory structure say progs to newprogs.
Ex say progs directory contains three files kernel, bash, korn. To copy all three files under progs
to newprogs directory
$ cp -R progs newprogs
rm : deleting files
The rm command deletes one or more files.
Ex 1: The following command deletes three files chap01, chap02, chap03.
$ rm chap01 chap02 chap03
Ex 2: to delete files named chap01 and chap02 under progs directory
$ rm progs/chap01 progs/chap02
Ex 3: to remove all file
$ rm*
rm options:
Interactive deletion (-i): the i optin makes the command ask the user for confirmation before
removing each file.
$rm -i chap01 chap02 chap03
rm: remove chap01(yes/no)?y rm: remove chap01(yes/no)?y rm: remove chap01(yes/no)?y
Recursive deletion(-r or -R) deletes all subdirectories and files recursively. Rm wont normally
remove directories but when used with -r or -R option it will.
$ rm -r *
Forcing removal: rm prompts for removal, if a file is write protected. The -f option overrides this
minor protection and forces removal.
$ rm -rf * /*(deletes everything in the current directory and below)

mv: RENAMING FILES.


The mv command renames or moves files. It has two distinct functions:
h.It renames a file or directory
i. it moves a group of files to a different directory

To rename a file chap01 to man01

$ mv chap01 man01
mv replace the filename in the existing directory entry with the new name. No additional space
is consumed on disk during renaming.

To rename a directory:
$ mv pts perdir
pts directory is renamed as perdir

To move group of files to a directory mv chap01 chap02 chap03 progs


to move three files chap01, chap02, chap03 to directory named progs

wc command: COUNTING LINES,WORDS,CHARACTERS


wc command takes one or more filenames as arguments and displays four columnar output.
First we will create a file named infile
$ cat > infile
I am the wc command
I count characters,words and lines [ctrl+D]

$wc infile
2 10 55 infile
wc counts lines in first column ,words in second column,characters in third column and filename
in fourth column..
A line is any group of characters not containing a newline
A word is group of characters not containing a space tab or newline.
A character is the smallest unit of information and includes a space, tab and newline wc options:
$ wc -l infile 2
$wc -w infile 10
$wc -c infile 55
The -b option displays the octal values for each character.
When two filenames are passed as wc argument

First line : number of lines, words and characters of chap01


Second line: number of lines, words and characters of chap02
Third line: Total number of lines, words and characters of both.
od Command: DISPLAYING DATA IN OCTAL.
$ cat odfile
White space includes a
The ^G character rings a bell
$ od -b odfile

Each line displays 16 bytes of data in octal , preceded by the offset in the file of the
first byte in the line.

000000 127 150 151 164 145 040 163 160 141 143 145 040 151 156 143 154
000000 165 144 145 163 040 141 040 011 012 124 150 145 040 007 040 143
FILE ATTRIBUTES AND PERMISSIONS
The ls command with options
ls l: LISTING FILE ATTRIBUTES
ls command is used to obtain a list of all filenames in the current directory. The output in UNIX
lingo is often referred to as the listing. Sometimes we combine this option with other options for
displaying other attributes, or ordering the list in a different sequence. ls look up the inode
to fetch its attributes. It lists seven attributes of all files in the current directory and they are:
File type and Permissions
The file type and its permissions: The first column shows the type and permissions
associated with each file.The first character in this column is mostly a which indicates that the
file is an ordinary one. In unix, file system has three types of permissions- read, write and
execute.

Links: The second column indicates the number of links associated with the file. This is actually
the number of filenames maintained by the system of that file.

Ownership: The third column shows the owner of files. The owner has full authority to tamper
with files content and permissions. Similarly, you can create, modify or remove files in a
directory if you are the owner of the directory.

Group ownership: The fourth column represents the group owner of the file. When opening a
user account, the system admin also assigns the user to some group. The concept of a group of
users also owning a file has acquired importance today as group members often need to work on
the same file.

File size: The fifth column shows the size of the file in bytes. The important thing to remember
here is that it only a character count of the file and not a measure of the disk space that it
occupies.

Last modification time: The sixth, seventh and eighth columns indicate the last modification
time of the file, which is stored to the nearest second. A file is said to be modified only if its
content have changed in any way.If

Filename: The last column displays the filename arranged in ASCII collating sequence.
For example, $ ls l
total 72

-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01


-rw-r--r-- 1 kumar metal 4174 may 10 15:01 chap02
-rw-rw-rw- 1 kumar metal 84 feb 12 12:30 dept.lst
-rw-r--r-- 1 kumar metal 9156 mar 12 1999 genie.sh
drwxr-xr-x 2 kumar metal 512 may 09 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 09 09:57 progs

FILE PERMISSIONS
UNIX has a simple and well defined system of assigning permissions to files. Lets issue the ls l
command once again to view the permissions of a few lines .

Consider the first column.


- rwx r-x r
Each group here represents the category and contain three slots representing the read, write
and execute permissions of the file.
r indicates the read permission; w indicates write permission; x indicates execute permission
- (hyphen) indicates the absence of the corresponding permission.
In the above example, the file permissions of chap02 file is
-rwx r-x r - -File owner/user groupothers
First group(rwx) has all the three permissions.
The file is readable, writable and executable by the owner of the file, kumar. The third column
shows the owner of the file.
The first permissions group applies to kumar.
You have to login with the name kumar for the privileges to apply to you.
Second group(r-x):
has a hyphen in the middle slot, which indicates the absence of write permissions by the group
owner of the file.
The group owner is metal and all users belonging to group metal has only read and execute per-
missions.
Third group(r--):
has the write and execute bits absent.
This set is applicable to others i,e those who are neither the owner nor group. This category is
referred to as the world.

CHANGING FILE PERMISSIONS: RELATIVE AND ABSOLUTE PERMISSIONS


chmod command.
A file or a directory is created with a default set of permissions, which can be determined by
umask. Let us assume that the file permission for the created file is -rw-r--r--. Using chmod
command, we can change the file permissions and allow the owner to execute his file. The
command can be used in two ways:
In a relative manner by specifying the changes to the current permissions In an
absolute manner by specifying the final permissions

RELATIVE PERMISSIONS
chmod only changes the permissions specified in the command line and leaves the other
permissions unchanged.
Its syntax is:
chmod category operation permission filename(s)
chmod takes an expression as its argument which contains: user category (user, group, others)
operation to be performed (assign or remove a permission) type of permission (read, write,
execute)

The below shows the abbreviations used by chmod command


Category operation permission
u - user + assign r - read
g group - remove w - write
o - others = absolute x - execute
a - all (ugo)

Ex 1:
$ls l xstart
-rw-r--r-- 1 kumar metal 1906 sep 23:38 xstart Here user is having the only read and
execute permission .

Using relative file permission need to add the execute permission to user
chmod category operation(+,-) permission filename.
$chmod u + x xstart
$chmod u+x xstart

$ ls l xstart
-rwxr--r-- 1 kumar metal 1906 sep 23:38 xstart
After executing the chmod command, the command assigns (+) execute (x) permission to the
user (u), other permissions remain unchanged.

Ex 2: To remove execute permission from all and assign read permission to group and others
$chmod a-x, go+r xstart /*to remove execute permission from all(a)ie user, group, others
/*to assign read permission to group and others (go+r)

Ex 3: To assign write and execute permissions for others.


$chmod o+wx xstart

ABSOLUTE PERMISSIONS

A string of three octal digits is used as an expression. The permission can be represented by
one octal digit for each category. For each category, we add octal digits. If we represent the
permissions of each category by one octal digit, this is how the permission can be represented:
Read permission 4 (octal 100)
Write permission 2 (octal 010)
Execute permission 1 (octal 001)

Octal Permissions Significance


0 --- no permissions
1 --x execute only
2 -w- write only
3 -wx write and execute
4 r-- read only
5 r-x read and execute
6 rw- read and write
7 rwx read, write and execute

We have three categories and three permissions for each category, so three octal digits can
describe a
others. chmod can use this three-digit string as the expression.

Ex 1: To assign read,write permissions to all .Using relative permission, we have,


$chmod a+rw xstart
Using absolute permission, we have,
$chmod 666 xstart /* 6 for r-w
/* first digit 6 for user, second 6 for group and third 6 for others

Ex 2:
To assign read and write for user and remove write, execute permissions from group and others
Here to assign rw- corresponds to digit 6
Remove write , execute permissions is nothing but assigning only read option to group and oth-
ers
Only read permission is r corresponds to 4
$chmod 644 xstart

Ex 3:
To assign all permissions to the owner, read and write to group and only execute for others.
$chmod 761 xstart

Ex 4
To assign all permissions to all categories.
$chmod 777 xstart

5.2 The Security Implications


Let the default permission for the file xstart is
-rw-r r--
$chmod u-rw, go-r xstart or
$chmod 000 xstart
After Executing above any one command the output will be removes the all permission from all
categories as shown below
----------
This is simply useless but still the user can delete this file On the other hand,
chmod a+rwx xstart or
chmod 777 xstart
After Executing either of the one command it adds the all permission to all categories as shown
below
-rwxrwxrwx

The UNIX system by default, never allows this situation as you can never have a secure system.
Hence, directory permissions also play a very vital role here.

RECURSIVELY CHANGING FILE PERMISSIONS


use chmod Recursively.(-R)
It possible to make chmod descend directory hierarchy and apply the expression to every file
and sub directory it finds. This is done by using -R option
$chmod -R a+x shell_scripts

This makes all the files and subdirectories found in the shell_scripts directory, executable by all
users.

DIRECTORY PERMISSIONS
Directories also have their own permissions and the significance of these permissions differ from
those of ordinary files.
The default permissions of a directory are,
rwxr-xr-x (755)
A directory must never be writable by group and others
Ex1:
$mkdir c_progs ; ls ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs
Here the c_progs directory is created (mkdir c_progs) and then the attributes of directory is
listed out(ls ld c_progs)

If a directory has write permission for group and others also, be assured that every user can
remove every file in the directory. As a rule, you must not make directories universally writable
unless you have definite reasons to do so.

CHANGING FILE OWNERSHIP


There are two commands meant to change the ownership of a file or directory.
chown changing file owner and
chgrp changing group owner
chown : Changing file owner
The syntax is
chown options owner [:group] file(s)
For changing ownership requires super user permission. So first change our status to that
of super user with the su command:
$su
Password: ****** #_
After the password is successfully entered, su returns a # prompt, the prompt used by root.
su
lets us acquire super user status if we know the root password.
Consider the file note owned by kumar.To now renounce the ownership of the file note to Shar-
ma, use chown in the following way:
# ls -l note
-rwxr----x 1 kumar metal 347 may 10 20:30note

#chown sharma note; ls -l note


-rwxr----x 1 sharma metal 347 may 10 20:30 note
Once ownership of the file has been given away to sharma, the user file permissions that
previously applied to Kumar now apply to sharma. Thus, Kumar can no longer edit note since
there is no write privilege for group and others. He cannot get back the ownership either. But he
can copy the file to his own directory, in which case he becomes the owner of the copy.

Department of CSE, ATMECE, Mysuru 47


chgrp :Changing Group Owner
This command changes the group owner. No superuser permission is required.
$ ls l dept.lst
-rw-r--r-- 1 kumar metal 139 jun 8 16:43 dept.lst Here the group owner of dept.lst is
metal

$chgrp dba dept.lst; ls l dept.lst


-rw-r--r-- 1 kumar dba 139 jun 8 16:43 dept.lst

The group owner of the file dept.lst is changed from metal to dba by issuing the command
$chgrp dba dept.lst

The file attributes of dept.lst is listed by issuing the command


$ls l dept.lst

Using chown to change both file owner and group : The syntax requires two arguments to
be separated by :
Chown sharma: dba dept.lst
Here the ownership of dept.lst is changed to sharma and group to dba

vikas pts/14 aug 1 09:51 (:0)

$who | wc -l
Output: 5 /* count of number of lines of who command
Here the output of who command has been passed directly as the input to wc command
and who is said to be piped to wc.

The grep,egrep

grep(globally search regular expression and print)


Unix has a special family of commands for handling search requirements and the principal
member of the family is the grep command.
grep scans its input for a pattern and displays lines containing the pattern, the line numbers or
filename where the pattern occurs.
Syntax:
grep options pattern filename(s)
grep searches for pattern in one or more filename or the standard input if no filename is
specified. The first argument(barring the options) is the pattern and the remaining arguments
are filenames.

Consider three files emp.lst, emp1.lst, emp2.lst


$cat emp.lst

Department of CSE, ATMECE, Mysuru 58


$cat emp2.lst

Ex 1: Now to search the pattern sales in emp.lst file

Ex 2: To search the pattern director from 2 files i,e emp.lst and emp2.lst

grep along with options

Ignoring case: when you look for a name but are not sure of the case, use the i option to
ignore case for pattern matching.

Department of CSE, ATMECE, Mysuru 62


Deleting lines(-v): The v option selects all lines except those containing the pattern

The lines containing the pattern director are deleted in the output.
Dislaying line numbers(-n).
The n option displays the line numbers containing the pattern along with the line

Counting lines containing patter(-c)


The c option counts the number of lines containing the pattern.
4

Dislaying filenames(-l)
The l option displays only the names of the files containing the pattern. Here the pattern
manager is searched in all files ending with .lst (*.lst)

Matching multiple patterns(-e)


By using e option, you can match multiple patterns

BASIC AND EXTENDED REGULAR EXPRESSIONS


TYPICAL EXAMPLES INVOLVING DIFFERENT REGULAR EXPRESSIONS

BASIC REGULAR EXPRESSIONS

Department of CSE, ATMECE, Mysuru 62


The character class []
A regular expression lets you specify a group of characters enclosed within a pair of rectangle
brackets [ ], in which the match is performed for a single character in the group.
Thus the expression
[aA] Matches either a or A

Negating a class(^)
Regular expressions use the caret(^) to negate the character class, while the shell uses
bang(!) Ex:
[^a-zA-Z] matches a non-alphabetic character

The *(asterisk)
The * refers to the immediately preceding character.Here it indicates that the previous character
can occur many times or not at all.

The patttern g*
Matches none, g, gg, gg, ggg,.....

The dot ( .)
A . matches a single character where as the shell uses ? to indicate that.

Here the . matches single character. It list all files beginning with 10 followed by single
character. It displays lines with id 101,102,103,104,105.

7.1.1 Specifying pattern locations(^ and $)


The two regular expressions characters that match pattern at the beginning or end of line .
^(caret) Matching at the beginning of the line
$(dollar) Matching at the end of the line

Department of CSE, ATMECE, Mysuru 62

^3 matches all the lines beginning with digit 3.


5...$ matches all the lines ending with four digit number beginning with 5.

Department of CSE, ATMECE, Mysuru 62


EXTENDED REGULAR EXPRESSIONS(ERE)
ERE make it possible to match dissimilar patterns with a single expression. The ERE
has to be used with E option.

The ? symbol matches zero or one occurrences of character c i,e 0 c , 1 c.


The other two lines occurrences (2 c) and occurrences (3 c) are not matched .

Matching multiple patterns( |, ( and ) )


The | is the delimiter for the multiple patterns.
Consider the file ere.lst with two lines of employee details.

The + and ?
+ Matches one or more occurrences of the previous character
? Matches zero or one occurrences of the previous character.

The + symbol matches one or more occurrences of character c i,e


The occurrences of a character is not matched by +c , since there is no c in the occurrences.
c, cc, ccc

To locate both sengupta and dasgupta from file ere.lst


Department of CSE, ATMECE, Mysuru 62
The characters ( and ) lets you group patterns and use of | inside the parenthesis,
you can frame more compact pattern

Note—Next concept is shell programming

Department of CSE, ATMECE, Mysuru 62

You might also like