UNIT1UNIX
UNIT1UNIX
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.
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.
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.
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
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:
-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
$who -uH
Displays "all" information, and headers above each column of data, for example:
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.
To display the diagnostic messages on the terminal or to issue prompts for taking user input
To evaluate shell variable.
If you specify the -e option, the following escape sequences are recognized:
\r A carriage return.
\t A horizontal tab.
A vertical tab.
\v
Ex 3 : $echo -e Here\bthe\bspaces\bare\bbackspaced
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]
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
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:
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.
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!
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 %
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.
password route:
$su GMIT
This sequence .
Su runs a separate sub shell, so this mode is terminated by hitting [Ctrl-d] or using exit.
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.
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
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
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
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
/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
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.
Command Function
cd Returns you to your login directory
cd ~ Also returns you to your login directory
cd /
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 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
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
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
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
Ex 2: To create three directories at a time, named patch, dbs, doc, pass directory names as
argu- ments.
$mkdir patch dbs doc
Ex 5: $mkdir test
$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
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
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.
$ 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
$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
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
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 .
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)
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)
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)
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 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
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.
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.
The group owner of the file dept.lst is changed from metal to dba by issuing the command
$chgrp dba 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
$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
Ex 2: To search the pattern director from 2 files i,e emp.lst and emp2.lst
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.
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
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)
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.
The + and ?
+ Matches one or more occurrences of the previous character
? Matches zero or one occurrences of the previous character.