Unix Module 1
Unix Module 1
Unix Module 1
Module - I
1. 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.
2. 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,
who was a researcher at Bell Labs, to have a go at writing a simpler operating system himself. 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.
3. UNIX ARCHITECTURE
3.1 Kernel: is the core of operating system. A collection of routines mostly written in C.
It is loaded into memory when the system is booted and communicates directly with the hardware. The
kernel manages system memory, processes, decides priorities.
3.2 Shell: interface between Kernel and User. 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.
3.3 Files and Process: file is an array of bytes and it contain virtually anything. Unix considers even
the directories and devices as members of file system. The dominant file type is text and behavior of
system is mainly controlled by text files.
The second entity is the process, which is the name given to a file when it is executed as a program.
Process is simply a time image of an executable file.
3.4 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.
4. 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.
Sandeepa G S, Asst Prof, Page 2
Dept of CSE, GMIT
UNIX AND SHELL PROGRAMMING
In a time sharing environment, many users are connected to one or more computers. The output devices
(such as printers) and auxiliary storage devices (such as disks)are shared by all users .In this
environment all of the computing must be done by the central computer. The central computer has
several responsibilities
All of this work tends to keep the central computer busy so, user has to wait more time for get done
their work so, it is nonproductive because of slow response.
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.
6.UNIX STRUCTURE
Unix consists of four major components: the kernel, the shell, a standard set of utilities and application
programs.
USERS
SHELL
KERNEL
HARDWARE
Sandeepa G S, Asst Prof, Page 4
Dept of CSE, GMIT
UNIX AND SHELL PROGRAMMING
6.1 Kernel: is the heart of UNIX system. It contains two basic parts of the OS: process control and
resource management. All other components of the system call on the kernel to perform these services
for them.
6.2 Shell: interface between Kernel and User. 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. Shell has two
major parts.
a. Interpreter: reads your commands and works with the kernel to execute them.
b. Shell Programming: is a programming capability that allows you to write a shell scripts.
A shell script is a file that contains the shell commands that perform a useful function. It is also known
as shell program.
6.3 Utilities: A utility is a standard Unix program that provides a support for users. Three common
utilities are text editors, search programs and sort programs.
6.4 Applications: are programs that are not a standard part of UNIX. Written by system administrator’s
professional programmers or users they provide an extended capability to the system.
8. ACCESSING UNIX
There are three methods for accessing the unix
User ID
Passwords
Interactive session
8.1 User ID when you work with your own computer at home, you do not need to log in or be too
concerned about who uses the system. When you work in a UNIX environment, security and user
control becomes major concerns. Permission comes in the form of account created by sytem admin.
you and your account are identified by a special code known as User id
8.2 Passwords: A password is a word or string of characters used for user authentication to prove
identity or access approval to gain access to a resource (example: an access code is a type of password),
which is to be kept secret from those not allowed access.
A good password has atleast 6 characters.
It should contain both upper and lower case letters along with atleast one digit and special character.
8.3 Interactive session: contains three steps login , Interaction and logout.
8.3.1 Login
The details of Login process varies from system to system. There is a general pattern to the steps listed
below.
a. you must make contact with the system.
If you are working on a local network, always connected to remote server starting the login process is
simple as selecting option in a menu.
If you are making the connection from a remote location such as from home to work then you need o
use special software for connection such as Telnet.
b. wait for the system login prompt.
login:
c.Type User Id
Sandeepa G S, Asst Prof, Page 6
Dept of CSE, GMIT
UNIX AND SHELL PROGRAMMING
user id:
d. Type Password.
Password:
8.3.2 Interaction:
Once you connect to server you can enter commands that allow you to work with the computer.Tyical
commands allow you to work with the files-edit copy, sort, process data and print files;send and receive
mails etc..
8.3.3 Logout:
Its important to log out when you are done with your work .There are several reasons. First it frees
system resources for others who may need to use them.Second it is a security concern.
Typical User Session:
login:gilberg
password:***********
$ ls
File1 file 2…….
$logout
9. 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 [-option(s)] [argument(s)]
9.1 Verb: is the command name. The command indicates what action is to be taken. This action
concept gives us the name verb for action .
9.2 option: modifies how the action is applied.
9.3 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
10. UNDERSTANDING OF SOME BASIC COMMANDS SUCH AS echo, printf, ls, who,
date, passwd, cal
10.1 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
$date -u
Format Explanation
code
$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 syntax
passwd [OPTION] [USER]
$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:
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.
If you specify the -e option, the following escape sequences are recognized:
$x=10
$echo “The value of x is $x”
Entering these two commands will output the following text:
Output :The value of x is 10.
Ex 3:
$echo -e “Here\bthe\bspaces\bare\bbackspaced”
Outputs the following text:
Output :Herthspacearbackspaced //one character before backslash is deleted.
Ex 4:
$echo –e “Unix \n is \n a \n os”
Output :$Unix
is
a
Os //prints in a new line
Ex 5:
$echo –e “unix \t is \t a \t os”
Output :unix is a os //\t –prints after 8 spaces
Ex 6:
$echo –e “unix \r is a os”
Output : is a os //moving cursor to beginning of line.
//words before \r is deleted.
Ex 7:
$echo –e “unix \c is a os”
Output :unix // cursor in same line.
//words after \c deleted.
Ex 8:
$echo $SHELL
Output :/bin/bash //prints the shell name
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 page found, even if page ex-
ists in several sections.
A man page is divided into a number of compulsory optional sections. Every command doesn’t 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.
apropos Search 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 com-
mand name followed by a number in parentheses, the number refers to one of these sections. For exam-
ple, man is the documentation of man found in section number 1. Some commands may have docu-
mentation in more than one section, so the numbers after the command name may direct you to the cor-
rect section to find a specific type of information.
The section numbers, and the topics they cover, are as follows:
Section number Description
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions), e.g. man, groff
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
Man Examples
$man man
View the manual page for the man command.
$man –s4 passwd
This displays the documentation for a configuration file from the section 4. Even this information is
present in the section 1 it won’t display section 1 information.
$whatis cal
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
11. ls COMMAND
The ls command lists all files in the directory that match the name. If name is left blank, it will list all
of the files in the directory.
Syntax
The syntax for the ls command is:
ls [options] [names]
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.
Option Description
-x Displays files as rows across the screen.
-1 Displays each entry on a line.
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
d directory
s socket file
l link file
2 to 9th character -- File Permissions: Next 9 character specifies the files permission. Each 3
nd
characters refers to the read, write, execute permissions for owner, group and other.
b.Field 2 – Number of links: Second field specifies the number of links for that file. In this example, 1
indicates only one link to this file ecos.c and 2 links to directory named fedora
c.Field 3 – Owner: Third field specifies owner of the file. In this example, ecos.c file is owned by
username ‘vizion’.
d. Field 4 – Group: Fourth field specifies the group of the file. In this example, the file ecos.c belongs
to ”vizion’ group.
e. Field 5 – Size: Fifth field specifies the size of file. In this example, ‘1129’ indicates the ecos.c file
size.
f. Field 6 – Last modified date & time: Sixth field specifies the date and time of the last modification
of the file.
Sandeepa G S, Asst Prof, Page 18
Dept of CSE, GMIT
UNIX AND SHELL PROGRAMMING
g.Field 7 – File name: The last field is the name of the file.
Display Directory Information Using ls -ld
When you use “ls -l” you will get the details of directories content. 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
$ ls -ld /etc
drwxr-xr-x 21 root root 4096 Jun 15 07:02 /etc
Example :
$(wc note ; ls -l note) > newlist
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 metacharaters 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 stil 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:
$echo “this is
> a three-line
>text message”
output:
this is
a three-line
text message
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 exe-
cuted.
For example, when you execute the "cat" command, which usually is at /usr/bin, the
executable /usr/bin/cat gets executed.
type command:
$ type cd
cd is a shell builtin
$ type cat
cat is /bin/cat
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.
Sandeepa G S, Asst Prof, Page 20
Dept of CSE, GMIT
UNIX AND SHELL PROGRAMMING
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 “command” is the name of the command you want to examine. Here are some examples:
$type type
Output: type is a shell built-in
$type ls
Output: ls is aliased to ‘ls –color=tty’
$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 Fedora
system) and how the ls command is actually an alias for the ls command with the “-- color=tty” option
added. Now we know why the output from ls is displayed in color!
$more ecos.c
-- more –(39%) prompt is seen at bottom left corner of the screen. At this prompt you can press a key to
perform navigation.
$tty
output is /dev/pts/10
The terminal filename is 10 resident in the pts directory. This directory in turns is under the /dev direc-
tory. You can use tty in a shell script to control the behavior of the script depending on the terminal it
is invoked from.
Syntax
script options arguments
Used to record an interactive session ,when you want to start recording key the command as
script . $script
Example:
$uname
or
$uname –s
Output: Linux
$uname –n
Output: localhost.localdomain
$uname –r
Output:2.6.23.1-42.fc8
$uname –a
Output: Linux localhost.localdomain2.6.23.1-42.fc8
Terminals and keyboards have no uniform behavioral pattern. Terminal settings directly impact key-
board operation.
Backspacing Doesn’t Work: consider that you misspelled passwd as password, and when you pressed
the backspace key to erase the last three characters, you saw this
$password^H^H^H.
Backspacing is not working here; that’s why you see the symbol ^H every time you press the key.
Killing a Line: if the command line contains many mistakes, you could prefer to kill the line altogether
without executing it. In that case, use [Ctrl-u].
The line-kill character erases everything in the line and returns the cursor to the beginning of the line.
Interrupting a Command: sometimes, a program goes on running for an hour and doesnot seem to
complete. You can interrupt the program and bring back the prompt by using either of the two sequence
[Ctrl-c] or [Delete]
Creating a user’s Environment: 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 -, recreates the user’s environment without taking the login password route:
$su – GMIT
This sequences executes GMIT’s .profile and temporarily creates GMIT’s environment. Su runs a
separate sub shell, so this mode is terminated by hitting [Ctrl-d] or using exit.
When opening a user account, you have to associate the user with a group. A group usually has more
than one member with a different set of privileges. People working on a common project should be
able to read one another’s files, which is possible only if they belong to same group.
Creating a user involves defining the following parameters
A user identification number(UID) and username.
A group identification number (GID) and group name.
The home directory
The login shell
The mailbox in /var/mail
The password
Most of these parameters are found in a single line identification the user in /etc/passwd. We will now
create a group for a user and then add that user to the system
-g, The numerical value of the group's ID. This value must be unique, unless the -o option is
--gid GI used. The value must be non-negative. The default is to use the smallest ID value greater
D than or equal to GID_MIN and greater than every other group.
See also the -r option and the GID_MAX description.
Note: For this command to work you must have superuser rights or be logged in as root.
#groupadd gmit
The above example would create a new group called "GMIT". This new group could then have users
added to it using the useradd command.
#groupadd –g 241 gmit
The above example creates a new group, gmit with a GID of 241.
The command places this entry in /etc/group. Once an entry for the group has been made, you are now
ready to add a user of tis group to the system.
Note: For these commands to work you must have superuser rights or be logged in as root.
useradd newperson
Creates newperson as a new user. Once the new user has been added, you would need to use
the passwd command to assign a password to the account.
Once a user has been created, you can modify any of the user settings, such as the user's home directo-
ry, using the usermod command.
Users are removed from the system with userdel. The following command removes the user kumar
from the system.
$userdel kumar