0% found this document useful (0 votes)
14 views34 pages

Introduction to UNIX Programming

This document provides an introduction to UNIX programming, detailing the operating system's features, history, architecture, and command structure. It explains the roles of the kernel and shell, the concept of files and processes, and the multiuser and multitasking capabilities of UNIX. Additionally, it covers the UNIX environment, structure, and standards such as POSIX and the Single UNIX Specification.

Uploaded by

vishwascgowda03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views34 pages

Introduction to UNIX Programming

This document provides an introduction to UNIX programming, detailing the operating system's features, history, architecture, and command structure. It explains the roles of the kernel and shell, the concept of files and processes, and the multiuser and multitasking capabilities of UNIX. Additionally, it covers the UNIX environment, structure, and standards such as POSIX and the Single UNIX Specification.

Uploaded by

vishwascgowda03
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIX PROGRAMMING (18CS56) MODULE - 1

UNIX PROGRAMMING (18CS56)

MODULE – 1

INTRODUCTION

THE OPERATING SYSTEM:

An operating system is a software that manages computer hardware and provides a


convenient and safe environment for running programs. It acts as an interface between
programs and hardware resources that these programs access. It is loaded into memory
when a computer is booted and remains active as long as machine is on.

Key features of an operating system:

 Operating system allocates memory for the programs and loads the program to
the allocated memory.
 Operating system loads the CPU registers with control information related to the
program.
 The instructions provided in the program are executed by the CPU. The operating
system keeps track of the instruction that was last executed. This enables it to
resume a program if it had to be taken out of the CPU before it completed
execution.
 If program need to access the hardware, it makes a call to the operating system
rather attempt to do the job itself.
 After the program has completed execution the operating system cleans up the
memory and registers and makes them available for the next program.
 Modern operating system are MULTI-PROGRAMMING. They allow multiple
programs to be in memory.
 The operating system creates a process for each program and then control the
switching of these processes. Eg for operating system: - DOS, WINDOWS, UNIX
etc.

THE UNIX OPERATING SYSTEM

Unix (trademarked as UNIX) is a family of multitasking, multiuser computer operating


systems that derive from the original AT&T Unix, developed in the 1970s at the Bell Labs
research center by Ken Thompson, Dennis Ritchie, and others. Unix is an operating
system.

Users can interact with Unix system through a command interpreter called shell. It
achieves unusual tasks with a few keystrokes.

Unix operating system doesn’t tell whether user is right or wrong and doesn’t warn the
consequences of their actions.

Page | 1
UNIX PROGRAMMING (18CS56) MODULE - 1

HISTORY OF UNIX OPERATING SYSTEM

Unix is actually a very old operating system, until Unix came on the scene, operating
systems were designed with a particular machine in mind. They were written in low
level language. Programs designed for one system simply wouldn’t run on another, so
two persons named Ken Thompson and Dennis Ritchie, of AT & T, in 1969, started to
build a flexible operating system that would run on any hardware. they designed and
built an elegant file system, command interpreter(shell) and a set of utilities. But this
system was once again hardware dependent.

In 1973, Dennis Ritchie rewrote the entire system in C –a high level language and
it was portable. This was the first version of Unix.

The University of California Berkley(UCB), created a Unix of its own. They called
it BSD UNIX (Berkley software distribution). These versions became quite popular
worldwide. BSD Unix had a standard editor of the Unix system(vi) and a popular shell (c
shell). Berkley also created a better file system, a more versatile mail feature and a better
method of linking files. Later they also offered with their standard distribution a
networking protocol software(TCP/IP) that made the internet possible.

Sun used the BSD Unix as a foundation for developing their own brand of Unix
called SunOS. Today their version of UNIX is known as solaries. Others had their own
brands such as IBM had AIX, HP offered HP-UX.

As each vendor modified and enhanced Unix to create their own versions, the
original Unix lost its identity as a separate product. There was no Standard version of
Unix. Hence AT & T Released SVR$(system V Release 4), a standard Unix.

Even before the advent of SVR4, big things were happening in the U. S Defense
Department. They created the first communication network called ARPANET and it
used TCP/IP protocol and they tried to implement TCP/IP on BSD Unix. The
incorporation of TCP/IP into Unix and its use as the basis of development were two key
factors in the rapid growth of the internet.

In the meantime, Microsoft released a new operating system called Windows,


which used GUI (Graphical user interface) that uses mouse rather than complex
commands to execute a job. Windows was a big threat to UNIX, because it was a
windowed system. So Unix badly needed a windowed type interface for its survival
hence the Massachusetts Institute of Technology(MIT) introduced X Window-the first
windowing system for Unix.

Linus Torvalds-Father of Linux developed Linux. Linux is distributed free under


general public license, which makes it mandatory for developers and sellers to make
the source code public. Linux is particularly strong in networking and internet features.
The most popular Linux flavors include Red Hat, Caldera, Mandrake, Fedora etc.

Page | 2
UNIX PROGRAMMING (18CS56) MODULE - 1

UNIX ARCHITECTURE / COMPONENTS

The figure 1.1: The Kernel-Shell Relationship

Division of Labor: Kernel and Shell

The main concept in the Unix architecture is the division of labor between two
agencies the KERNEL and SHELL. The kernel interacts with hardware and shell
interacts with user.
The kernel is the core of the 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.
User applications (programs) that need to access the hardware, uses the services
of kernel. These programs access the kernel through a set of function called
system calls.
Apart from providing support to user programs, the kernel also performs other
tasks like managing system’s memory, scheduling processes, decides their
priorities etc. So the kernel is often called the operating system- a program’s
gateway to the computer’s resource.
The shell is the command interpreter; it translates command into action. It is the
interface between user and kernel. System contains only one kernel, but there
may be several shells.
When user enters a command through the keyboard the shell thoroughly
examines keyboard input for special characters, if it finds any, it rebuilds

Page | 3
UNIX PROGRAMMING (18CS56) MODULE - 1

simplified command line and finally communicates with kernel to see that the
command is executed.

The File and Process

Two simple entities support the Unix system is the file and process.

A file is just an array of bytes and can contain virtually anything. It is also related
to another file by being part of a single hierarchical structure. So it is necessary
to locate a file within reference to a pre-determined place. Unix considers even
directories and devices as files. Files will be arranged in a hierarchical structure
in Unix system.

A process is the name given to a file when it is executed as program. So the


process is a time image of executable file. Process are treated as living organism
which have parents, children and grand-children and are born and die. Like files,
processes also belong to a hierarchical tree structure.

The System Calls

Unix is written in C. Though there are a thousand of commands in the system, they all
use a handful of functions called System Calls, to communicate with the kernel. All
versions of Unix use the same system calls.

FEATURES OF UNIX

Unix is an operating system, so it has all the features an operating system is expected
to have. The following sections present the major features of this operating system.

1. UNIX: A Multiuser System

From the fundamental point of view UNIX is a multiprogramming system; it


permits multiple programs to run and compete for the attention of the CPU.

This can happen in two ways:

 Multiple users can run a system

 A single user can also run Multiple jobs

In Unix, the resources are actually shared between all users, therefore Unix is also a
multiuser system. Multiuser technology is the great socializer that has time for
everyone.

In Unix systems, computer breaks up a unit of time into several segments. So at any
point in time, the machine will be doing the job of a single user. The moment the
allocated time expires, the previous job will be preempted and next user’s job is taken
up. This process goes on until clock has turned full circle and the first user’s job is
taken up once again.

Page | 4
UNIX PROGRAMMING (18CS56) MODULE - 1

2. UNIX: A Multitasking System Too


 A single user can run multiple tasks concurrently; UNIX is a multitasking
system.
 It is usual for a user to edit a file, print another one on the printer, send email and
browse world wide web – all without leaving any of the application.
 The Kernel is designed to handle a user’s multiple needs.
 In multitasking environment, a user sees one job running in the foreground, the
rest running in the background. It is possible to switch the job between
background and foreground, suspend or even terminate them.

3. The Building-Block Approach


 Unix Operating System provides features that allows complex programs to be
built from combining more than one command, this can be achieved by
“pipes” and “filters”.
 It is possible to connect different with the pipe (|) to get different jobs done.
The commands that can be connected in this way are called filters, because
they filter or manipulate data in different way.
 For ex: ls command lists all files. wc command counts the number of words.
They can be combined using pipes (|) to find the total number of files in the
directory.

4. Unix Tool Kit


 Unix provides set of tools like general purpose tools, text manipulation
utilities (called filters), compilers, interpreters, networked application and
system administration tools.
 New tools are being added and the old ones are being removed or modified.

5. Pattern Matching
 Unix features sophisticated pattern matching features. The “*” is a special
character used by the system to indicate that it can match a number of
filenames. Apart from the *, there are several other special characters used in
Unix system. Unix features elaborate pattern matching schemes that use
several characters from this metacharacter set.

6. Programming Facility
 The Unix shell is also a programming language. It has all the features of a
programming language such as control structures, loops and variables. These
features are used to design shell scripts.

Page | 5
UNIX PROGRAMMING (18CS56) MODULE - 1

7. Documentation
 To know about Unix in detail there exist many online help facilities like man
command. It is the most important reference for commands and their
configuration files. The reference manuals are available online.

Example: $man cp

[Displays the description of cp command].

The UNIX Environments and UNIX Structure

UNIX Environments

Unix can be used in 3 different environments:

1. Personal Environment
2. Time sharing environment
3. Client-server environment

Personal Environment

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.

Time Sharing Environment

 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:

Page | 6
UNIX PROGRAMMING (18CS56) MODULE - 1

 It must control the shared resources.


 It must manage the shared data
 It must manage the printing data and resource.
 It must handle the computing all task

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.

Client-Server Environment

 In client server environment splits the computing function between a central


computer and user’s computers.
 User computer are personal computer or workstation central computer assigned
to the workstations. In client server environment the user’s 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.
 Server is a one serves the request of a client. The users are given personal
computers or workstations so that some of the computations can be moved off to
clients. Here workload will be shared between server and clients and hence the
system becomes fast.
 Since work is shared between user’s computer and the central computer,
response time and monitor display are faster and users are more productive.

UNIX STRUCTURE

UNIX consists of four major components: The Kernel, The Shell, a standard set of
utilities and Application programs. These components are shown in the figure:

Page | 7
UNIX PROGRAMMING (18CS56) MODULE - 1

The kernel:

 Kernel is the heart (core) of the UNIX Operating system.

 It Contains most basic parts including process control and resource


management.

 All the other components call on the kernel to perform these services for them.

The Shell:

 It receives and interprets the commands entered by the user.

 To do any operations in the system, user must give the Shell a command. If the
command requires a utility, the shell requests that the kernel execute the utility.

 There are two major parts of the shell. The first is Interpreter; the second part of
the shell is a programming capability that allows you to write a shell script.

Utilities:

 A Utility is a Standard UNIX program that provides a support process for users.

 There are literally hundreds of UNIX Utilities. Three common utilities are text
editors, search programs and sort programs.

 For example: vi (text editor), the list (ls) utility display the files that reside on the
disk.

Applications:

 Applications are programs that are not a standard part of UNIX.

 These are written by system administrators, professional programmers or users,


they provide an extended capability to the system.

Page | 8
UNIX PROGRAMMING (18CS56) MODULE - 1

 Many standard utilities standard out as application years ago and proved so
useful that they are now part of the system.

POSIX and SINGLE UNIX SPECIFICATION

POSIX- Portable Operating System standard for Computer Environment (specially for
UNIX) is the group of standards developed by IEEE for operating system.

Two standards from POSIX family are POSIX.1 and POSIX.2

 POSIX.1 deals with C application program interface.


 POSIX.2 deals with shell and utilities.

X/OPEN (open group) developed standard for UNIX i.e. X/OPEN portability guide (XPG).

In 2001, X/OPEN and IEEE unified these two standards and called it as SUSV3- Single
Unix Specification Version 3. This is based on “write once, adopt anywhere” approach,
i.e. once software has been developed on any POSIX compliant UNIX system, it can be
easily ported to another POSIX compliant UNIX system with minimum modification.

GENERAL FEATURES OF UNIX COMMANDS/ COMMAND STRUCTURE

Command Structure:

The syntax of command is:

$command [ -/+ options] [arguments]

Verb:
 The Verb is the command name. The command indicates what action is
to taken.
 Command name and options/arguments have to separate by spaces or
tabs to enable the system to interpret them as words.
 Single/Multiple spaces can be given to separate the commands and
options/arguments.
 The shell compresses these multiple spaces to a single space.

Page | 9
UNIX PROGRAMMING (18CS56) MODULE - 1

Options:
 The options modify how the action is applied.
 Options are usually one character preceded by a minus sign (-) or plus sign
(+).
Arguments:
 The Arguments provide additional information to the command.
 Some commands may accept single argument; some may accept multiple
arguments or so may not accept any arguments.
Exceptions:
 There are exceptions to the general syntax of commands.
 There are commands (pwd) that don’t accept any arguments.
 Some commands (EX: who) may or may not take arguments.
 Some commands like ‘ls’ can run without arguments(ls), with only
options (ls -l), with only filenames (ls chap1 chap2) or can be run with
options and filenames (ls –l chap1 chap2).

BASIC UNIX COMMANDS

1. echo: DISPLAYING A MESSAGE

Syntax: echo [option] … [string] ….

echo command used in shell scripts to display messages on the terminal, or to issue
prompts for taking input from the user. It can also be used to display shell variables.

echo command can be used to

 To display a message (like echo “cseise”)


 To evaluate shell variables (echo $PAGER)

Originally echo was an external command, but now all shells have echo as built in
command.

The options used by echo command.

Option Purpose
N Do not output the trailing new line
E Enable interpretation of the backslash escaped characters listed
below
E Disables interpretation of those sequences in Strings.
Help Display this help and exit
version Output version information and exit

An escape sequence is generally a two character-string beginning with a \ (backslash).

Page | 10
UNIX PROGRAMMING (18CS56) MODULE - 1

The following below are commonly used ones:

Examples:

1. $echo

[Displays a blank line]

2. $echo $SHELL

Output: /bin/bash [Displays the name of the shell being used]

3. $echo Unix Programming

Output: Unix Programming

4. $echo –e “Enter filename: \c”

Output: Enter filename: _

[Displays the message and suppresses the new line character. To enable
the use of escape sequence in LINUX, the –e option must be used.]

2. printf: AN ALTERNATIVE TO echo

printf can be used as alternative command to echo. The difference is echo command by
default inserts ‘\n’ character at the end, but printf does not insert newline at the end.

Eg: $printf cseise

Output: cseise $ //prompt will be in the sameline.

printf also accept all escape sequence used by echo. printf also uses formatted strings in
the same way the C language function of the same name.

Page | 11
UNIX PROGRAMMING (18CS56) MODULE - 1

Example:

$printf “My current shell is %s\n” $SHELL

Output: My current shell is /usr/bin/bash

Here are some of the commonly used ones:

%s  String

%30s  As above but printed in a space 30 characters wide

%d  Decimal integer

%6d  As above but printed in a space 6 characters wide

%o  Octal integer

%x  Hexadecimal integer

%f  Floating point number

Example:

$printf “Value of 255 is %o in octal and %x in hexadecimal\n” 255 255

Output: Value of 255 is 377 in octal and ff in hexadecimal.

3. ls: LISTING FILES


 The UNIX system has a large number of files that controls its functioning and
users also create files on their own. These files are organized in separate folders
called directories.
 It is possible to list the names of the files in the directory using ls command.
 ls command can also be used to search for a command.

Options used with ls command

 -a  Displays all files including hidden files,current directory(.) and parent


directory(..)
 -d dirname  Searches for directory with name dirname,if found displays
it,otherwise produces a message directory not found.
 -i  displays file with inode number
 -l  displays all files with seven attributes such as permissions,links,owner
name ,group name,size in bytes,date and time ,file name.
 -r  displays files in reverse alphabetical order
 -R  Displays recursive list of files
 -t  Sorts file based on last modified time
 -u  sorts files based on last access time
 -x  multi columnar output

Page | 12
UNIX PROGRAMMING (18CS56) MODULE - 1

Example:

$ls

Output: [It displays all the files present in the current directory]

$ls p*

Output: [It displays all the files starts with p]

$ls –i

Output: [it displays file with inode number]

$ls –l

Output: [Displays all files with seven attributes such as permissions,links,owner name
,group name,size in bytes,date and time ,file name.]

Page | 13
UNIX PROGRAMMING (18CS56) MODULE - 1

4. who: WHO ARE THE USERS?

Unix maintains an account of all users, who are logged on to the system. The ‘who’
command displays an informative listing of these users.

Example:

$who

Output:

First column shows the username, second shows device name of user’s terminal, third,
fourth, fifth shows date and time of logging in. User can login remotely to a unix system.
Last column shows the ma chine name from where user logged in. (:0) indicates user
logged in from his own terminal.

The options used with the who command:

-H To display the header.


-u  detailed list
Example:

$who –Hu

Output:

First column shows name of the user, second shows terminal name, 3rd,4th,5th shows
date and time. 6th column shows system’s IDLE timing, i.e. from how long system is
idle.

Page | 14
UNIX PROGRAMMING (18CS56) MODULE - 1

5. date: DISPLAYING THE SYSTEM DATE

The Unix system maintains an internal clock meant to run perpetually. When the
system is shut down a battery backup keeps the clock ticking. Date command is used to
display both date and time to the nearest second.

Example:

$date

Output:

The command can also be used with suitable format specifiers as arguments, each
format preceded by the + symbol, followed by the % operator, and single character
describing the format.

To print only the month using the format +%m:


$date +%m
11
To print the month name:
$date +%h
Nov

There are many other format specifiers and the useful ones are listed below:

d  Displays the day of the month (1 to 31)


$date +%d
08
y  Displays the last two digits of the year
$date +%y
22
H, M and S  Displays the hour, minute and second respectively.
$date +%H %M %S
19 45 00
D  Displays the date in the format mm/dd/yy
$date +D
11/08/22
T  The time in the format hh:mm:ss
$date +T
19 : 46 : 47

Page | 15
UNIX PROGRAMMING (18CS56) MODULE - 1

6. passwd: CHANGING YOUR PASSWORD

The passwd command is used to change password of a user account.

If your account doesn’t have a password or has one that is already known to others, you
should change it immediately. This is done with the passwd command.

$passwd
passwd: changing password for kumar
Enter login password: ******
New password: ***********
Re-enter new password: ***********
passwd (SYSTEM): passwd successfully changed for kumar

passwd expects user to respond three times. First, it prompts for the old password. Next,
it checks whether user have entered a valid password, and if user have, it then prompts
for the new password. Enter the new password using the password naming rules
applicable to user system. Finally, passwd asks user to re-enter the new password. If
everything goes soomthly, the new password is registered by the system.

When user enters a password, the string is encrypted by the system. Encryption
generates string of random characters and stores in a file named shadow in the /etc
directory.

Password framing rules:

Some of the rules which should be followed while framing password:


 Don’t choose a password similar to old one.
 Don’t use commonly used names like names of friends, relatives, pets, etc.
 Use mix of alphabetic or numeric characters.
 Don’t write password in an easily accessible document.
 Change password regularly.

7. cal: THE CALENDAR

Using cal command it is possible to see the calendar of any specific month or a complete
year. The facility is totally accurate and takes into account the leap year adjustments
that took place in the year 1752.

Syntax: cal [month] year]

Example:

$cal

[It displays the calendar of the current month]

Page | 16
UNIX PROGRAMMING (18CS56) MODULE - 1

User can use cal command with arguments.

Eg: $cal 03 2006 [Displays calendar of march 2006].

When ‘cal’ is used with arguments month is optional but year is mandatory. A single
argument to cal is interpreted as year.

Eg: $cal 2003 [Displays the calendar of 2003].

COMBINING COMMANDS

Unix allows to specify more than one command in the command line. Each command
has to be separated from the other by a ; (semi-colon).

Eg: $wc note; ls –l note

Output will be line, word and character count of the file note and long listing of attributes
of file note.

Eg: $ (wc note; ls –l note) > list

Output of ‘wc note’ and ‘ls –l’ will be redirected to the file list

MEANING OF INTERNAL AND EXTERNAL COMMANDS

Internal Commands: The commands that are directly executed by the shell are known
as Internal commands.

 Examples: echo, pwd etc.


 Internal commands don’t have independent existence, they are shell built-ins.

External Commands: The commands that are executed by the kernel are called external
commands.

 Examples: cat, ls etc.


 External commands will have independent existence in one of the directories
specified in the PATH variable.

Shell is an external command with difference, i.e. shell possesses its own set of internal
commands. If a command exists both as an internal command of the shell as well as an
external one, the shell will give top priority to its internal command.

Page | 17
UNIX PROGRAMMING (18CS56) MODULE - 1

Eg: echo command, which is also found in /bin directory but rarely ever executed,
because the shell makes sure that the internal echo command takes precedence over
the external.

The type command: knowing the type of a command and locating it.

The type command is used to describe how its argument would be translated if used as
commands. It is also used to find out whether it is built-in or external binary file.

Synatx: type [options] command names

Examples:

Type command options:

-a: This option is used to find out whether it is an alias, keyword or function and it also
displays the path of an executable.

Ex: type –a pwd

-t: This option will display a single word as an output. alias – if command is a shell alias

1. keyword – if command is a shell reserved word


2. builtin – if command is a shell builtin
3. function – if command is a shell function
4. file – if command is a disk file
Ex: type -t pwd
type -t cp
type -t ls

Page | 18
UNIX PROGRAMMING (18CS56) MODULE - 1

type -t while

THE ROOT LOGIN

The Unix system provides a special login name for the exclusive use of the
administrator. It is called root. This account doesn’t need to be separately 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:

Login: root
Password: ****** [Enter}
#:
The prompt of root is #, unlike the $ or % used by non-privileged users. Once user login
as root, you are placed in roots home directory. Depending on the system this directory
could be / or /[Link] PATH list is also different from the one used by other users:

/sbin : /bin : /usr/sbin : /usr/bin /usr/dt/bin

su: Becoming the superuser

Any user can acquire superuser status with the su command if he/she knows the root
password. For example, the user Juliet becomes a superuser in this way:

$su
Password: *********
# pwd
/home/Juliet
Though the current directory doesn’t change, the # prompt indicates that Juliet now has
powers of a superuser. To be in root’s home directory on superuser login use su –l.

Creating user’s environment: Users often rush to the administrator with the complaint
that a program has stopped running. The administrator first tries running it in a
simulated environment. Su, when used with a -, recreates the users’ environment
without taking the login-password route:

su – henry

Page | 19
UNIX PROGRAMMING (18CS56) MODULE - 1

This sequence executes henry’s profile and temporarily creates henry’s environment.
This mode can be terminated by hitting [ctrl-d] or using exit.

UNIX FILES
Introduction:

Files are the building blocks of any operating system. When user execute a command
in UNIX, the UNIX kernel fetches the corresponding executable file from a file system,
loads its instruction text to memory, and creates a process to execute the command on
your behalf.

 In the course of execution, a process may read from or write to files. All these
operations involve files. Thus, the design of an operating system always begins
with an efficient file management system.
 UNIX Looks at everything as a file and any UNIX System has thousands of files.
 The file system in UNIX is one its simple and conceptually clean feature.
 UNIX File system is a logical method of organizing and storing large amounts of
information in a way that makes it easy to manage.
 The file is a container of storing information. user can treat it simply as a
sequence of characters. If you name a file foo and write three characters a,b,c into
it, then foo will contain only the string abc and nothing else.
 UNIX treats directories and devices as files as well. A directory is simply a folder
where you store filenames and other directories.
 All physical devices like the hard disk, memory, CD, ROM, Printer and modem are
treated as files.

Naming Files:

A filename can consist of up to 255 characters. Files may or may not have extensions,
and can cosists of practically any ASCII character except the / and the NULL character
(ASCII value 0). We are permitted to use control characters or other unprintable
characters in a filename. The following are the valid filenames in UNIX:

 last_time
 list.
 ^V^B^D-++bcd
 @#$%*abcd
 a.b.c.d.e
The third filename contains three control characters. These characters shoud be
definitely be avoided in framing filenames. Moreover, since the UNIX system has a
special treatment for characters like $, `, ?, *, & among others, it is recommended that
only the following characters be used in filenames.

Page | 20
UNIX PROGRAMMING (18CS56) MODULE - 1

 Alphabetic characters and numerals.


 The period(.), hyphen (-) and underscore(_ ).
 Unix imposes no rules for framing filename extensions.

DOS/Windows users must also keep these two points in mind:

 A file can have as many dots embedded in its name; a.b.c.d.e is a perfectly valid
filename. A filename can also begin with a dot or end with one.

 UNIX is case sensitive to case; Chap01, chap01. And CHAP01 are three different
filenames, and it’s possible for them to coexist in the same directory.

Basic File Types/Categories:

1. Ordinary (Regular) File.


2. Directory File.
3. Device File.

Ordinary File:

An ordinary file or regular file is the most common file type. All programs you write
belong to this type. An ordinary file itself can be divided into two types:

a) Text File
b) Binary File

A Text file contains only printable characters, and you can often view the contents and
make sense out of them. All C and Java program sources, shell and perl scripts are text
files. A text file contains lines of characters where every line is terminated with the
newline character, also known as line feed (LF). When you press (Enter) while inserting
text, the LF character is appended to every line. You won't see this character normally,
but there is a command (od) which can make it visible.

A Binary file, on the other hand, contains both printable and unprintable characters that
cover the entire ASCII range (0 to 255). Most UNIX commands are binary files, and the
object code and executables that you produce by compiling C programs are also binary
files. Picture, sound and video files are binary files as well. Displaying such files with a
simple cat command produces unreadable output and may even disturb your terminal's
settings.

Directory File:

A directory contains no data, but keeps some details of the files and subdirectories that
it contains The UNIX file system is organized with a number of directories and
subdirectories, and you can also create them as and when you need. You often need to
do that to group a set of files pertaining to a specific application. This allows two or more
files in separate directories to have the same filename.

Page | 21
UNIX PROGRAMMING (18CS56) MODULE - 1

A directory file contains an entry for every file and subdirectory that it houses. If you
have 20 files in a directory, there will be 20 entries in the directory Each entry has two
components:

 The filename

 A unique identification number for the file or directory (called the inode number),

If a directory bar contains an entry for a file foo, we commonly (and loosely) say that the
directory bar contains the file foo Though we'll often be using the phrase "contains the
file rather than "contains the filename, you must not interpret the statement literally A
directory contains the filename and not the file's contents.

Device File:

You'll also be printing files, installing software from CD-ROMs or backing up files to tape.
All of these activities are performed by reading or writing the file representing the
device. For instance, when you restore files from tape you read the file associated with
the tape drive. It is advantageous to treat devices as files as some of the commands used
to access an ordinary file also work with device files.

Device file names are generally found inside a single directory structure, /dev. A device
file is indeed special, it's not really a stream of characters. In fact, it doesn't contain
anything at all.

Every file has some attributes that are not stored in the file but elsewhere on disk. The
operation of a device is entirely governed by the attributed of its associated file. The
kernel identifies a device from its attributes and then uses them to operate the device.

ORGANIZATION OF FILES & STANDARD DIRECTORIES:

Unix file system is a logical method of organizing and storing large amounts of
information in a way that makes it easy to manage. A file is a smallest unit in which
the information is stored. Unix file system has several important features. All data in
Unix is organized into files. All files are organized into directories. These directories are
organized into a tree-like structure called the file system.

Files in Unix System are organized into multi-level hierarchy structure known as a
directory tree. At the very top of the file system is a directory called “root” which is
represented by a “/”. All other files are “descendants” of root.

Page | 22
UNIX PROGRAMMING (18CS56) MODULE - 1

Directories or Files and their description:


• / : The slash / character alone denotes the root of the filesystem tree.
• /bin: Stands for “binaries” and contains certain fundamental utilities, such as ls or
cp, which are generally needed by all users.
• /boot: Contains all the files that are required for successful booting process.
• /dev: Stands for “devices”. Contains file representations of peripheral devices and
pseudo-devices.
• /etc: Contains system-wide configuration files and system databases. Originally
also contained “dangerous maintenance utilities” such as init,but these have
typically been moved to /sbin or elsewhere.
• /home: Contains the home directories for the users.
• /lib: Contains system libraries, and some critical files such as kernel modules or
device drivers.
• /media: Default mount point for removable devices, such as USB sticks, media
players, etc.
• /mnt: Stands for “mount”. Contains filesystem mount points. These are used, for
example, if the system uses multiple hard disks or hard disk partitions. It is also
often used for remote (network) filesystems, CD-ROM/DVD drives, and so on.
• /proc: procfs virtual filesystem showing information about processes as files.
• /root: The home directory for the superuser “root” – that is, the system
administrator. This account’s home directory is usually on the initial filesystem,
and hence not in /home (which may be a mount point for another filesystem) in
case specific maintenance needs to be performed, during which other filesystems
are not available. Such a case could occur, for example, if a hard disk drive suffers
physical failures and cannot be properly mounted.
• /tmp: A place for temporary files. Many systems clear this directory upon startup;
it might have tmpfs mounted atop it, in which case its contents do not survive a
reboot, or it might be explicitly cleared by a startup script at boot time.

Page | 23
UNIX PROGRAMMING (18CS56) MODULE - 1

• /usr: Originally the directory holding user home directories,its use has changed. It
now holds executables, libraries, and shared resources that are not system critical,
like the X Window System, KDE, Perl, etc. However, on some Unix systems, some
user accounts may still have a home directory that is a direct subdirectory of /usr,
such as the default as in Minix. (on modern systems, these user accounts are often
related to server or system use, and not directly used by a person).
• /usr/bin: This directory stores all binary programs distributed with the operating
system not residing in /bin, /sbin or (rarely) /etc.
• /usr/include: Stores the development headers used throughout the system. Header
files are mostly used by the #include directive in C/C++ programming language.
• /usr/lib: Stores the required libraries and data files for programs stored within /usr
or elsewhere.
• /var: A short for “variable.” A place for files that may change often – especially in
size, for example e-mail sent to users on the system, or process-ID lock files.
• /var/log: Contains system log files.
• /var/mail: The place where all the incoming mails are stored. Users (other than
root) can access their own mail only. Often, this directory is a symbolic link to
/var/spool/mail.
• /var/spool: Spool directory. Contains print jobs, mail spools and other queued tasks.
• /var/tmp: A place for temporary files which should be preserved between system
reboots.

Hidden Files:
To show all the hidden files in the directory, use ‘-a option’. Hidden files in Unix starts
with ‘.’ in its file name. It will show all the files including the ‘.’ (current directory) and
‘..’ (parent directory).

ls –a

ls –A: To show the hidden files, but not the ‘.’ (current directory) and ‘..’ (parent directory).

Page | 24
UNIX PROGRAMMING (18CS56) MODULE - 1

Parent child relationship:

• All files in Unix are related to one another.


• The file system is organized in a hierarchical structure.
• The implicit feature of Unix file system is that there is a top directory which is
called as root which serves as the reference point for all files.
• Top is represented by a / (front slash).
• Root directory (/) has several sub directories under it.
• These subdirectories in turn have more sub directories and other files under them.
o Ex: bin and usr are two directories directly under /, while cp and pwd are
subdirectories under bin.
• Every file must have a parent and it should be possible to trace the ultimate
parentage of a file to root.
• Thus, the home directory in the above figure is the parent for mthomas, while / is
the parent of home and the grand parent of mthomas.
• In the parent-child relationship, the parent is always a directory.
o Ex: [Link] is an ordinary file, it cannot have a directory under it.

The Home Variable and The Home Directory:


• When user log on to the system, UNIX automatically places user in a directory
called the home directory. It is called by the system when a user account is opened.
• If you log in using the login name Kumar, you will land up in a directory that could
have the pathname /home/kumar.
• Home directory can be changed by the user.
• The shell variable HOME knows your home directory.
o E: $echo $HOME
/home/Kumar
• The above output is an absolute pathname.

Page | 25
UNIX PROGRAMMING (18CS56) MODULE - 1

• The slashes act as a delimiter to the file and directory names except the first /(root).

Absolute and Relative Path

A PATHNAME is a sequence of directory names separated by slashes. The pathname


shows the location of a file or directory with reference to the root directory.

Example: /usr/staff/joan/file3

Depending on the beginning point from where a path is defined, there are two types of
pathnames.

(1) Absolute Pathname

(2) Relative Pathname

ABSOLUTE PATHNAME: If the first character of a pathname is a /, the file’s location


must be determined with respect to root (the first /). Such a pathname is called an
ABSOLUTE PATHNAME. An absolute pathname is one that starts with the character /.

Example: /usr/staff/joan/file3

RELATIVE PATHNAME: A Relative pathname does not contain / as the first character
and starts from the current working directory.

Example: joan/file3

The command will work only when the directory joan is under the current working
directory and file3 exists in joan directory.

Relative pathname uses either current working directory or the parent directory as
reference, and specifies a path relative to it. A relative pathname uses one of these
symbols:

. (a single dot) – represents the current working directory

. . (two dots) – represents the parent directory.

Directory Commands:
1. pwd: Checking your current directory
• User can move around from one directory to another, but at any point of time,
if user wants to find out in which directory he is present then user can use
Print Working Directory(pwd) command.
Ex: pwd

/home/kumar/SantoshReddyP

[pwd also displays the absolute pathname].

2. cd: Changing the current directory:


• User can move around the UNIX file system using cd (change directory)
command.

Page | 26
UNIX PROGRAMMING (18CS56) MODULE - 1

• When used with the argument, it changes the current directory to the
directory specified as argument, progs:
$ pwd

/home/kumar

$cd progs

$ pwd

/home/kumar/progs

• Here we are using the relative pathname of progs directory. The same can be
done with the absolute pathname also.
$cd /home/kumar/progs

$ pwd

/home/kumar/progs

$cd /bin

$ pwd

/bin

• cd can also be used without arguments:


$ pwd

/home/kumar/progs

$cd

$ pwd

/home/kumar

• cd without argument changes the working directory to home directory.


$cd /home/sharma

$ pwd

/home/sharma

$cd

/home/kumar

3. mkdir: Making Directory


• Directories are created with mkdir (make directory) command. The
command is followed by names of the directories to be created. A directory
patch is created under current directory like this:
$mkdir patch

Page | 27
UNIX PROGRAMMING (18CS56) MODULE - 1

• You can create a number of subdirectories with one mkdir command: $mkdir
patch dba doc
• For instance, the following command creates a directory tree:
$mkdir progs progs/cprogs progs/javaprogs
• This creates three subdirectories – progs, cprogs and javaprogs under progs.
• The order of specifying arguments is important. You cannot create
subdirectories before creation of parent directory.

• For instance, following command doesn ‘t work $mkdir progs/cprogs


progs/javaprogs progs
mkdir: Failed to make directory “progs/cprogs”; No such directory
mkdir: Failed to make directory “progs/javaprogs”; No such directory

• System refuses to create a directory due to fallowing reasons:


o The directory is already existing.
o There may be ordinary file by that name in the current directory.
o User doesn ‘t has permission to create directory.

4. rmdir: Removing a Directory


• rmdir command removes the directory.
• The directory must be empty before using rmdir with the directory name.
o Ex: rmdir pis
• Multiple directories can be removed.
o Ex: $rmdir pis/data pis/progs pis
• Here first subdirectories are removed and at last pis is removed.
• A subdirectory cannot be removed unless the user is placed in a directory which
is hierarchically above that directory.
▪ Ex: $pwd
/home/kumar/pis/progs

$cd /home/kumar/pis

/home/kumar/pis

$pwd

/home/kumar/pis

$rmdir progs

rmdir: Things to remember


• You can ‘t removes a directory which is not empty
• You can‘t remove a directory which doesn‘t exist in system.
• You can‘t remove a directory if you don‘t have permission to do so.

Page | 28
UNIX PROGRAMMING (18CS56) MODULE - 1

Using . and .. in Relative Pathname:


• User can move from working directory /home/kumar/progs/cprogs to home
directory /home/kumar using cd command like
$pwd

/home/kumar/progs/cprogs

$cd /home/kumar

$pwd

/home/kumar
• Navigation becomes easy by using common ancestor.
• . (a single dot) - This represents the current directory
• .. (two dots) - This represents the parent directory
• Assume user is currently placed in /home/kumar/progs/cprogs
$pwd

/home/kumar/progs/cprogs

$cd ..

$pwd

/home/kumar/progs
• This method is compact and easy when ascending the directory hierarchy. The
command cd .. Translates to this ―change your current directory to parent of
current directory‖.
• The relative paths can also be used as: $pwd
/home/kumar/progs

$cd ../..

$pwd

/home
• The following command copies the file [Link] present in javaprogs, which is
present is parent of current directory to current directory.
$pwd

/home/kumar/progs/cprogs

$cp ../javaprogs/[Link] .
• Now [Link] is copied to cprogs under progs directory.

File Related Commands:


1. cat: Displaying and creating files:
o cat command is used to display the contents of a small file on the
terminal.

Page | 29
UNIX PROGRAMMING (18CS56) MODULE - 1

$ cat cprogram.c
# include
<stdioh>

void main ()
{
Print(“hello:);

o As like other files cat accepts more than one filename as arguments
$ cat ch1 ch2

It contains the contents of chapter1


It contains the contents of chapter2
o In this the contents of the second files are shown immediately after the
first file without any header information. So cat concatenates two files-
hence its name.

cat options:
o Displaying Nonprinting Characters (-v) cat without any option it will
display text files. Nonprinting ASCII
characters can be displayed with –v option.

o Numbering Lines (-n)


-n option numbers lines. This numbering option helps programmer in
debugging programs.

Using cat to create a file:

cat is also useful for creating a file. Enter the command cat, followed by > character
and the filename.

$ cat > new

This is a new file which contains some text, just to

Add some contents to the file new


[ctrl-d]

$_

When the command line is terminated with [Enter], the prompt vanishes. Cat now
waits to take input from the user. Enter few lines; press [ctrl-d] to signify the end of
input to the system. To, display the file contents of new use file name with cat
command.
$ cat new

Page | 30
UNIX PROGRAMMING (18CS56) MODULE - 1

This is a new file which contains some text, just to

Add some contents to the file new

2. cp: Copying a file o The cp command copies a file or a group of files. It creates
an exact image of the file on the disk with a different name. The syntax takes
two filename to be specified in the command line.
o When both are ordinary files, first file is copied to second.
$ cp csa csb
o If the destination file (csb) doesn‘t exist, it will first be created before
copying takes place. If not it will simply be overwritten without any
warning from the system.
o Example to show two ways of copying files to the cs directory:
$ cp ch1 cs/module1 ch1 copied to module1 under cs

$ cp ch1 cs ch1 retains its name under cs

o cp can also be used with the shorthand notation, .(dot), to signify the
current directory as the destination. To copy a file „new‟ from
/home/user1 to your current directory, use the following command:
$cp /home/user1/new new destination is a file

$cp /home/user1/new . destination is the current directory


o cp command can be used to copy more than one file with a single
invocation of the command. In this case the last filename must be a
directory.
Ex: To copy the file ch1,chh2,ch3 to the module , use cp as

$ cp ch1 ch2 ch3 module

o The files will have the same name in module. If the files are already
resident in module, they will be overwritten. In the above diagram
module directory should already exist and cp doesn‘t able create a
directory.
o UNIX system uses * as a shorthand for multiple filenames.
Ex:

$ cp ch* usp Copies all the files beginning with ch


cp options
o Interactive Copying(-i) : The –i option warns the user before overwriting
the destination file, If unit 1 exists, cp prompts for response
$ cp -i ch1 unit1

$ cp: overwrite unit1 (yes/no)? Y

o A y at this prompt overwrites the file, any other response leaves it


uncopied.

Page | 31
UNIX PROGRAMMING (18CS56) MODULE - 1

Copying directory structure (-R) : o It performs recursive behavior command can


descend a directory and examine all files in its subdirectories.
o -R : behaves recursively to copy an entire directory structure
$ cp -R usp newusp $ cp -R class newclass

o If the newclass/newusp doesn‘t exist, cp creates it along with the


associated subdirectories.

3. rm: Deleting Files:


• The rm command deletes one or more files.
Ex: Following command deletes three files:

$ rm mod1 mod2 mod3


• Can remove two chapters from usp directory without having to cd
Ex:

$rm usp/marks ds/marks


• To remove all file in a directory use *
$ rm *
• Removes all files from that directory

rm options
• Interactive Deletion (-i) : Ask the user confirmation before removing each file:
$ rm -i ch1 ch2 rm: remove ch1 (yes/no)? ? y rm: remove ch1 (yes/no)? ? n
[Enter]
• A ‗y‘ removes the file (ch1) any other response like n or any other key leave the
file undeleted.
• Recursive deletion (-r or -R): It performs a recursive search for all directories and
files within these subdirectories. At each stage it deletes everything it finds.
$ rm -r * #Works as rmdir

• It deletes all files in the current directory and all its subdirectories.
• Forcing Removal (-f): 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

4. mv: Renaming the files:


The mv command renames (moves) files. The main two functions
are: It renames a file(or directory)
• It moves a group of files to different directory
• It doesn't create a copy of the file; it merely renames it. No additional space is
consumed on disk during renaming.

Page | 32
UNIX PROGRAMMING (18CS56) MODULE - 1

Ex: To rename the file csb as csa we can use the following
command $ mv csb csa
• If the destination file doesn‘t exist in the current directory, it will be created. Or
else it will just rename the specified file in mv command.
• A group of files can be moved to a directory.
Ex: Moves three files ch1,ch2,ch3 to the directory module

$ mv ch1 ch2 ch3 module

• Can also use to rename directory


$ mv rename newname
• mv replaces the filename in the existing directory entry with the new name. It
doesn't create a copy of the file; it renames it
• Group of files can be moved to a directory
• mv chp1 chap2 chap3 unix

5. wc: Counting Lines, words, and Characters:


• wc command performs Word counting including counting of lines and
characters in a specified file. It takes one or more filename as arguments and
displays a four columnar output.
$ wc ofile 4 20 97 ofile

• Line: Any group of characters not containing a newline


• Word: group of characters not containing a space, tab or newline
• Character: smallest unit of information, and includes a space, tab and newline
• wc offers 3 options to make a specific count. –l option counts only number of
lines, - w and –c options count words and characters, respectively.
$ wc -l ofile 4 ofile $ wc -w ofile 20 ofile
• Multiple filenames, wc produces a line for each file, as well as a total count.
$ wc -c ofile file 97 ofile 15 file 112 total

6. od: Displaying Data in Octal


• od command displays the contents of executable files in a ASCII octal value.
$ more ofile this file is an example for od command ^d used as an
interrupt key
• -b option displays this value for each character separately.
• Each line displays 16 bytes of data in octal, preceded by the offset in the file of
the first byte in the line.
$ od –b file

0000000 164 150 151 163 040 146 151 154 145 040 151 163 040 141 156 040 0000020 145

170 141 155 160 154 145 040 146 157 162 040 157 144 040 143 0000040 157 155 155 141

Page | 33
UNIX PROGRAMMING (18CS56) MODULE - 1

156 144 012 136 144 040 165 163 145 144 040 141 0000060 163 040 141 156 040 151 156

164 145 162 162 165 160 164 040 153 0000100 145 171

-c character option
• Now it shows the printable characters and its corresponding ASCII octal
representation
$ od –bc file

• Some of the representation:


o The tab character, [ctrl-i], is shown as \t and the octal value 011

o The bell character, [ctrl-g] is shown as 007, some system shows it


as \a

o The form feed character, [ctrl-l], is shown as \f and 014

o The LF character, [ctrl-j], is shown as \n and 012

o Od makes the newline character visible too.

Page | 34

You might also like