0% found this document useful (0 votes)
9 views

UNIX Shell Programming

This document provides an introduction to the Unix operating system, covering its fundamentals, history, structure, and features. Unix is highlighted as a powerful, multitasking, and multi-user operating system that is portable across various hardware platforms. Key features discussed include multitasking, multi-user capabilities, portability, job control, tools and utilities, and security.

Uploaded by

Farhan Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

UNIX Shell Programming

This document provides an introduction to the Unix operating system, covering its fundamentals, history, structure, and features. Unix is highlighted as a powerful, multitasking, and multi-user operating system that is portable across various hardware platforms. Key features discussed include multitasking, multi-user capabilities, portability, job control, tools and utilities, and security.

Uploaded by

Farhan Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 93

Unix: C HA PT E R

An Introduction
1
After studying this chapter, the reader will be conversant with the following:
• Fundamentals of operating systems
• History of Unix
• Structure of the Unix operating system
• Various types of shells and their responsibilities
• Numerous features of the Unix operating system
• The Unix environment

Even after four decades of use, Unix is regarded as one of the most powerful operating
systems, due to its portability and usage in almost all kinds of environments, ranging from
micro to supercomputers.
We cannot even think of using a computer system without an operating system. An
operating system is an interface that enables the use of a computer system’s resources;
without an operating system, the computer will be a dead piece of electronic device.
In this chapter, before delving into the history and structure of Unix, we will attempt
to understand the following: what an operating system is; why it is essential in running a
computer system; and in what manner Unix is different from the other operating systems
used earlier and in recent times.

1.1 OPERATING SYSTEM


An operating system is the main software component of a computer system. It provides users
with an environment that makes it possible to use the hardware devices of a computer. Without
an operating system, we cannot access any of the resources of the computer system, including
its hardware and software. Examples of popular operating systems available nowadays include
Android, BSD, iOS, LINUX, Microsoft Windows, Mac OS X, and z/OS. Apart from Microsoft
Windows and z/OS, all the other operating systems in this list are Unix-based.
Let us understand how an operating system is related to hardware, software, and the users
(see Fig. 1.1).
2 Unix and Shell Programming

As depicted in Fig. 1.1, it is evident that users are able to interact with hardware through the
operating system. The operating system as well as the software creates an environment for
the user that enables easy access and use of hardware. Basically, the operating system creates
an interface between the user and the hardware.
The following section discusses the functions that an operating system performs, which
enable easy operation of a computer system.
1.1.1 Functions of Operating Systems
An operating system performs the following functions:
Memory and data management All operating systems provide methods for controlling
data in the memory. When a job has to be performed, the operating system should allocate
the memory for loading that job into the memory.
Communication An operating system should support methods in such a manner that the
various computer systems can communicate with one another for exchange of data.
Time sharing Time sharing enables several people to use the same computer
simultaneously. A few operating systems support time-sharing features.
Security In a multi-user environment, security should be provided by the operating system.
This security prevents one user from interfering with the work done or being done by another
user. It also prevents unauthorized personnel from using the computer system.
User-command interpretation This is a function of the operating system using which the
commands that are typed in by the user are read and interpreted by the operating system.
Through interpretation, the operating system understands what the user wants.
Accounting Through this function, the operating system keeps an account of all the
resources used by different processes. Resources, here, means memory, CPU, disk space
requirement, and so on.
Program development tools All operating systems provide program
development tools, which assist users in writing and maintaining programs.
Users Software development is one of the important features provided by the
operating system.
Scheduling A scheduler is the heart of all multi-user operating systems.
Software This program enables many people to use the computer simultaneously. The
scheduler assigns the CPU time slice to the ready process. After that time slice,
the process is stored in the wait queue, the next process in the ready queue is
Operating system picked, and the CPU pays attention to it.
Swapping When several users are working simultaneously, their processes are
stored in the memory. When the memory is full and a new process has to be
Hardware activated, the scheduler takes the current process in the memory and copies it to
the hard disk. Next, the scheduler starts a new process in the space freed in the
Fig. 1.1 Operating memory. This process is known as swapping. After returning to the time slice,
system in relation to the process that was swapped out of the memory is brought in (swapped in), and
hardware, software, and some other process is swapped out. This feature is available in a virtual memory
users environment.
Unix: An Introduction 3

1.2 HISTORY OF UNIX


The development of the Unix operating system began in 1957 and had its roots in Bell
Labs. The growth of this wonderful, multitasking, highly powerful, affordable, and secure
operating system was not an accident, but the result of the joint efforts of many people,
including students, professors, researches, and commercial companies. A short date-wise
history of the evolution of Unix is given here.
In 1957, Bell Labs required an operating system for their in-house computer centre. They
created BESYS to sequence their jobs and to control the system resources. However, they
wanted a more efficient operating system.
In 1964, the researchers from General Electric, MIT, and Bell Labs came together
and created a new general-purpose, multi-user, time-sharing operating system known as
Multiplexed Information and Computing System (Multics).
In 1969, the Multics project was withdrawn because of the high cost of development
and due to differences among its members. When this happened, Ken Thompson, Dennis
Ritchie, Douglas Ritchie, and Douglas Mcllroy, along with a few others, began working on
Uniplexed Information and Computing System (UNICS) by using an old PDP-7 computer.
The name Unics was then shortened to Unix. While working on the early assembly versions
of Unix, Thompson worked on a FORTRAN compiler that evolved to support the language
B, which was a smaller version of BCPL.
In 1971, the first edition of Unix appeared along with the B compiler. It introduced
several well-known Unix commands including cat, chdir, chmod, chown, and cp. Together
it included more than 60 commands. However, it did not have the pipe feature. Some of the
utilities in this first edition were written in the B compiler. In the next few years, Dennis
Ritchie rewrote the B compiler and developed the C compiler.
In 1972, the second edition of Unix was released.
In 1973, the third edition of Unix appeared along with the Unix C compiler (cc). The
kernel was still written in assembly language. The pipe feature was also introduced in this
version.
In 1973, the fourth edition of Unix was released. The kernel was rewritten in the C compiler.
In 1974, the fifth edition of Unix was released. The source code was made freely available
to universities for educational purposes. Unix also spread outside AT&T and Bell Labs and
was provided to academic institutions at a very small charge. It became very popular, as it was
inexpensive, could run on the available hardware, was provided along with the source code,
and was written in a programming language that was easier to understand. In 1974, Thompson
taught Unix for a year at the University of California, Berkeley. When Thompson returned
to Bell Labs, students and professors at Berkeley continued to enhance Unix. This led to the
formation of the Berkeley Software Distribution, which was commonly known as BSD.
In 1975, the sixth edition of Unix was released. This edition, also known as V6 UNIX,
was the first edition that was available outside Bell Labs.
In 1977, 1BSD, the first edition, was released; in 1978, 2BSD, the second edition, was
released.
In 1979, the seventh edition of Unix was released. This edition was released along
with Steve Bourne’s shell (sh). The kernel was rewritten to make it more portable to other
4 Unix and Shell Programming

types of architecture. At this point, the Unix systems group (USG) was created, and it was
focused on enhancing the seventh edition. Three groups were working in all and the original
versions of Unix were developed by the computer research group (CRSG) of Bell Labs. The
support for internal releases was provided by the USG. The task of developing and writing
tools was done by another group at Bell Labs, the programmer’s workbench (PWB).
The development of Unix split into two main branches: System 5 (SYSV) and Berkeley
software distribution (BSD). BSD was developed by students and professors at the University
of California, Berkeley. SYSV was developed by AT&T and other commercial companies.
In 1979, 3BSD, the third edition, was released.
In 1980, 4.0BSD, the fourth version of the BSD Unix variant, was released.
In 1982, AT&T transferred its Unix development to Western Electric, which developed
the System III version of Unix.
In 1983, Western Electric released System V, whereas System IV was reserved for only
AT&T’s use.
In 1984, the USG group, which was renamed the UNIX system development laboratory
(USDL) group, released System V Release 2 (SVR2), which was the first version of Unix
that supported paging, shared memory, and other associated features.
In 1985, the eighth edition of Unix was released on the basis of the 4.1BSD version.
In 1987, the USDL group, which was renamed AT&T Information Systems (ATTIS)
group, released System V Release 3 (SVR3).
In 1988, the ninth version of Unix was developed, and it was based on the 4.3BSD version.
In 1989, the tenth version of Unix was developed.
Unix is one of the most popular operating systems, which was developed step by step, as
evident from the aforementioned timeline.
Let us now have a broad overview of the Unix system.

1.3 OVERVIEW AND FEATURES OF UNIX SYSTEM


The Unix system is a multitasking, multi-user operating system that is portable on several
hardware platforms and which is quite secure. It also provides a rich set of tools and utilities
that help administrators, programmers, and users, to a great extent, in executing their tasks.
Besides this, the system provides the flexibility of controlling individual jobs executed by
the user.
Some of the important features of the Unix operating system are as follows:
1. Multitasking 4. Job Control
2. Multi-user 5. Tools and Utilities
3. Portability 6. Security

1.3.1 Multitasking
Unix is a multitasking operating system, that is, it can execute multiple tasks simultaneously.
In a multitasking environment, the CPU processes a task and when the process waits for
an input/output (I/O) operation to be completed, the CPU switches to another task. The
switching between tasks is so fast that it appears that the operating system is executing all
the tasks simultaneously. Due to multitasking, we can carry out several tasks simultaneously.
Unix: An Introduction 5

For instance, commands for printing a file, editing text, and managing files can be given
simultaneously; all tasks are thus performed simultaneously. With the help of this feature,
Unix maximizes the computer resource utilization and hence, the computer’s efficiency.

1.3.2 Multi-user
The multi-user feature of Unix enables several users to work simultaneously and access
system resources concurrently. The operating system not only receives commands from all
the users, but also carries out the desired processing and responds accordingly. The operating
system manages the consumption of system resources among the users and implements the
locking mechanism to maintain the integrity and consistency of applications and data that
are accessed simultaneously. The multi-user approach maximizes the computer resource
utilization and hence reduces the cost per user. Since the system resources are shared,
resource management is done so as to avoid any deadlock.
Note: Deadlock is a situation wherein two or more competing actions wait for each other to finish and, as a
result, neither reaches completion.

1.3.3 Portability
Unix is portable, that is, it is available on a wide range of hardware. Since the Unix operating
system is coded in a high-level language, C programming language, it is less hardware
dependent and, hence, can be easily moved from one brand of computer to another without
a major code rewrite. It is also the kernel that provides an interface between the hardware
and other application modules. The application modules interface with the kernel and not the
hardware, and hence, when Unix is ported to another hardware platform, only the kernel and
not the application modules requires modification. This makes the operating system almost
hardware independent and does not require much modification.

1.3.4 Job Control


Unix enables us to control the execution of jobs. For example, we can suspend or resume
any job, switch a job from the background to the foreground, and kill a job. The jobs that
require user interaction frequently need I/O operation, have specific time constraints, and
are executed in the foreground. In foreground jobs, the shell waits for a job to be completed
and only then displays a prompt to execute another job. Background jobs are those that are
executed behind the scenes. Jobs of a lower priority do not require user interaction and are
executed in the background. Suspended jobs are paused for a while and can be moved to
either the background or the foreground. The job control feature of Unix enables users to
execute several jobs and control them on the basis of their priority.

1.3.5 Tools and Utilities


Unix supports a number of tools and utilities that make the users’ job easier. Tasks such as
splitting files, merging files, searching for content in files, arranging files, and sending mail
can be simply done by issuing certain commands. Unix not only has a vast library of system
tools, but also has programming tools that provide a flexible platform for programmers and
developers to create portable and efficient applications.
6 Unix and Shell Programming

1.3.6 Security
Unix is considered a comparatively more secure operating system. Each user has an identity
through a unique user ID and group ID. In order to avoid any unauthorized access, each
file and directory has an owner and a group that are associated with it. Three permissions
are attached to each file and directory—read, write, and execute. The set of permissions, r,
w, and x, are associated with the three types of users—owners, groups, and others. Hence,
we can individually assign the desired permissions to these three types of users. Next let us
explore the structure of the Unix system.

1.4 STRUCTURE OF UNIX SYSTEM


The structure of the Unix operating system consists of four parts (as shown in Fig. 1.2):
hardware, kernel, shell, and tools and applications. The various parts of the Unix system are
discussed in detail in the following sections.

1.4.1 Hardware
Hardware refers to the physical components that collectively form a computer machine.
The following three primary components constitute the hardware of a computer system:

I/O devices Data is supplied or entered into the computer for processing through input
devices such as keyboard, mouse, track ball, magnetic ink character recognition (MICR),
optical character recognition (OCR), and optical mark recognition (OMR). Output devices
display processed data. The two most common output devices are screen and printer.

Central processing unit The central processing unit (CPU) is the heart of the computer.
It obtains the data from the user through input devices, processes the entered data into
information, and displays the information through output devices. The processed data can be
saved in the memory for future use.

Memory It is used for storing data and is of


two types: primary and secondary. The primary
Tools and applications
memory includes RAM and ROM, out of which
Shell RAM is volatile in nature. While the data is being
Kernel processed by the CPU, it is temporarily stored
in the RAM. After processing, it is removed
and replaced by new data, which has to be
Hardware
processed further. ‘Volatile’ means that the data
stored in the RAM is temporary in nature, (i.e.,
it is overwritten by the new data and the whole
data is lost on switching off the computer). The
secondary memory includes hard disk drives, pen
drives, CDs/DVDs, and so on. These devices are
of a permanent nature, that is, once data is written
Fig. 1.2 Structure of the Unix system in them, it will be stored until deleted by the user.
Unix: An Introduction 7

Note: Networking components such as LAN cards, cables, routers, and switches are also considered part of
the hardware.

1.4.2 Kernel
The kernel is the heart of any operating system. Its main purpose is to ensure that the jobs
of the operating system are performed properly. These jobs mainly include the scheduling
of tasks, resource management, process management, and file management. Resource
management refers to the allotment of CPU time, disk space, memory space, and so on to
different processes. Process management includes the allocation of resources such as CPU,
memory, and other devices. File management includes the management of files and their
permissions, among others.
The kernel hides all the complexities of accessing hardware and provides a user-friendly
interface by doing all the tasks behind the scene.
A brief view of the different tasks performed by the kernel is provided in Fig. 1.3.
Let us take a quick look at the operations that a kernel can perform:
1. It controls the execution of processes by enabling their creation, termination or suspension,
and communication.
2. It schedules processes fairly for execution on the CPU. The processes share the CPU
in a time-shared manner. The CPU executes a process; the kernel suspends it when its
time quantum elapses and schedules another process to be executed. Later, the kernel
reschedules the suspended process.
3. It allocates the main memory for an executing process. The kernel enables processes to
share portions of their address space under certain conditions, but protects the private
address space of a process from outside tampering. If the system runs low on free memory,
the kernel frees the memory by writing a process temporarily to the secondary memory,
which is called a swap device. If the kernel writes entire processes to a swap device,
the implementation of the Unix system is called a swapping system, whereas if it writes
pages of memory to a swap
device, it is called a paging
Applications Shells Utilities system.
4. It allocates secondary
memory for efficient stor-
age and retrieval of user
System call interface
data. This service consti-
tutes the file system. The
kernel allocates second-
Kernel
ary storage for user files,
reclaims unused storage,
structures the file sys-
Memory Process File Peripheral tem in a well-understood
management scheduling systems devices manner, and protects
unauthorized users from
Fig. 1.3 Different tasks performed by the kernel illegal access.
8 Unix and Shell Programming

5. It allows processes-controlled access to peripheral devices such as terminals, tape


drives, disk drives, and network devices.
6. It provides the necessary functionality to applications, shells, and utilities through the
system call interface. The applications of all the respective systems are called in order to
get certain tasks performed by the kernel.
After having understood the kernel and the tasks that it performs, we are ready to
understand the next part of the Unix structure—the shell.

1.4.3 Shell
The shell is an interface between the user and the kernel. The kernel does not know human
language; hence the shell accepts the commands from the user and converts them into a
language that the kernel can understand. It is a program that interprets user requests, calls
programs from the memory, and executes them one at a time. Several shells such as Bourne,
Korn, Bourne-again, and C Shell are available.
The shell also provides the facility of chaining or pipelining commands. This means the
output of one command is sent to the input of another command for further processing. In
this manner, one input data can be processed by several commands.
There are two major parts of a shell. The first is the interpreter. The interpreter reads
out commands and works with the kernel to execute them. The second part of the shell is a
programming capability that enables us to write a shell (command) script. A shell script is a
file that contains a collection of shell commands to perform a specified task. It is also known
as a shell program.

Types of shells
Shells are independent of the underlying Unix kernel. This fact has enabled the development
of several shells for Unix systems. Each type of shell has its own special features.
Bourne shell It is the most common shell in Unix systems and was the first major shell. It
was developed by Steve Bourne at the AT&T Labs. This shell was released in 1977 and was
called ‘sh’.
Korn shell It was developed by David Korn at AT&Bell Labs. It is built on the Bourne
shell. The most stable version of this shell was released in 1988 by AT&T’s Unix System
Laboratories as ‘ksh’. The Korn shell also incorporates the features of the C shell (e.g.,
process control). One of the important features of this shell is that it can run Bourne shell
scripts without any modification at all.
Bourne-again shell An enhanced version of the Bourne-again shell, which is also known
as ‘bash’, is distributed as the standard shell in almost all Unix systems. This is a freeware
shell from the Free Software Foundation (FSF), where it was developed by Brian Fox and
Chet Raney.
C shell It is also called the programmer’s shell and exists as ‘csh.’ It was developed by Bill
Joy at the University of California, Berkeley. The C shell got its name because its syntax and
usage is very similar to the C programming language. A compatible version of the C shell,
‘tcsh’ is used in Linux.
Unix: An Introduction 9

Bourne-again The C shell is not always available on all machines.


shell In addition, shell scripts written in the C shell are not
compatible with the Bourne shell. Such scripts should
be modified for working with the Bourne shell. One
Bourne shell of the major advantages of the C shell (compared with
the Bourne shell), however, is its ability to execute
processes in the background.
Korn shell The four shells are shown in Fig. 1.4. Tcsh is a
compatible version of the C shell that is used in LINUX.
Both the Korn shell (ksh) and the Bourne-again
C shell Tcsh
shell (bash) are extensions of, and compatible with, the
basic Bourne shell (sh). The original C Shell (csh) is
Fig. 1.4 Different shells in Unix operating system
only partially based on the Bourne shell and has been
extended into a shell called the ‘TC shell’ (tcsh), which is a C Shell with some additional
features. Since the TC shell is completely compatible with the C shell, it is also frequently
referred to as the ‘C shell’.
Until now, we have seen the functions that are generally performed by an operating
system; we should also know the additional features of the Unix operating system.
1.4.4 Tools and Applications
Tools and applications are built-in modules that are used by the operating system to perform
the tasks assigned by the user. These are available in the form of libraries that add special
capabilities to the operating system. Irrespective of whether the task is to display date and
time, find files, copy files, list files, or translate characters, among others, all tasks are
performed through Unix utilities. The tools and utilites are categorized on the basis of the
kind of tasks they perform. For example, file utilites do the tasks related to files: breaking
text files into pieces, combining text files together, and sorting their contents. Other utilities
such as grep, sed, and awk help in filtering or searching the desired content from the files.
Some of the most commonly used file-related Unix utilities are as follows:
1. cp: Copying files
2. ln: Linking one file to another
3. ls: Listing files or directory contents
4. mv: Moving or renaming files
5. rm: Removing files
6. pr: Printing files
7. tr: Translating characters
Usually an operating system is used in a single environment, but Unix is an operating
system that can be used in several environments. Let us now have a brief discussion of the
Unix environment.

1.5 UNIX ENVIRONMENT


Unix is a multi-user and multiprocessing operating system that can be used in three
environments: stand-alone personal environment, time-sharing environment, and client–
server environment.
10 Unix and Shell Programming

1.5.1 Stand-alone Personal Environment


Unix can be installed on personal computers and used as stand-alone machines. Though
the major features of the Unix operating system are exploited in a multi-user environment,
its security features, multitasking capability, and portability make it an attractive choice for
installation on personal computers.

1.5.2 Time-sharing Environment


A time-sharing environment is an environment in which a computer is connected to several
terminals and all the terminals share the resources of the central computer: CPU time, hard
disk, and printer. The central computer divides its CPU time into small time slices and serves
each terminal in the time slot assigned to it. Hence, each terminal waits for its time slot
to get its jobs processed by the central computer. Though this environment is economical,
the total dependency on the central computer is its major drawback. If the central machine
fails, all the terminals connected to it stop working, and hence, this environment is not very
popular nowadays. Since all the tasks of the terminals are performed by the central CPU, it
is overloaded and hence its response is very poor.

1.5.3 Client–Server Environment


The client–server environment is better than the time-sharing environment, as here, the
central computer is not connected to dumb terminals but to workstations or PCs that have
their individual processing power. As a result, all the processing tasks are not assigned to the
central computer but are divided among the central computer and the connected workstations
so that the local and small tasks can be processed at the workstation level (without bothering
the central computer), and the main tasks (that require more resources) are transferred to the
central computer. The workstations in this environment are known as clients, and the central
computer (that serves the requests sent by the clients) is known as server. In this environment,
dependency on the central computer is decreased and since the local tasks are performed at
the client’s level, the server is not overloaded, hence increasing its response time.

Note: We can customize the Unix shell environment by also making use of system variables known as
environment variables, which will be discussed in Chapter 10.

■ SUMMARY ■

1. Operating systems provide an environment that interprocess communication, time sharing, security,
makes it possible for us to use the resources of a user-command interpretation, accounting, program
computer, namely hardware and software. A few development, scheduling, and swapping.
examples of modern-day operating systems include 3. In 1960, Multics started the development of the now
Android, BSD, iOS, LINUX, Microsoft Windows, Mac well-known Unix operating system. Unix became
OS X, and z/OS. commercially viable in 1973 when it was entirely
2. The various functions that an operating system recoded in C, thereby facilitating portability in other
performs include memory and data management, hardware. A typical structure of the Unix operating
Unix: An Introduction 11

system consists of hardware, a kernel, a shell, and 6. The main features of the Unix operating system are
various tools and applications. portability, multitasking, and multi-user capability.
4. The kernel is the heart of the operating system. It is 7. Since Unix is a multiprocessing and multitasking
defined as a nucleus of the operating system that operating system, it can be used in three different
manages all the resources and gets the task performed types of environments: stand-alone personal environ-
by the desired hardware. ment, time-sharing environment, and client–server
5. A shell acts as an interface between a user and a environment.
kernel. Mainly four types of shells are available in the 8. Currently, Unix is also portable on mobile devices.
Unix operating system, namely Bourne shell (sh), C Almost all mobile operating systems, including
shell (csh), Korn shell (ksh), and Bourne-again shell iOS, Android, and webOS, run on Unix or LINUX
(bash). kernels.

■ EXERCISES ■

Objective-type Questions
State True or False
1.1 The Unics operating system was further 1.6 The Bourne-again shell (bash) was developed by
developed to Unix. David Korn.
1.2 An operating system creates an environment 1.7 The Korn shell was developed by Brian Fox and
that enables us to use different resources of a Chet Raney.
computer system. 1.8 The Bourne shell derives its name from Stephen
1.3 The Korn shell is the oldest of all shells. Bourne.
1.4 The Korn shell and Bourne-again shell are not 1.9 The shell manages all the resources and gets the
compatible with the Bourne shell. tasks performed by the desired hardware.
1.5 Unix is a multi-user and multitasking operating 1.10 Unix enables a user to run only one process at a
system. time.

Fill in the Blanks


1.1 Unix operating system is written in computer is connected to the workstations
language. or PCs.
1.2 BSD stands for . 1.7 Unix treats each job or task as a .
1.3 The operating system creates an 1.8 Both the Korn shell (ksh) and the Bourne-again
between the user and the hardware. shell (bash) are extensions of, and compatible
1.4 C shell was developed by . with, the shell.
1.5 When a job has to be performed, the 1.9 prevents unauthorized personnel
should allocate the memory for loading that job from using the computer system.
into the memory. 1.10 schedules processes for execution
1.6 In a environment, the central on the CPU.

Multiple-choice Questions
1.1 Which of the following is the heart of any 1.2 Korn Shell was developed by
operating system? (a) David Korn (c) Bill Joy
(a) Hardware (c) Software (b) Steve Bourne (d) Ken Thompson
(b) Kernel (d) Users
12 Unix and Shell Programming

1.3 The default prompt of the C shell is (c) WAN environment


(a) $ (b) % (c) cs (d) > (d) isolated environment
1.4 The three environments in which the Unix 1.5 The shell that is completely compatible with the
operating system can be used are stand-alone C shell is
personal environment, time-sharing environment, (a) Korn shell
and (b) Bourne-again shell
(a) client–server environment (c) TC shell
(b) LAN environment (d) Bourne shell

Review Questions
1.1 Write short notes on the following: 1.3 How did the Unix operating system come into
(a) Different tasks performed by the kernel the picture? Briefly explain its history.
(b) Role of shell in the Unix operating system 1.4 How many different types of shells are there?
(c) Structure of the Unix system Explain in detail.
1.2 Explain the functions performed by an operating 1.5 Explain the time-sharing and client–server
system. environment of the Unix operating system.

■ ANSWERS TO OBJECTIVE-TYPE QUESTIONS ■


State True or False 1.8 True 1.3 interface Multiple-choice
1.1 True 1.9 True 1.4 Bill Joy Questions
1.2 True 1.10 False 1.5 operating system 1.1 (b)
1.3 False 1.6 client–server 1.2 (a)
1.4 False Fill in the Blanks 1.7 process 1.3 (b)
1.5 True 1.1 ‘C’ 1.8 Bourne 1.4 (a)
1.6 False 1.2 Berkeley Software 1.9 Security 1.5 (c)
1.7 False Distribution 1.10 Scheduler
Unix File C HA PT E R

System

2
After studying this chapter, the reader will be conversant with the following:
• Unix files and their types
• Different types of device files
• Organization of a file system
• Accessing, mounting, and unmounting a file system
• Different blocks of a file system
• Structure of inode blocks

2.1 INTRODUCTION TO FILES


A file is a container of text, images, codes, and so on. Everything is a file on a Unix system.
Not only the data, programs, and applications, but also the directories and input/output (I/O)
devices are considered special kinds of files.
Generally, files are ordered in a hierarchical tree-like fashion with a root represented by
the character, ‘/’. The directories are the internal nodes of the tree structure, while the files
are considered to be the leaves. Let us learn about the different types of files in Unix.

2.1.1 Types of Files


The files are divided into the following three categories in the Unix operating system:
Ordinary files These files contain only data.
Directory files These files act as a container and can contain ordinary files and device files
along with directory files.
Device files These files represent all the hardware devices.
Ordinary files
We can store anything we want in these files. These files include data, source programs,
objects, executable codes, Unix commands, and any file created by the user. Commands such
as cat and ls are treated as ordinary files. An ordinary file is also referred to as a regular file.
14 Unix and Shell Programming

The most common type of ordinary file is the text file. This is just a regular file that contains
printable characters. For example, the programs that we write are text files. However, the Unix
commands that we use or the C programs that we execute do not fall into the category of text files.
The characteristic feature of text files is that the data stored inside them is divided into
groups of lines, with each line terminated by the newline character. This character is not
visible, and it does not appear in the hard copy output. It is generated by the system when we
press the <Enter> key.
Examples letter.txt, bank.sh, payment

The files in Unix may or may not have any extension. The first two examples depict files with
extensions .txt and .sh, respectively. The third example depicts a file without any extension.
In most Unix systems, a filename can have approximately 255 characters. If we enter more
than 255 characters while specifying a filename, only the first 255 characters are effectively
interpreted by the system.
Note: We have to assign extensions for the AWK files or other programming files (e.g., C).

Directory files
A directory contains no external data, but it stores some details of the files and sub-directories
it contains. The Unix file system is organized into a number of such directories and sub-
directories, which can also be created as and when needed. We often need to group a set of
files pertaining to a specific application. This enables two or more files in separate directories
to have the same filename.
If a directory contains, for example, 10 files, there will be 10 entries in the directory file
displaying information such as size of the file, date and time of creation, or last modification.
When an ordinary file is created or removed, its entry in the corresponding directory file is
automatically updated by the kernel with the relevant information about the file.
Note: The directory file contains the names of all resident files in the directory.

Examples projects, shell_scripts

These examples show two directories named projects and shell_scripts.


Device files
In the Unix operating system, peripheral devices, terminals, printers, CD-ROMs, modems,
disks, and tapes are treated as special files that are termed device files. The representation of
devices in the form of device files simplifies the task of using them. For example, printing
content on a printer is as simple as copying that content to the printer device file. A device
file interacts with the device driver, making it possible for the user to directly interact
with the device driver using standard I/O system calls, hence controlling the device more
precisely.
There are two types of device files based on how data is read or written into them: character
devices and block devices.
Character devices Character devices are those in which the read and write operations
are performed character by character, that is, one byte at a time. These devices are also
known as raw devices. The read and write operations in these device files are performed in

www.allitebooks.com
Unix File System 15

the actual transfer units of the device, that is, single characters at a time without collecting
or combining them into a block. It is quite obvious that character devices are comparatively
slow and have a large access time.
Examples include virtual terminals, terminals, and serial modems.
Block devices Block devices are those in which the read and write operations are performed
one block at a time, where the size of one block can range from 512 bytes to 32 KB. When
compared with character devices in which transactions are performed one character at a time,
block devices are quite fast. Moreover, block devices use caching to reduce the access time.
By caching, we mean that when a block device is accessed, the kernel reads the whole block
into a buffer in the memory, so that future read and write operations are performed to the
cached version in the memory, hence reducing the access time to a great extent. Finally, the
modified buffer contents are written to block devices. The only drawback in using memory
buffers is that if the system crashes before modified buffers are written into the block device,
the data will be inconsistent. Hence, we need to periodically flush out the modified buffers
to the block device.
Examples include hard disk, DVD/CD ROM, and memory regions.
Note: All device files are stored in the /dev directory.

2.1.2 Symbolic Links


A symbolic link is a special file that points to another existing file on the system. This link
contains the path name of the file it is pointing to. We can create several names for the same
file through symbolic links. In order to create symbolic links, the ln command is used, and
for listing them, the long-listing command ls -l is used. These commands will be discussed
in detail in Chapter 3. A brief introduction to these commands is as follows.
For example, let us assume we have a file letter.txt. Through the ln command, we can
create its symbolic link in the file memo.txt as shown here:
Syntax ln -s source destination

Here, source is the absolute or relative path of the file whose link we want to create, and
destination is the name of the link.

Example ln -s letter.txt memo.txt

The two filenames, letter.txt and memo.txt, refer to the same file, and changes made in
either file will be reflected in the other file.

2.1.3 Pipes
Pipes are used for sending the output of a command as the input to another command. Pipes
are created through the vertical bar character ‘|’, which contains commands on either side.
The output of the command on the left-hand side is sent as input to the command on the
right-hand side. The syntax for creating a pipe is as follows:
Syntax command1 | command2

Example ls | sort
16 Unix and Shell Programming

We will discuss two commands, ls and sort, in Chapter 3, but for the time being, it is
enough to understand that the output of the ls command is sent to the sort command before
outputting the result on the screen.
The pipe created through this syntax is known as anonymous pipe, because it is created
and later destroyed when the process is over. command1 and command2 on either side of the pipe
have their own file descriptors that are automatically closed when the process is over.
Apart from anonymous pipes, we can also create named pipes. As the name suggests,
named pipes have specific names that are assigned to them, and exist as special files within
the file system. Named pipes are known as first in first out (FIFO) because of two reasons.
First, once the data is read from the pipe, it cannot be read again. Second, the order in which
the data is read cannot be deviated. The named pipes are not automatically deleted as in the
case of anonymous pipes but have to be explicitly deleted using the rm or unlink command.
The command used for creating named pipes is mknod. The three commands, mknod, rm, and
unlink, will be discussed in detail in Chapter 3.

2.1.4 Sockets
Socket files are used for transferring information between two processes that are running
on different machines. Socket files are basically used as an interface between our Unix
process and the networking protocol. For example, while accessing the Internet through a
web browser, sockets are used to establish communication between the Unix process and the
browser. The creation of socket files is explained in detail in Chapter 14.

2.2 ORGANIZATION OF FILE SYSTEMS


The file system is organized as a tree with a single root node called root (written as ‘/’); every
non-leaf node of the file system structure is a directory of files, and files at the leaf nodes of
the tree are directories, regular files, or special device files. The name of a file is indicated by
a path name that describes how to locate the file in the file system hierarchy.
A path name is a sequence of component names that are separated by slash characters.
A component is an arrangement of characters that designates a filename that is uniquely
contained in the previous (directory) component. A full path name starts with a slash
character and specifies a file that can be found by starting at the file system root and
traversing the file tree, following the branches, which lead to successive component names
of the path name.
Unix is an operating system that is divided into directories and sub-directories. The
system programs and libraries are categorized according to their functions and placed in
their respective directories. The forward slash (/) at the top of the tree is the root of the tree.
All other directories are the sub-directories of the root directory as shown in Fig. 2.1.
The following is the list of directories and their contents:
bin Executable files are kept in this directory and these files can be run by users. After
compilation, a program is converted into an executable binary and is usually kept in the
/bin directory.

dev All the special files in the Unix file system, such as the keyboard or terminal device
drivers, are kept in this directory.
Unix File System 17

etc All administrative files of Unix are kept in this


directory.
bin dev etc lib lost + found tmp
lib This is the central library storage for files that
Fig. 2.1 List of files and directories in the Unix are commonly used by other programs. A library is a
operating system collection of files (usually binary) that can be shared
among many processes. The advantage of having a
library is that it is a single source of data and each program can use it without needing
a unique copy of these executable functions for itself.
lost + found This is the most likely place where files can be found after the system crashes.
tmp Programs usually need extra space to store data on a disk. The /tmp directory is a
directory used by programs that need extra buffer area in order to be executed.

Since the memory of a computer is limited in nature, we need to swap in the desired process
and swap out the process whose task is done. This swapping is handled by a special file
system known as the swap file system, which is discussed here.

Swap file system


Swapping is a useful technique that enables us to execute programs and manage files that are
larger than the computer’s primary memory. The program or file that we wish to execute or
manage is logically split into small blocks that are known as pages or segments. One of the
blocks is loaded into the primary memory where it can be worked on, whereas the remaining
blocks of the program or file are stored in the physical disk drive. When a part of the program
or file that is not in the primary memory is required, swapping takes place.
Swapping is an operation by which the block of the program or file in the primary memory
is swapped out to the physical disk drive and the next block from the physical disk drive is
loaded into the primary memory. Swapping continues until the desired block is loaded in the
primary memory. The concept by which a system appears to have more memory than what
it actually has is known as virtual memory.
In the Unix operating system, a partitition of hard disk can be treated as virtual memory.
Thus, a separate partition known as swap partition is created on the disk that is meant to hold
the swapped pages of the program or file. Every system should have a swap file system that
is used by the kernel to control the movement of processes. When the system memory is
heavily loaded, the kernel has to move processes out of the memory to this file system. When
these swapped processes are ready to run, they are loaded back to the memory. Users cannot
access this file system directly.

2.3 ACCESSING FILE SYSTEMS


Let us assume we have a device such as a floppy or CD containing a few files, and we want
to view and access those files. The files on these devices make up an individual file system
with its root as ‘/’. The file system on these devices will not be accessible by the Unix system
unless it is mounted.
Mounting a file system means assigning the root directory of the new file system to a sub-
directory of the root directory of our Unix system. The subdirectory on which the new file
18 Unix and Shell Programming

system is mounted is called the mount point of a file system. The files and directories in the
new file system or mounted file system are accessible when we go into that subdirectory. By
mounting a file system, it will become a non-distinguishable part of the existing file system.
Basically, mounting is a procedure of making the main existing file system aware of the new
file system.

2.3.1 Mounting File Systems


By mounting a file system of any device, we make its file and directories accessible through
the existing Unix file system. The format of mounting a file system is as follows:
Syntax mount filesystem/devicename directory

The device name or file system is mounted on the given directory. The directory, also known as
mount point, is the name of the directory that the newly mounted file system will be assigned to.
Note: For the file system to be mounted on a particular directory, the directory should already exist on the
current file system.

In order to mount a file system that has the special device name /dev/fdɧɧ (for floppy disk 0)
onto the existing /mnt directory, the following command is used:
#mount /dev/fdɧɧ /mnt

The new file system is simply an extension of the /mnt directory. We can view and access
the files and directories of the mounted file system by changing the directory to the /mnt
directory. We can also create directories and files in the /mnt directory sub-tree.
The mount point (/mnt) should usually be an empty directory, as we will not be able to
access its original files and subdirectories once a file system is mounted on it. The files of the
/mnt directory will be accessible only when the file system is unmounted.
The file system that is mounted to the main file system should be unmounted after its
job is done. Before shutting down the Unix system, all the mounted file systems need to be
unmounted; otherwise this may result in corruption of the content.

2.3.2 Unmounting File Systems


Unmounting a file system means detaching the mounted file system from the directory of the
Unix system on which it was mounted. Once a file system is unmounted, we will not be able
to access its files or directories.
A file system cannot be unmounted if any of its files or directories are still active, that
is, if they are currently in use. If we try unmounting a file system whose file or directory is
currently open, we get an error message, ‘device busy’.
To unmount a file system that is mounted on any directory, first of all, we need to close all
the open files and directories and then proceed to unmount it. We use the following command
without any arguments, in order to know the file systems mounted within a file system:
mount

It gives a list of the mounted file systems. We might obtain the following output:
mounted mounted over
/dev/fdɧɧ /mnt
Unix File System 19

This output shows that the file system /dev/fdɧɧ is mounted on the /mnt directory.
The command that is used for unmounting the mounted file system is umount. The
following format is adopted for using the umount command:
Syntax umount filesystem name/mount point

Example The command to unmount the file system, /dev/fdɧɧ that we mounted on the /
mnt directory will be as follows:
umount /mnt
We cannot unmount the file system even if we are sitting in the same file system. Thus,
unmounting the /dev/fdɧɧ file system while sitting in the /mnt directory is not possible. We
have to come out of the /mnt directory before giving the umount command.
Note: A file system cannot be dismounted if it is busy, that is, when a file or directory on that file system is
being accessed.

Nowadays, floppy disk drives are no longer manufactured or used. Only for the sake of
explaining mount and umount commands, the concept of floppy disk drives is used. In the
currently available Unix operating systems (like Oracle Solaris 10, which we are using in
this book) and Linux systems, CD ROMs, DVDs, and USB storage devices are automatically
mounted without using the mount command. Thus, mount and umount commands are no longer
needed in the currently available Unix operating systems or equivalents. The USB storage
device is automatically mounted and is available under the /rmdisk directory, whereas the
CD ROM/DVD is automatically mounted and available under the /cdrom directory. This also
means that the following commands will navigate us to the CD ROM/DVD drive and will
depict its contents:
$ cd /cdrom
$ ls

Table 2.1 gives a brief comparison of the file systems of Windows and Unix operating
systems.

Table 2.1 Comparison between file systems of Windows and Unix operating systems

Unix Windows
In Unix, the / (forward slash) represents a separator while In Windows, the \ (backslash) is used for defining the path.
defining the path to indicate a new directory level. The following For example, the directory levels, usr and projects, in
command represents two directory levels, usr and projects: Windows are represented as follows:
cd /usr/projects cd \usr\projects
In Unix, the forward slash (/) indicates the root directory, that In Windows (and in DOS), C:\ indicates the top-level
is, the directory from where all other directories begin. All directory of the file system. Other hard disk drives, floppy
other hard disk drives, pen drives, CD ROM/DVD drives, etc., disk drives, and CD ROM/DVD drives are indicated by
are accessed via the root (/) directory. For example, /cdrom various top-level directory equivalents such as D:\ and E:\.
represents the CD ROM drive.
In Unix, the root account acts as the Unix administrator. In Windows, there is an administrator account that
performs the administrative tasks.
20 Unix and Shell Programming

2.4 STRUCTURE OF FILE SYSTEMS


A file system is a group of files and directories that exists in the form of a tree-like structure
with its root in the form of a root directory. A hard disk can be partitioned into several
parts with each part having its own file system; there can be several file systems in a hard
disk. A file system cannot be split on two different disks; it has to be entirely on a single disk.
A file system has the following four sections that are known as blocks: boot, super, inode,
and data.
The first block of a file system is called the boot block, and it is followed by super, inode,
and data blocks, as shown in Fig. 2.2.

Boot block Super block Inode block Data blocks

Fig. 2.2 File system of Unix

2.4.1 Boot Block


The boot block is the first block of the file system that contains a small bootstrap program.
The bootstrap program is a short program that is loaded by the basic input/output system
(BIOS) on starting any computer. It checks and initializes the I/O devices and also loads the
operating system. During bootstrapping, which is also known as booting, the master boot
record (MBR) is loaded into the memory, which in turn, loads the kernel into the memory.

2.4.2 Super Block


The boot block is followed by the super block. This block contains global file information
about disk usage and availability of data blocks and inodes. It also contains a pointer to the
head of the free list of data blocks.
The super block consists of the following information:
1. Size of the file system
2. Number of free data blocks available in the file system
3. List of free data blocks available in the file system
4. The index of the next free data block in the free data block list
5. Size of the inode list
6. Number of free inodes available in the file system
7. List of free inodes in the file system
8. The index of the next free inode in the free inode list
Figure 2.3 shows a super block containing an array of free data block numbers and a
pointer to the head of the free list of data blocks.
The values 109, 125, 104, 175, and 138 in this figure refer to the
109 125 104 175 138 Super block free data block numbers. The entry 109 is a data block that contains
a pointer to an array of free data blocks. We assume that the index,
which points to the next free data block in the free data block list, is
207 204 292 275 250 Data block
pointing to the last data block number, which is 138.
Fig. 2.3 List of free data blocks in When a process requests for a data block, it searches the free
super block data blocks in the super block and returns the available data block
Unix File System 21

207 204 292 275 250 Super block pointed to by the index, that is, the data block number 138 will
be returned and the index will shift to another data block in the
Fig. 2.4 List of free data blocks list. Thus, after having assigned data block 138 to the requesting
copied from data block to super block process, the index will shift to the point at the data block numbered
175, and the procedure will continue. If the super block contains
only one entry, which is a pointer to the array of free data blocks, all the entries from that
block will be copied to the super block free list as shown in Fig. 2.4.
As usual, the requesting process will continue to get block numbers from the ones listed
in the super block.

2.4.3 Inode Block


Every file or directory has an inode number—a unique number that recognizes the file or
directory in the file system. All inodes are stored in inode blocks.
We know that a hard disk is organized into blocks (or sectors) and a file stored in a
hard disk may be scattered in different blocks. The addresses of these blocks (containing
file parts) are stored in the form of a linked list in an inode block, that is, a table of blocks
is maintained for each file. Each Unix file system has its own inode table in which inode
blocks are stored. Each inode is referenced by a device + inode number pair. There are three
reserved inode numbers—0, 1, and 2—defined as follows:
0 refers to the deleted files and directories.
1 refers to file system creation time, bad blocks count, and so on.
2 refers to the root directory of the file system.
The inode block contains information on each file in the data block. The information
comprises owner of the file, file type, permissions, address of the file, and so on. Each inode
block usually contains the following entries:
1. Owner: Indicates the owner of the inode
2. Group: Indicates the group to which the inode belongs
3. File type: Indicates the type of file, that is, whether the inode represents a file, a directory,
a FIFO, a character device, or a block device (The type value is set to 0 to indicate that
the inode is free).
4. Permissions: Indicates the read, write, and execute permissions of the owner (user),
group, and other members, for the file
5. Access time: Indicates the time at which the file was last accessed
6. Modification time: Indicates the time at which the file was last modified
7. Inode modification time: Indicates the time at which the inode was last modified. (The
inode is modified when the contents of the file are changed, the permission of the file is
changed, link for the file is created, and so on.)
8. Number of links to the file: Indicates the number of links of the file
9. Physical addresses: Indicates the blocks containing the file parts
10. Size: Indicates the actual size of the file
A file’s inode number can be found using the ls -i command.
Example $ ls -i letter.txt
45267 letter.txt
22 Unix and Shell Programming

Here, 45267 is the inode number.


The Unix system also maintains an inode table in the memory for a file that it uses. When
the file is opened, its inode is copied from the hard disk to the system’s inode table.
Note: The inode contains all the attributes of a file except the filename. The filename is stored in the directory
in which the file is kept. The i number is also not stored in the inode, but is used to locate the position of the
inode in the inode blocks.

Directory
The directory contains only two file attributes: inode number and filename. When we create
a link for a file, no separate inode is allocated for it, but the link count in the inode is
incremented by one. A directory entry is also created with the new filename. When we
remove a linked file with the rm command, the link count in the inode is decremented,
and the directory entry for that link is also removed. A file is removed when its link count
becomes zero. The associated disk blocks are also freed in order to make them available for
new files.
A file is internally identified by Unix through a unique inode number that is associated
with it. A directory file contains the names of the files and the subdirectories present in that
directory along with an inode number for each. The inode number is nothing but an index
to the inode table in which information about the file is stored. For example, if the inode
number of the file letter.txt is 45267, it means that the slot number 45267 in the
inode table contains information about the file letter.txt.
Suppose the file letter.txt is present in a directory called India. If we attempt to cat
the letter.txt file, Unix will first check if the user has the read permission for the directory
India. If so, it will find out whether this directory has an entry with letter.txt. If such an
entry is found, its inode number is fetched from India. This inode number is an index to the
inode table. The contents of the file letter.txt are read from the disk addresses mentioned
in the inode entry of letter.txt and then displayed on the screen.
The file contents are placed in the form of data blocks dispersed throughout the disk. In
each inode, an array is maintained to keep track of the data blocks. The first 10 elements of
the array indicate direct indexing, that is, they directly point to the data blocks that contain
the file content. Thus, a file that needs less than or equal to 10 data blocks is accessible
via the direct index entries. After direct indexing comes single indirect indexing, which
in turn, is followed by double indirect indexing and triple indirect indexing, as shown in
Fig. 2.5.
If the file needs more than 10 blocks, it uses single indirect indexing. It contains a pointer
that points to a block, which in turn, contains an array of pointers pointing to the file’s data
blocks.
Double indirect indexing is used for larger files where a pointer points to a block of
pointers that point to other blocks of pointers, which in turn, point to the file’s data blocks.
Triple indirect indexing is used for extremely large files where a pointer points to a block
of pointers that point to other blocks of pointers, which in turn, point to other blocks of
pointers, which finally point to the file’s data blocks.
A question arises with regard to the maximum size of a file that can be pointed to by an
inode.
Unix File System 23

Owner
Group
Data
File type
block
Permissions
Access time
Modification time
Inode modification time Data
Size block
Direct

Data
Direct block
pointers
pointing
to file’s Data
data blocks block

Direct
Single indirect Data
Double indirect block
Triple indirect

Data
block

Data
block

Blocks of pointers
Fig. 2.5 Single, double, and triple indirect addressing for large files

Assuming a data block is of size 4KB and there are 10 direct pointers in an inode, the
directly addressable data block size is 10 × 4KB = 40KB.
In case of single indirect indexing, a pointer points to an entire block of pointers. If a
block is of size 4KB, and each pointer is of 4 bytes, there will be 4 KB/4 pointers, that is,
1024 pointers in a block, where each pointer points to a 4KB block. This means that a single
indirect addressing can address a file that is 1024 × 4KB in size.
Similarly, in double indirect indexing, a pointer points to a block of pointers, which in
turn, point to a block of pointers. Hence, a double indirect addressing can address a file that
is 1024 × 1024 × 4KB in size. By following the same pattern, a triple indirect addressing can
address a file of 1024 × 1024 × 1024 × 4KB size.

Note: The maximum file size that Unix supports is the sum of sizes accessible by the direct, single indirect,
double indirect, and triple indirect addressing.
24 Unix and Shell Programming

2.4.4 Data Block


The actual data is stored in data blocks. Apart from these direct data blocks, there are also
indirect blocks that contain the addresses of the direct blocks. The inode maintains a list of
these indirect block addresses.
When a file is created, the kernel looks up the list available in the super block to look for
a free inode. The efficiency of the system is increased to a great extent, as the list is always
updated, and hence, quite reliable. The kernel reads and writes the copy of the super block
in the memory when controlling the allocation of inodes and data blocks.
The information in the file system needs to be written to the disk before the power of the
Unix system is turned off. The system checks for a possible mismatch during booting. Since
the kernel works with the memory copy of the superblock rather than with the disk copy, the
kernel updates the disk copy with the memory copy. This is done with the sync operation.

Note: The kernel always maintains a copy of the superblock in the memory. The in-memory copy actually
contains the latest and the correct file system status rather than its disk copy.

The information stored in the inode table changes whenever we use any file or change its
permissions; hence, a copy of the super block and inode table are kept in the memory (RAM)
at start-up time, and all changes are made in the RAM copies of the super block and inode
table every time some modification occurs. The original super block and inode table in the
disk are updated after a fixed interval of time, say every 30 seconds, by a command called
sync. This command synchronizes the inode table in the memory with the one on the disk by
simply overwriting the memory copy on to the disk.
The disk space allotted to a Unix file system is made up of blocks, each of which is
typically 512 bytes in size. Some file systems may have blocks of 1024 or 2048 bytes.
Note: The standard system block size is 1024 bytes (known as logical block) and the physical block size is
512 bytes long (i.e., one logical block contains two physical blocks).

■ SUMMARY ■

1. In the Unix operating system, there are three types cached block of the memory.
of files: ordinary files, directory files, and device files. 4. All device files are stored in the /dev directory.
Ordinary files are also referred to as regular files, and 5. A symbolic link is a special file that points to another
they may contain printable characters. existing file on the system. These links are used to
2. The device files are of two types—character device create several names for the same file. Through the
files and block device files. In character devices, in command, we can create the symbolic link of a
read and write operations are performed character by file.
character, that is, 1 byte at a time, whereas in block 6. A pipe is represented as a vertical bar character (|)
devices, read and write operations are performed one and is used for sending the output of a command
block at a time. as an input to another command. Pipes are of two
3. Caching is a process in which the block of the disk types: anonymous pipes and named pipes. Named
accessed is kept in buffer in the memory so that in pipes are known as FIFO, as once the data is read
future, read and write operations are performed in the from the pipe, it cannot be read again.
Unix File System 25

7. Socket files are used for transferring information subdirectory on which the new file system is mounted
between two processes that are running on different is called the mount point of a file system.
machines. 11. Unmounting a file system means detaching the
8. The file system is organized as a tree with a mounted file system from the directory of the Unix
single root node called root that is represented system on which it was mounted.
as ‘/’. 12. A Unix file system typically consists of four blocks:
9. The concept by which a system appears to have boot, super, inode, and data.
more memory than what it actually has is known as 13. Every file or directory has an inode number—a
virtual memory. unique number that recognizes the file or directory
10. Mounting a file system means assigning the root in the file system.
directory of the new file system to a subdirectory 14. A file’s inode number can be found using the ls -i
of the root directory of our Unix system. The command.

■ EXERCISES ■

Objective-type Questions
State True or False
2.1 The first block of the Unix file system is known through symbolic links.
as super block. 2.7 Named pipes are also known as last in first out
2.2 Every file or directory has a unique inode number. (LIFO).
2.3 Unix also treats the physical devices as files. 2.8 In order to see the files or directories of any
2.4 tmp is the folder in which all administrative files device, its file system needs to be mounted.
are kept. 2.9 In block devices, read and write operations are
2.5 Double indirection is used for smaller files. performed one byte at a time.
2.6 We can create several names for the same file 2.10 Pipes are of two types: anonymous and named.

Fill in the Blanks


2.1 In the Unix system, a filename may approximately have more memory than what it actually has is
be characters long. known as .
2.2 Every Unix system has a file system 2.7 Character devices are also known as
in it. .
2.3 The command is used to unmount a 2.8 The boot block is the first block of the file system
file system. that contains a program.
2.4 There are three types of files in Unix: , 2.9 Inodes are maintained in an array form and
, and . are accessed through their indices known as
2.5 Every file or directory is represented by a unique .
number known as . 2.10 Ordinary files are also referred to as ,
2.6 The concept by which our system appears to and they contain printable characters.

Multiple-choice Questions
2.1 The first block of a file system is 2.2 If a directory has 10 files, the number of entries
(a) super block (c) inode block in the directory file will be
(b) data block (d) boot block (a) 10 (b) 11 (c) 9 (d) 0
26 Unix and Shell Programming

2.3 In the Unix operating system, the files are divided (d) triple indirect addressing
into three categories—ordinary, directory, and 2.7 The command that synchronizes the inode table
(a) special files (c) device files in the memory with the one on the disk is
(b) hidden files (d) inode files (a) sync
2.4 The directory in which executable files of the (b) synchronizer
Unix operating system are kept is (c) tally
(a) lib (c) dev (d) matcher
(b) etc (d) bin 2.8 The reserved inode number 0 refers to the
2.5 The Unix file system is organized as a tree with a (a) linked files
single node at the top known as (b) deleted files and directories
(a) foundation (c) seed (c) directories
(b) root (d) stem (d) device files
2.6 The indexing by which a pointer points to a block 2.9 The bootstrap program is a short program
of pointers that point to other blocks of pointers, loaded by
which in turn, point to the file’s data blocks is (a) data block (c) BIOS
known as (b) hard disk (d) named pipe
(a) direct addressing 2.10 The number of sections or blocks that a file
(b) single indirect addressing system has is
(c) double indirect addressing (a) 1 (b) 2 (c) 3 (d) 4

Review Questions
2.1 Write short notes on the following: unmounting a file in a Unix operating system.
(a) Inode block What is the significance of this process?
(b) Ordinary files 2.4 Differentiate the following:
(c) Pipes (a) Character and block devices
(d) Symbolic link (b) Boot block and data block
(e) Inode table (c) Single and double indirect addressing
2.2 What are the different blocks that constitute a 2.5 Explain the role of default files and directories in
Unix file system? the Unix operating system.
2.3 Explain the procedure of mounting and

■ ANSWERS TO OBJECTIVE-TYPE QUESTIONS ■


State True or False 2.10 True 2.5 inode number 2.2 (a)
2.1 False 2.6 virtual memory 2.3 (c)
2.2 True Fill in the Blanks 2.7 raw devices 2.4 (d)
2.3 True 2.1 255 2.8 bootstrap 2.5 (b)
2.4 False 2.2 swap 2.9 i number 2.6 (c)
2.5 False 2.3 umount 2.10 regular file 2.7 (a)
2.6 True 2.4 ordinary files, 2.8 (b)
2.7 False directory Multiple-choice 2.9 (c)
2.8 True files, device Questions 2.10 (d)
2.9 False files 2.1 (d)
Basic Unix C HA PT E R

3
Commands

After studying this chapter, the reader will be conversant with the following:
• Some basic commands that are frequently used
• Logging in to the system, changing password, checking who is logged in,
and displaying date and time of the system
• Dealing with file operations such as creating files, displaying their contents,
deleting files, creating links to files, renaming files, and moving files
• Maintaining directories, creating a directory, changing the current directory,
and removing a directory
• Displaying calendars, using basic calculators, displaying information about
current systems, deleting symbolic links, and exiting from a Unix system

Unix has a large family of commands. However, even before we discuss how to perform a
task with the help of these commands, we need to first log in to the system. Let us see how
this is done.

3.1 LOGIN: LOGGING IN TO SYSTEMS


The first step involved while working on the Unix system is to log in or identify ourselves
with the system. We should be assigned a user ID and password by the administrator,
which would enable us to log in to the system. The user ID and password (along with
other information) are assigned while adding a new user to the system. As soon as we
switch on the Unix system, we are prompted to log in. The login prompt appears as Login.
At the login prompt, we type the user ID (a unique login name provided by the
administrator).
After having typed the user ID, we are prompted for a password. The password entered
here is encrypted and appears in the form of a string of asterisk symbols in the following
manner.
28 Unix and Shell Programming

Example login: chirag


password: *****

Note: One of the main security features of the Unix operating system is the displaying of asterisks while typing
the password and storing the actual password in an encrypted format (also known as the hash of the password)
in the /etc/shadow file that can be accessed only by the root.

In case the user ID or password is wrongly entered, we get the following error message:
Login incorrect
login:

This message informs the user that either the user ID or the password has been entered
incorrectly, and a new login prompt is displayed to try again.
If the user ID and password are correct, we will be allowed to log in to the Unix system
and will be navigated to our home directory, that is, the directory in which our personal files
and settings are stored. In addition, a message indicating when we last logged in, along with
the shell prompt, is displayed:
Last login: Fri Dec 15 10:30:05 on ttys17
$

This message indicates the date, time, and terminal from which we last logged in. The
message is followed by the default Unix shell prompt by which we can write and execute
Unix commands. The default Unix prompt for the Bourne, Bash, and Korn shells is the
dollar sign ($). For C and tcsh shells, the prompt is the percentage sign (%).
You must be wondering who the administrator refers to. Let us understand this term.
System administrator A system administrator is a person who is responsible for setting up
and maintaining the Unix operating system. He/She is responsible for the proper functioning
of the Unix system and also ensures that the system resources are optimally utilized. The
following are a few of the tasks performed by the system administrator:
1. Set up and maintain user accounts
2. Monitor access and privileges and set up security policies
3. Monitor system performance and ensure proper utilization of resources
4. Install and upgrade software whenever desired
5. Take backup at regular intervals and restore systems in case of a crash
6. Perform proper starting and shutting down of systems

3.2 OVERVIEW OF COMMANDS


Commands refer to one-line statements that operate on the supplied operands to perform some
action. Each command is supported with certain options that add extra features to the command.
These options enable the user in driving the command to carry out the desired action. The options
are mostly prefixed with a hyphen (-) and more than one option can be used in a command.
We are going to look at very basic internal and external commands that users execute while
working on the Unix operating system. These command functions include listing of files
and directories, making new directories, changing directories, removing directories, creating
Basic Unix Commands 29

files, looking at the content of the files, copying, renaming, and deleting files, viewing system
date and time, and knowing the list of users who are logged in, among others.
The user performs very general operations while working with the Unix operating system.

3.2.1 Structure
As mentioned in Section 3.2, a traditional Unix command consists of options and operands,
where options are generally in the form of a character prefixed by a hypen (-), which is used for
exploiting a particular feature of the command. The argument refers to the content or data to
which the command has to be applied. An argument can be a file, directory, terminal, device, etc.
The syntax of a Unix command is as follows:
Unix_Comannd [-option1][-option2]...[Argument]
Let us understand the different types of commands in Unix.

3.2.2 Types of Commands in Unix


The basic commands in Unix are divided into two broad categories:
Internal commands The shell has a number of built-in commands that are known as internal
commands. Some of these, such as cd and echo, do not generate a process, and are directly
executed by the shell. These commands are built into the shell and do not exist as separate files.
External commands External commands are Unix utilities and programs such as cat and
ls. These commands exist in the form of individual files and are distributed in different
directories. The commonly used user commands are placed in the /bin directory, and the
commands that are usually used only by system administrators are placed in the /etc directory.
In the subsequent sections, we will be learning about the usage of the basic commands in
Unix, namely passwd, ls, mkdir, cd, rmdir, pwd, uname, touch, cat, cp, rm, mv, ln, unlink, tput,
who, finger, date, cal, echo, bc, globbing, and exit, and some line-continuation characters.

➢ passwd: Changing password


It is considered good practice to change the login passwords at regular intervals so as to
avoid any possibility of illegal access of the files by any unauthorized person. The command
for changing the password is passwd.
Syntax $passwd

On executing the passwd command, we will be prompted to enter the old password before
giving the new password (to confirm that only authorized people are changing the password).
In addition, the new password should be significantly different from the older one. It should
be at least six characters long, and have at least two alphabets, one numeric, and one special
character. On executing the command, we may get the output shown in the following example.
Example
$passwd
Changing password for chirag
Old password: *********
New password: **********
Re-enter new password: **********
30 Unix and Shell Programming

If the new password and the old password are not very different from each other, we may get
the following error:
Passwords must differ by at least 3 positions
The two passwords entered in New password and Re-enter new password should be the same;
else we will get the following error:
They don't match
Try again
In case the two passwords entered in New password and Re-enter new password are exactly
the same, the password of the user will be changed and we will get a confirming message:
Password updated successfully.

➢ ls: Listing files and directories


This command shows the files and directories on the disk. By default, the files and directories
are displayed in alphabetical order. If the name of the directory is not specified, it will display
the list of files and directories of the current working directory.
Syntax $ ls -[options]

There is a list of options available with the ls command, as shown in Table 3.1.
Table 3.1 List of options available with the ls command
Options Syntax Description
-x ls –x Shows files in multiple columns (default)
-F ls -F Shows files and directories, files have / as suffix
-r ls -r Shows files sorted in reverse alphabetical order
-R ls -R Shows the recursive listing, that is, files of directories as well as
subdirectories are also displayed
-a ls -a Shows all the hidden and visible files; hidden files start with a dot (.)
-d ls –d directory_name Shows only the directory name instead of listing its content; used
with –l option to know the status of the directory
-l ls -l Shows files in the long-listing format (shows seven attributes of a
file, that is, file permissions, number of links, owner, group, size,
date and time, and file/directory name)
-t ls -t Sorts files by modification time; the latest file is on the top
-u ls -u Sorts files according to the last access time, starting with the most
recent file
-i ls -i Shows inode number of all the files

While listing and searching for files and directories, we can also make use of wild-card
characters. These characters help in finding files and directories that begin with specific
character(s), contain specific character(s) or a range of characters in their names, consist of
names of a specific length, and so on. They provide a quick and convenient way of searching
for the desired files and directories.
Wild card matching A string is a wild-card pattern if it contains one of the following
characters: ‘?’, ‘*’, or ‘[’.
Basic Unix Commands 31

1. ‘?’ matches any single character.


2. ‘*’ matches 0 or more instances of any character, that is, it also matches the empty string.
3. [c1-c2] matches a single instance of any character within the range c1 and c2. For
example, [a-d] is equivalent to [‘abcd’]. Similarly, [0–9] represents a value from 0 to 9.
[a-zA-Z] represents all lower-case and upper-case letters.
We will learn more about wild cards and filename substitution (globbing) at the end of this chapter.
In order to understand these options, let us for a while assume that on giving the ls command
in the current directory, we get a list of the following files and directories:
$ls
courses
notes.txt
programs.doc
university
..

To get all the files beginning with a specific character, we can give a command using the
following syntax:
Syntax $ ls charactername*

Here, * represents 0 or more occurrences of any character.


Example In order to obtain all the files beginning with character n, we can give the
following command.
$ ls n*
notes.txt

In order to get all the files beginning with a character in a given range, we give the command
in the following syntax.
Syntax $ ls [c1-c2]*

Here, c1 and c2 represent the beginning and ending character of the range, respectively.
Example In order to get all the files beginning with characters a to d, we can give the
following command.
$ ls [a-d]*
courses

Similarly, we can use the wild-card character, ?, which represents a single character, to get
the desired files. For example, to get all the files that consist of three characters and begin
with character a, we can give the following command:
$ ls a??
However, since none of the files meet these criteria (assuming no filename exists that is three
characters long and begins with character a), we will not get anything as the output.
In order to get all the files that begin with character a followed by any digit, we can give
the following command:
$ ls a[0-9]*
32 Unix and Shell Programming

Again, as we can see, no file that begins with character a is followed by a digit in our list of
directories and thus no output is generated.
If we use the –l option for long listing, we may get the following output:
$ls –l
-rwxr--r-- 2 chirag it 48 Nov 11:31 courses
-rw-rwxr-- 1 chirag it 669 Dec 09:15 notes.txt
-rwxrwxrwx 1 chirag it 1560 Nov 11:21 programs.doc
-rwxr-xrw- 2 chirag it 65 Dec 05:10 university

Seven attributes are displayed: file permissions, number of links, owner, group, size, date
and time, and file/directory name.
In order to see all the files, including the hidden files, we use the –a option. The output is
as follows:
$ls –al
-rwxr--r-- 2 chirag it 80 Nov 11:31 .
-rwxr--r-- 2 chirag it 72 Nov 11:31 ..
-rwxr--r-- 1 chirag it 210 Nov 11:31 .profile
-rwxr--r-- 2 chirag it 48 Nov 11:31 courses
-rw-rwxr-- 1 chirag it 669 Dec 09:15 notes.txt
-rwxrwxrwx 1 chirag it 1560 Nov 11:21 programs.doc
-rwxr-xrw- 2 chirag it 65 Dec 05:10 university

Note: Filenames that begin with the dot (.) are considered hidden files in Unix.

By default, the file and directory names are sorted alphabetically. We can use the –t option to
sort them according to the modification time; the file that is created last is displayed at the top.
$ls –lt
-rwxrwxrwx 1 chirag it 1560 Nov 11:21 programs.doc
-rwxr--r-- 2 chirag it 48 Nov 11:31 courses
-rwxr-xrw- 2 chirag it 65 Dec 05:10 university
-rw-rwxr-- 1 chirag it 669 Dec 09:15 notes.txt

In order to get the inode number of the specified file, we can use the –i option, as shown here:
$ ls –li programs.doc
39984 -rwxrwxrwx 1 chirag it 1560 Nov 11:21 programs.doc

The digit 39984 is the inode number of the file programs.doc. Let us recall a concept from
Chapter 1: each file or directory in the Unix operating system has a unique number known as
inode number, which recognizes the file or directory in the file system.

➢ mkdir: Making directories


The mkdir command enables us to create one or more new directories.
Syntax $ mkdir –[mp] dirname

The option –m stands for mode and is used for creating the directory with certain specific
permissions.
Basic Unix Commands 33

The option –p stands for parent and is first used for creating all the non-existing parent
directories that are mentioned in the given path.
dirname is the directory name that may be either an absolute path name or a relative path
name. We may specify more than one directory name on a single command line.
Note: Absolute and relative paths—A path refers to the exact location of a given file or directory. Basically,
directories exists in a tree hierarchy, one inside another, and a directory or file is referred through a path, where
the path components are delimited by the forward slash (/).
A path can be an absolute path or a relative path. The absolute path points to the given file or directory
regardless of the current working directory and is written in reference to the root directory, whereas the relative
path is a path for a given file or directory in relation to the current working directory. Remember, the absolute
path always starts with a forward slash, which represents the root directory. Moreover, the absolute path of the
given file or directory is always the same, whereas the relative path changes according to the current directory
location. The following are the examples:
(a) Assuming a directory projects exists inside another directory usr, exists on the root and that the current
working directory is usr, the following are the two paths to the projects directory:
Absolute path: /usr/projects
Relative path: projects
(b) Similarly, if there is another directory experiment inside the directory /usr, and the current working
directory is projects, then the following are the two paths to the experiments directory:
Absolute path: /usr/experiments
Relative path: ../experiments

Consider the following example.


$ mkdir courses

This command creates a directory by the name courses under the current directory.
$ mkdir courses faculty placement

This command will create three directories by the names courses, faculty, and placement.
Note: If dirname already exists, the mkdir command aborts and does not overwrite the existing directory.

$ mkdir courses

Since a directory with the name courses already exists, this command generates the following
error:
mkdir: can't make directory courses

By default, the directories are created with read, write, and execute permissions for owners and
with read and execute permissions for groups and others, respectively. However, in order to create
a directory with a particular set of permissions of our choice, we can use the following command:
$ mkdir –m 746 country

This command creates a directory country with read, write, and execute permissions for the
owner; only read permission for the group; and read and write permissions for others.
The option –p stands for parent and is used for creating a parent directory in the given path.
34 Unix and Shell Programming

Example $ mkdir –p university/colleges/professors

This command creates a directory university; within the university, a subdirectory


colleges gets created, and under that, a sub-subdirectory professors is created.
There are several situations in which the directory is not created and the mkdir command
is aborted while displaying the following error:
mkdir: Failed to make directory

The reasons for this error can be any of the following:


1. A directory with the same name already exists.
2. An ordinary file by the same name exists in the current directory.
3. The user does not have the read and write permissions to create files and directories in
the current directory.
Line-continuation characters Sometimes, we come across commands that are too long to
be accommodated in a single line. Such commands are continued in the next consecutive line by
making use of the line-continuation character. The line-continuation character is \ (backslash).
Hence while writing a long command, when we come to the end of a line, we have to type
\ (backslash) and press the Enter key without any space or character following it. The shell
displays the ‘>’ symbol to indicate that the current line is in continuation of the previous line. We
can continue typing the remaining part of the command to the right of the ‘>’symbol and press the
Enter key when the command is over in order to get its output. We can use the line-
continuation character, that is, \ (backslash) any number of times for a single command.
Figure 3.1 demonstrates this by using the mkdir command for creating three direc-
tories, courses, faculty, and placement, while using the line-continuation character
(i.e., backslash).

➢ cd: Changing directories


We use the cd command to change to any directory in the file system.
Syntax $ cd pathname

Here, path name is either an absolute or a relative path name for the desired target directory.
Example $ cd ajmer

This command changes our current directory to ajmer (that is assumed to exist in the current
directory). When we directly give the directory name (without using ‘/’ as prefix), it means
that it is a relative path (i.e., a path related to the current directory).
$ cd /home/chirag/ajmer
$ mkdir courses \
> faculty \ This command takes us to the sub-subdirectory ajmer, which is in the chirag
> placement
subdirectory of the home directory. The path used in the aforementioned
$ example is an absolute path.
Fig. 3.1 Line-continuation
$ cd ..
character used in the
mkdir command This command takes us to the parent directory.

www.allitebooks.com
Basic Unix Commands 35

Note: .. refers to the parent directory.

We can return to our home directory from any other directory by simply typing the
cd command without an argument. We do not need to specify our home directory as an
argument, because our shell always knows the name of our home directory.

➢ rmdir: Removing directories


This command is used to remove a directory.
Syntax $ rmdir [-p] pathname

Here, the –p option is used for deleting the parent directory if it is empty.
Note: The rmdir command cannot remove a directory until it is empty.

Examples
(a) In order to remove a single directory, consider the following example.
$ rmdir ajmer

This removes the directory ajmer if it is empty; else, we will get the following error:
rmdir: ajmer: Directory not empty

(b) We can delete more than one directory using the following single command.
$ rmdir courses placement

The directories that are empty will be deleted with this command.
$ rmdir university/colleges/professors university/colleges university

This command deletes the professors sub-subdirectory from the colleges subdirectory;
then it deletes the colleges subdirectory from the university directory and finally, from
the university directory.
We can get the same result using the –p option as follows:
$ rmdir –p university/colleges/professors
Remember, we cannot use rmdir to remove our current working directory. If we wish to
remove our working directory, we have to first come out of it.

➢ pwd: Print working directory


pwd stands for print working directory. It displays the absolute path name of our working
directory.
Syntax $ pwd

Example $ pwd
/home/chirag

This output indicates that we are in the home directory of the user ID chirag. We can see that
the pwd command displays the full path name of the current directory.
36 Unix and Shell Programming

The pwd command is a valuable utility when we are moving around in the file system
hierarchy. If we change our directory, pwd confirms the change of our location, as shown in
the following sequence of commands:
$ pwd
/home/chirag
$ cd ajmer
$ pwd
/home/chirag/ajmer

We can see that when we change our directory to the ajmer subdirectory, the output displayed
by the pwd command confirms the same.

➢ uname: Displaying information about current system


The uname command displays information about the current system: hardware platform,
name of the operating system, and release level.
Syntax uname [-a] [-i] [-n] [-r] [-v] [-s] [-S system_name]

The options and arguments shown in the aforementioned syntax are briefly explained in
Table 3.2.
Table 3.2 Brief description of the options in the uname command
Options Description
-a Displays basic information currently available in the system
-i Displays the name of the hardware platform
-n Displays the node name, the name by which it is connected to the communication network
-r Displays the operating system release level
-v Displays the operating system version
-s Displays the name of the operating system (default)
-S Used to get basic information of the specified system name (Only the super user can use this
option.)

Note: Super user and root user refer to the Unix administrator.

Examples
(a) $ uname -a
SunOS station1 5.10 Generic_147441-01 i86pc i386 i86pc
This output shows the basic information of the system, including the hardware
platform, the operating system, its version, and so on.
(b) $uname -n
station1
This output shows that our machine is connected in the network by name, station1.
(c) $uname -i
i86pc
This output indicates that our machine is using a 64-bit processor.
Basic Unix Commands 37

(d) $uname -r
5.10
This output shows the operating system release level.
(e) $uname -s
SunOS
This output indicates that our machine has a Linux operating system installed.

➢ touch: Creating files and changing time stamps


The touch command is used for creating files and changing time stamps. Here, time stamps
means both the times, that is, the time the file was last accessed and the time the file was last
modified.
Syntax touch –[ma] time_expression filename

Here, the –m option is used for changing the modification time, and the –a option is used
for changing the access time. The time_expression that we would provide should be in the
following format: MMDDhhmm, where M: month, D: day, h: hour, and m: minute.
When the touch command is given without any option and time expression, it simply
creates a file of zero bytes.
Examples
(a) $ touch chirag.txt
This creates a file called chirag.txt of zero byte.
We can create several empty files quickly with the touch command.
(b) $ touch chirag1 chirag2 chirag3 chirag4
This command creates four new files with the following names: chirag1, chirag2,
chirag3, and chirag4 (without any contents in them).
(c) $ touch 09211520 chirag.txt
This sets the modification and access time of the file chirag.txt to Sep 21 15:20.
(d) $ touch –m 11071015 chirag.txt
This command sets the modification time of the file chirag.txt to Nov 07 10:15.
(e) $ touch –a 07120820 chirag.txt
This will set the access time of the file chirag.txt to Jul 12 08:20.
Note: The commands ls –l and ls –lu can also be used to set the modification time and access time,
respectively, of any file.

➢ cat: Showing, creating, and concatenating files


The cat command is basically a short form of concatenate, which means ‘combine’. With
the cat command, we can not only create files and see their contents but also combine their
contents. The operator > can be used with the cat command to combine multiple files into a
single one. The operator >> can be used to append to an existing file.
Syntax cat [-n] [-s] [-v] files

The options and arguments shown in the aforementioned syntax are briefly explained in
Table 3.3.
38 Unix and Shell Programming

Table 3.3 Brief description of options available with the cat command
Options Description
-n It precedes each line output with its line number.
-s It suppresses messages when non-existent files are used in the command.
-v It displays non-printing characters, except tabs, new lines, forms, and feeds, that exist in a file. To
display new lines, the -e option is used along with the –v option. To display tabs and form feeds, the -t
option is used along with the –v option. The new lines are represented by ‘$’, tabs are represented by
‘^I’, and form feeds are represented by ‘^L’.

Showing content To display the contents of any file, we just need to specify the filename
after the cat command
$ cat chirag

This command shows the contents of the file chirag.


If more than one filename is specified, cat will display the contents of all the files one after the
other, that is, the contents of the first file followed by the contents of the second file, and so on.
$ cat chirag notes.txt
This command displays the contents of the file chirag followed by the contents of the file
notes.txt.
To get the line numbering along with the file contents, we use the –n option with the cat
command:
$cat –n chirag
1 Today it might rain
2 Thanks so much
3 This is supposed to be a leap year

Note: We assume that the file chirag contains a couple of tabs that are deliberately added to the file.

Creating files For creating files through the cat command, we redirect the standard output
to a file instead of the monitor, as shown in the following example:
$ cat >chirag

If we press the Enter key, we would find the cursor positioned in the next line, waiting to
type the matter that we want to store in the file chirag. After typing a few lines, press Ctrl-d.
Note: Ctrl-d keys indicate the end of file character (EOF).

Example $ cat >chirag


Today it might rain
Thanks so much
This is supposed to be a leap year
Ctrl-d

Showing hidden characters in files The following command shows the hidden characters
and new lines in the form of $ (refer to Fig. 3.2):
Basic Unix Commands 39

Today it might rain$ TodayᶺIitᶺImightᶺIᶺIrain


Thanks so much$ Thanks so much
This is supposed to be a leap year$ This isᶺIsupposedᶺIto be a leap year

Fig. 3.2 New lines displayed in the form of $ Fig. 3.3 Tabs and form feeds displayed as ^I and ^L

$ cat –ve chirag

The following command shows the hidden characters and tabs in the form of ‘^I’ and form
feeds as ‘^L’ as shown in Fig. 3.3.
$ cat –vt chirag

The cat command, apart from displaying the contents of the file, also helps concatenate the
contents file.
Concatenating files To concatenate the contents of two files and store them in the third
file, we can use the following command:
$ cat chirag1 chirag2 >chirag3

This command stores the contents of the file chirag1 followed by the contents of the file
chirag2 into the file chirag3. If chirag3 already contains something, it would be overwritten.
If we want it to remain intact and the contents of chirag1 and chirag2 to be appended, we
should use the following command:
$ cat chirag1 chirag2 >>chirag3

➢ cp: Copying files


The cp command is used to create a duplicate copy of ordinary file(s) in another name.
Syntax $ cp –[ir] srcfile destfile

Here, srcfile is the original or source filename, and destfile stands for destination filename.
If a file by the destination filename already exists, it will be overwritten with the contents of
the source file without any warning.
The option –i is used for interactive copying, that is, if a file by the destination filename
already exists, then cp will prompt us before overwriting the file.
The option –r is used for recursive copying and especially when we want to make a copy
of an entire directory (along with its subdirectories and files) using another directory name.
Example $ cp chirag chirag1
This example makes a copy of the file chirag in the name chirag1. We can confirm this by
looking at the content of both the files. If the contents of both the files are found to be the
same, it indicates that the chirag file is successfully copied in the name chirag1. With the
help of the cat commands, we can look at the contents of the files chirag and chirag1.
$ cat chirag
Microchip Computer Education
Sri Nagar Road, Ajmer
Gone time never returns
40 Unix and Shell Programming

$cat chirag1
Microchip Computer Education
Sri Nagar Road, Ajmer
Gone time never returns

The contents of chirag and chirag1 are found to be the same and hence, this confirms that
the file chirag is copied in the filename chirag1.
$ cp /home/chirag/ajmer/a.bat .

This command copies the file a.bat from the directory ajmer (subdirectory of chirag)
into the current directory. The period (.) at the end of the cp command denotes the current
directory.
For interactive copying, we use the following command:
$ cp –i chirag chirag1

If a file by the name chirag1 already exists, then, before overwriting it, we will be notified
with the following message:
cp: overwrite chirag1 (yes/no)?

Here, we need to enter y followed by the Enter key if we want to overwrite the file.
For copying an entire directory along with its subdirectories, we use the following
command:
$ cp –r courses latestcourses

It will make a copy of the courses directory (along with its files and subdirectories) with the
name latestcourses.

➢ mv: Renaming files


The mv (move) command changes the name of a file. Basically, using the mv command, the
file is removed from its current location and is copied to another location. In the directory
entry, the link to the old filename is removed, and it is replaced by a link to the new filename.
Syntax $ mv oldname newname

This command in the syntax will change the filename from oldname to newname.
Example $mv chirag chirag2

This command moves or renames the file chirag to chirag2. When we look at the contents
of the file chirag2, we get the same contents that were in the file chirag, which is shown
here.
$ cat chirag2
Microchip Computer Education
Sri Nagar Road, Ajmer
Gone time never returns

It indicates that the file chirag2 is nothing but the same file that existed earlier with the name
chirag.
Basic Unix Commands 41

$ mv chirag2 ajmer
or
$ mv chirag2 /home/chirag/ajmer

This moves the file chirag2 into the ajmer subdirectory. Now, the file chirag2 is no longer
available in the current directory.
For moving more than one file, we use the following command:
$ mv notes.txt programs.doc /home/chirag/ajmer
The files notes.txt and programs.doc are removed from the current location and moved to
the ajmer subdirectory.

➢ rm: Removing files


This command removes one or more ordinary files from a directory. The file is removed by
deleting its pointer in the appropriate directory. In this way, the link between that filename
and the physical file is broken, hence, the file can no longer be accessed.
Syntax $ rm -[irf] filename

Here, each filename is separated by white space. The options and arguments shown in the
aforementioned syntax are briefly explained in Table 3.4.
Table 3.4 Brief description of options available with the rm command
Options Description
-i It is used for interactive file deletion, i.e., we will be prompted for confirmation before the
file is deleted.
–r It is used for recursive deletion, i.e., it is used for removing an entire directory along with
its files and subdirectories.
–f It is used to forcibly remove a file for which we do not have the write permission.

We will learn about file permissions shortly.


Examples
(a) $rm notes.txt
This command deletes the files notes.txt.
(b) $rm –i programs.doc
This command prompts us for confirmation before removing the file. The prompt may
be as follows:
rm: remove programs.doc (yes/no)?
To delete the file, we type y followed by the Enter key (or type character n in case we
do not want to delete the file).
(c) We can delete more than one file using a single rm command.
$ rm syllabus notes.txt programs.doc
This command deletes all the three files syllabus, notes.txt, and programs.doc.
(d) $ rm –r courses
This command removes all the files and subdirectories of the courses directory and,
finally, the courses directory itself.
42 Unix and Shell Programming

(e) To remove a file that is write protected (for which we do not have the write permission),
we can use the following command.
$ rm –f results
This command deletes the results file even if we do not have the write permission for
doing so.

➢ ln: Linking files


The Unix file system allows the creation of more than one filename for the same physical
file. In other words, it is possible to have aliases (links) for any given file.
The ln command may be used for establishing additional links. There are two types of
links—hard as well as symbolic links—and both can be created with this command.
Syntax $ ln –[sf] oldname newname

After the ln linking, both newname and oldname refer to the same file.
The default link type is hard. In order to create a symbolic link, the symbolic option (-s)
is used.
Example $ln chirag1 mce1
$ls

Through this command, a hard link will be created for the file chirag1 by the name mce1.
Note: When the –s option is not used with the ln command, a hard link is created.

We get several filenames and directories in the current directory along with the two filenames
mce1 and chirag1, and when we write the following command,

$ ls -l chirag1

we get the following output:


-rwxrwxrwx 2 chirag it 7669 Nov 11:21 chirag1

The group of rwx is the permissions for owners, groups, and others; 2 is the number of
links (also known as link count) of the file; and chirag is the owner. The group name is it.
The size of the file is 7669 bytes. Next comes the date and time the file was last modified.
The output ends with the filename chirag1.
If another link was to be created, the link count would change to 3.
Note: A link count is an integer value that is maintained for each file or directory and indicates the total number
of links pointing to it. When a new link is created, the link count value is increased by one. Similarly, when a link
is removed, the value is decreased by one. When a link count becomes zero, it means the file or directory has
no links, and hence, the disk space allocated to it is deallocated.

Both mce1 and chirag1 point to the same file. When we look at the contents of the file mce1,
we get the same contents as in chirag1.
$cat mce1
Microchip Computer Education
Sri Nagar Road, Ajmer
Gone time never returns
Basic Unix Commands 43

Note: If we change the contents of the file mce1, the contents of chirag1 will also change, because although
the names mce1 and chirag1 are different, both of them refer to the same file.

To see the inode numbers of the linked files, we give the following command:
$ls -li ichirag1 mce1
20985 -rwxrwxrwx 2 chirag it 320 Nov 11:21 chirag1
20985 -rwxrwxrwx 2 chirag it 320 Nov 11:21 mce1

The -li option with the ls command displays the inode number along with the long listing
of the specified files. We can see that both the files have the same inode number, 20985,
which confirms that both point to the same file.
In order to remove a file with more than one link from the file system, we should delete all
the links with the rm command. For example, let us delete the link mce1 using the following
command:
$ rm mce1

The file still exists under the name chirag1 as confirmed by the following command:
$cat chirag1
Microchip Computer Education
Sri Nagar Road, Ajmer
Gone time never returns

Let us remove the file chirag1.


$ rm chirag1

The file is now completely inaccessible.


If the destination file already exists, the link will not be created. Assume we want to make
xyz.txt as the link of the file abc.txt and xyz.txt is already an existing file that has some
contents. Let us give the following command:
$ ln abc.txt xyz.txt

The link will not be created and the following error will be displayed:
ln: xyz.txt: File exists

The –f option stands for force option and is used when we want to overwrite an existing file
(while creating a link) without getting any message.
$ ln -f abc.txt xyz.txt

Hard links The default link that is created is a hard link (which we have been using until
now). The following are the characteristics of hard links:
1. Unix hard links can point to programs and files, but not to directories.
2. If the original program or file is renamed, moved, or deleted, the hard link is not broken.
3. Hard links in Unix cannot span different file systems, that is, we cannot have a hard link on
the /usr file system that refers to a program or file on the /tmp file system. The reason is that
hard links share an inode number, whereas each file system has its own set of inode numbers.
44 Unix and Shell Programming

Symbolic links Hard links cannot be created for different file systems. that is, they can
be made within the current directory structure. Symbolic links (symlinks) are used to link
to a different file system. The symbolic link, also referred to as soft link, is a special type
of file that references another file or directory. It simply contains the name of the file that it
references and contains no actual data. It gives us power and flexibility to manage files. We
can change the symlink to point to the desired files. Soft links also inherit the permission of
the folder they are pointing at. To create a symbolic link in Unix, let us use the following
syntax:
Syntax ln -s target_file symbolic_link

Here, target_file is the name of the existing file for which we want to create the symbolic
link, and the symbolic_link is the symbolic link for the target_file.
Example Consider a file named chirag1. Let us create a symlink called mce1, which points
to the original file, inventory.txt.

$ ln -s chirag1 mce1

We first specify the target file, the file that we want our symlink to point to, and then specify the
name of our symbolic link. On executing the ls -al command, we will find that the mce1 file
will have an ‘l’ in the long format of the ls command, which confirms that it is a symbolic link.

Note: An orphan symlink is a symbolic link that points nowhere, that is, the original target file it used to point
to earlier is either deleted or renamed.

20985 -rwxrwxrwx 2 chirag it 320 Nov 11:21 chirag1


20985 lrwxrwxrwx 2 chirag it 320 Nov 11:21 mce1->chirag1

The difference between symbolic link and hard link is that the symbolic link has the ability
to link to directories or files on remote computers. In addition, when you delete a target file,
the symbolic links to that file become unusable, whereas the hard links preserve the contents
of the file.

➢ unlink: Deleting symbolic links


The unlink command removes the specified file, including symbolic links.
Syntax unlink filename

Example unlink accounts.txt


If this file accounts.txt exists as a linked file, it will be deleted.

➢ tput: Exploiting terminal capabilities


The tput command is used for exploiting terminal capabilities through the terminfo database.
Hence, we can use the tput command to clear the screen, move the cursor, and underline
text. The tput command uses the terminfo database to know the different features that are
supported by a terminal, and converts the commands given by the user through the tput
command into the code that the terminal understands.
Basic Unix Commands 45

Terminfo is a database that defines terminal and printer attributes and capabilities. It contains
information such as the number of rows and columns in a terminal and the attributes of text
displayed on the terminal.
Syntax tput [clear][cup col row] [cols][lines][sc][rc][civis][cnorm][dl n][setb]
[setf][bold][sgr0][smul[rmul]

Table 3.5 gives a list of options available with the tput command.

Table 3.5 Brief description of the options available with the tput command
Options Description
clear It clears the whole screen.
cup col row It moves the cursor position to the given row and col position.
cols It displays the number of columns on the terminal screen.
lines It displays the number of lines on the terminal screen.
sc It saves the current cursor location.
rc It restores the cursor position, i.e., it returns the cursor to its last saved location.
dl n It deletes n number of lines below, including the current row, i.e., the row in which the
cursor is positioned.
bold It makes the text appear in bold.
sgr0 It turns off bold.
Smul It begins underlining text.
Rmul It stops underlining text.

Examples
(a) $ tput cup 10 5
This statement moves the cursor to the fifth row and the tenth column.
(b) $ tput cols
This statement displays a value 80, which represents the number of columns of the
terminal screen.
(c) $ tput dl 4
This statement deletes four lines below, including the current row.
(d) $ tput bold
This statement will make the text appear in bold until the srg0 command is invoked.
(e) $ tput clear
It clears the whole screen.

Note: We know that the tput command is mostly used in scripts but is deliberately provided here, as its option
clear is frequently used for clearing the screen while running commands at the command prompt.

➢ who: Who is online


The who command displays all users who are currently logged in to the system. It returns the
user’s name (ID), terminal, and the time at which he or she logged in.
46 Unix and Shell Programming

Table 3.6 Brief description of options in the who command


Options Description
-u It displays information regarding users who are logged in. Their login name, name of the terminal through
which they are logged in, and the date and time of login are displayed.
-H It displays information pertaining to the users who are logged in along with the column headings.
am I It displays information of the users who are logged in.

Syntax who [-u] [-H] [am i]

These options are briefly explained in Table 3.6.


Examples
(a) $ who
anil tty1 Oct 15 10:56
chirag tty2 Oct 10 11:25
ravi tty5 Oct 15 13:07

To know whether the user is active, we use the -u option, which also indicates how long it
has been since there was any activity. This is known as idle time. It also returns the process
ID for the user.
$ who -u
anil tty1 Feb 10 14.25 0:45 1103
chirag tty2 Feb 08 11:25 old 1568
ravi tty5 Feb 10 15:10 . 1456

If we look at this example carefully, we will see three different formats for idle time. The
first user has had no activity for 0 hours and 45 minutes. The second user has had no activity
for more than 24-hours. Since there is only enough room for 24 hours in the idle time format,
when a user is inactive for more than 24 hours, the system simply says ‘old’. The third
user’s idle time is a period (.), that is, he/she has carried out an activiy in the last minute.
(b) If we use the H option, Unix displays a header that explains each column.
$ who -uH
NAME LINE TIME IDLE PID COMMENTS
anil tty1 Feb 10 14.25 0:45 1103 (:0)
chirag tty2 Feb 08 11:25 old 1568 (:0.0)
ravi tty5 Feb 10 15:10 . 1456 (:0.0)
(c) If we want to view information about ourselves, we can use the argument am I along with
the who command.
$who am I
chirag tty2 Feb 08 11:25

➢ finger: Online user’s details


The finger command displays information of users who are logged in. Compared to the
who command, the finger command displays more elaborate information pertaining to these
Basic Unix Commands 47

Table 3.7 Brief description of the options in the finger command

Options Description
-l Displays information of the user in a long format comprising login name, real name, terminal name, write status,
idle time, login time, office location, office phone number, user’s home directory, home phone number, login
shell, mail status, and the contents of the files, .plan, .project, and so on, from the user’s home directory
-s Displays information of the user in a short format comprising login name, real name, terminal name, write
status, idle time, login time, office location, and office phone number
-b Suppresses printing the user’s home directory and shell in a long format display
-w Suppresses printing the full name in a short format display

users. Apart from the login name, terminal name, date, and time of the logged-in users, the
command also displays other information such as the user’s home directory, phone number,
login shell, and mail status, among others.
Syntax finger [-b] [-l] [-s] [-w] [username]

These options are briefly explained in Table 3.7.


If no options are specified, the finger defaults to the -l option output if username is provided;
else, the -s option output is chosen. In the case of fields whose information (such as office
location and phone number) is not available, the information will not be displayed in the output.
To find out who is logged in to which terminal, we use the finger command without an
argument, as in the following example.
Example $ finger
Login Name TTY Idle When Where
chirag chirag tty2 3:10 Sat 08:15 :0
ravi ravi tty3 Sat 11:33 :0.0
root root tty1 10d Sat 08:15 :0.0

This list shows three active logins. The actual time at which each user logged in and the time
the terminal has been idle are also listed. The idle time is the time that has elapsed since the
last keystroke. From the idle time, we can usually tell whether someone is at the terminal.
For example, we can say that there is no root user at the tty1 terminal, because there has
been no keystroke for 10 days. The user on the tty2 terminal has not used the terminal for
more than three hours.
The finger command can also be used to get the details of a single user, as shown in the
following example.
$ finger chirag
Login: chirag In real life: (null)
Directory: /home/chirag Shell: /bin/bash
On since Mon Dec 26 02:15 on tty2 from :0.0
No mail.
No Plan.

This output shows the login name, real name (null), home directory, login shell, login time,
terminal name, mail status, and so on.
48 Unix and Shell Programming

➢ date: Displaying system date and time


This command displays the system date and time.
$ date

If no argument is given, the current date and time are displayed:


Sunday 12 February 2012 05:32:20 AM IST
The command can also be used with suitable format specifiers as arguments. Each format is
preceded by a + symbol followed by the % operator, and a single character describing the format.
Syntax date [arguments]

The arguments are used for displaying the date in the desired format. The list of available
arguments is given in Table 3.8.
Table 3.8 Brief description of the arguments used in the date command
Arguments Description
%d For displaying day (01–31)
%m For displaying month (01–12)
%b For displaying abbreviated month name (Jan, Feb, etc.)
%y For displaying the year—last two digits (00,…, 99)
%Y For displaying the year with century—four digits
%H For displaying hours—military format (00,01,…, 23)
%I For displaying hours (0,1,…, 12)
%p For displaying a.m./p.m.
%M For displaying minutes (0,1, …, 59)
%S For displaying seconds (0,1,…, 59)
%x For displaying only date (07/15/12)
%X For displaying only time (17:15:30)
%a For displaying abbreviated weekday (Fri)

Examples
(a) $ date + %m
It prints only the month, that is, 07.
(b) $ date +%b
It prints the month name, that is, Jul.
(c) $ date +%Y
It prints the year with century, that is, 2012.
(d) $date + "%I %p"
It displays the hour with a.m./p.m.
05 PM

➢ cal: Displaying calendar


The cal command is used to display the calendar of a specified month and year.
Basic Unix Commands 49

Syntax cal {month [1-12]} {year[1-9999]}

Here, values 1–12 represent the month, and values 1–9999 represent the year.
Examples
(a) To display the current month’s calendar, just use the cal command without any arguments
(refer to Fig. 3.4).
$cal
(b) To display the calendar of March 2012, write the following command (refer to Fig. 3.4).
$ cal 3 2012
(c) To display the calendar for a whole year, specify the year in the cal command as shown
in Fig. 3.5.
$ cal 2012

➢ echo: Displaying messages and results


The echo command is used to display messages and the results of computation on the screen.
Syntax echo [-n] message/variables

These options are briefly explained in Table 3.9.


Table 3.9 Brief description of an option in the echo command
Option Description
-n It suppresses a new line after the echoed message or variables. The output of the next echo
statement will appear on the current line. This option is usually used while scripting.

The echo command recognizes the following Escape characters:


\\ represents backslash.
\a rings a bell.
\b represents the backspace key.
\f represents the form feed.
\n represents a new line character.
\r represents carriage return.
\t represents a horizontal tab character.
\v represents a vertical tab character.
Example echo "Hello World"

This example displays the message, Hello World on the screen.


Figure 3.6 shows how Escape sequences can be used with the echo command. We can see that
the \n results in a new line character, displaying the following word, World that appears on the
next line. Similarly, \t results in a horizontal tab between the words Hello and World. The third
example displays backslash (\) between Hello and World. The fourth example shows how \b
takes the cursor one character back, hence overwriting the character ‘o’ of Hello and displaying
the word, HellWorld. The fifth example inserts a vertical tab between the words Hello and World.

➢ bc: Basic calculator


The bc command activates a basic calculator that is meant for doing simple calculations. The
command is executed in the interactive mode, that is, we enter the expression we wish to compute
50 Unix and Shell Programming

# cal # cal 3 2012 $ echo "Hello\nWorld"


Hello
April 2012 March 2012 World
S M Tu W Th F S S M Tu W Th F S $ echo "Hello\tWorld"
1 2 3 4 5 6 7 1 2 3 Hello World
8 9 10 11 12 13 14 4 5 6 7 8 9 10 $ echo "Hello\\World"
15 16 17 18 19 20 21 11 12 13 14 15 16 17 Hello\World
22 23 24 25 26 27 28 18 19 20 21 22 23 24
$ echo "Hello\bWorld"
29 30 25 26 27 28 29 30 31 HellWorld
$ echo "Hello\vWorld"
Fig. 3.4 Calendar of current month and specified month Hello
World

Fig. 3.6 Echo command


output

Jan Feb Mar


S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 7 1 2 3 4 1 2 3
8 9 10 11 12 13 14 5 6 7 8 9 10 11 4 5 6 7 8 9 10
15 16 17 18 19 20 21 12 13 14 15 16 17 18 11 12 13 14 15 16 17
22 23 24 25 26 27 28 19 20 21 22 23 24 25 18 19 20 21 22 23 24
29 30 31 26 27 28 29 25 26 27 28 29 30 31

Apr May Jun


S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 26 27 28 29 30

Jul Aug Sep


S M Tu W Th F S S M Tu W Th F S S M Tu W Th F
S
1 2 3 4 5 6 7 1 2 3 4 1
8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8
15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15
22 23 24 25 26 27 28 19 20 21 22 23 24 25 23 24 25 26 27 28 29
29 30 31 26 27 28 29 30 31 30

Oct Nov Dec


S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 37 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31

Fig. 3.5 Calendar of the entire year


Basic Unix Commands 51

on the command line, and the command immediately displays the result on pressing the Enter
key. To quit the interactive mode, we either press Ctrl-d or type quit followed by the Enter key.
Syntax bc [-l]

-l defines the math functions and initializes the scale to 20, instead of the default zero.
The functions that can be used with the bc command are given in Table 3.10.
Table 3.10 List of functions available with the bc command
Function Description
sqrt() It calculates the square root of the supplied number.
s() It calculates the sine value. The argument should be in radians.
c() It calculates the cosine value. The argument should be in radians.
a() It calculates the arctangent. The result of the function is displayed in radians.
l() It calculates the natural logarithm of the supplied number.
e() It calculates the exponential of the supplied number.

We can use all operators including +, -, *, /, %, ^, where % represents the mod operator, that
is, it returns the remainder and ^ represents ‘to the power’.
Apart from the -l option, we can also use the scale to specify the number of digits to the
right of the decimal point.
Examples bc
$bc
5/3
1
quit

$ bc -l
5/3
1.66666666666666666666
quit
$ bc
2 + 2
4
5/3
1
scale = 2
5/3
1.66
3^2
9
sqrt(81)
9.00
quit
52 Unix and Shell Programming

$ x='echo "5/3" | bc -l'


$ echo $x
1.66666666666666666666

Table 3.11 Brief description of the wild cards used in Filename substitution—Globbing
filename substitution Filename substitution is the process by which the
shell expands a string containing wild cards into a list
Wild card Description
of filenames. The process of filename substitutions
! Used with [ ] to negate the meaning
is also known as globbing. Apart from the wild
~ Substitutes the user's home directory cards, *, ?, and [c1-c2], which we discussed while
{characters} Matches the given set of characters learning the ls command, Table 3.11 shows the
wild cards that are used in filename substitution.
Examples

(a) $ ls *
It displays all the names of the files and directories in the current directory.
(b) $ ls a*
It displays all the names of the files and directories that begin with the character a.
(c) $ ls *a
It displays all the names of the files and directories that end with the character a.
(d) $ ls *ab*
It displays all the names of the files and directories that contain ab.
(e) $ ls a*/*
It displays all the names of the files and directories that begin with the character a in all
the directories that are one level under the current directory.
The filename substitution applies to the files in the current directory. To match filenames
in the subdirectories, we need to use the / character.
(f) $ ls a*/*/*
It displays all the names of files and directories that begin with the character a in all the
directories that are two levels under the current directory.
(g) $ ls ???
It displays all the names of the files and directories that consist of three characters.
(h) $ ls ???*
It displays all the names of the files and directories that consist of at least three characters.
(i) $ ls student?.txt
It displays all the names of the files and directories that begin with the word student
followed by one character followed by extension .txt such as stduent1.txt, student2.
txt, and studenta.txt.
(j) $ ls [ab]*
It displays all the names of the files and directories that begin with either character a
or character b followed by zero or more occurrences of any character.
(k) $ ls [ab]*[12]
It displays all the names of the files and directories that begin with either character a or
character b followed by zero or more occurrences of any character and which end with
either the digit 1 or 2.
Basic Unix Commands 53

(l) $ ls [ab]*[1-5]
It displays all the names of the files and directories that begin with either character a or
character b followed by zero or more occurrences of any character and which end with
any digit from 1 to 5.
(m) $ ls [a-d]*
It displays all the names of the files and directories that begin with any character from a
through d followed by zero or more occurrences of any character.
(n) $ ls [a-d]??
It displays all the names of the files and directories that begin with any character from a
through d followed by exactly two characters.
(o) $ ls [!a-d]*
It displays all the names of the files and directories that begin with any character except
a through d followed by any number of characters.
(p) $ ls [A-Za-z]*
It displays all the names of the files and directories that begin with any character from a
through z in either upper case or lower case followed by any number of characters.
(q) $ ls [A-Za-z][a-z]*
It displays all the names of the files and directories that begin with any character from a
through z in either upper case or lower case, followed by any character from a through z
in lower case, followed by any number of characters.
(r) $ ls [A-Za-z][a-z][12]
It displays all the names of the files and directories that begin with any character from a
through z in either upper case or lower case, followed by any character from a through
z in lower case, followed by either digit 1 or 2.
(s) $ ls {aa,bb,cc}*
It displays all the names of the files and directories that begin with the characters aa, bb,
or cc followed by any number of characters.
(t) $ ls a*{d,1,z}
It displays all the names of the files and directories that begin with the character a
followed by any number of characters and, which end with d, 1, or z.
(u) $ ls a*{d,[1-3],[ab]}
It displays all the names of the files and directories that begin with the character a
followed by any number of characters and which end with d, a number from 1 through
3, or by either character a or b.
(v) The tilde (~) character by itself expands to the full path name of the user’s home directory.
The following echo command confirms this:
$ echo ~
/home/bintu

(w) When the tilde is appended before a path, it expands to the home directory and the rest
of the path name. Consider the following command.
$ cd ~/data

(x) We will be taken into the directory, data that is present within the user’s home directory.
The following pwd command confirms this.
54 Unix and Shell Programming

$ pwd
/home/bintu/data
(y) When the tilde is appended before a username, it expands to the full path name of that
user’s home directory. Consider the following command.
$ cd ~john
We will be taken to the user john’s home directory. The following command confirms this:
$ pwd
/home/john

➢ exit: Exiting
The exit command is used to log out of the Unix system, exit from a shell, and exit from a
shell script.
Syntax exit

Example exit
To log out of the Unix shell, Ctrl-d is a short cut that is used. Before exiting from the Unix
system, we should make sure that all the files that were open are saved and closed; else they
might get corrupted. Usually, when we exit from the shell, the currently running process
or command is automatically killed. In order to run the task in the background even after
exiting from the shell, we should use the nohup command (discussed in Chapter 6).

■ SUMMARY ■

1. When compared with the who command, the finger directory, phone number, login shell, mail status, and
command displays more elaborate information per- much more.
taining to users who are logged in. 3. Filename substitution or globbing is the process by
2. The finger command not only displays the login name, which the shell expands a string containing wild cards
terminal, date, and time of the logged-in users, but also into a list of filenames.
displays other information such as the user’s home

■ F U N C T ION SPECIFICATION ■

Command Function Command Function


ls To see a list of files and directories, including mv For moving files from one directory to another
hidden files as well as for renaming files
mkdir To create directories. We can also create passwd For changing the password
directories with specific permissions with this ln For creating links of files. There are two types
command. of links—hard links and symbolic links.
cd For changing a directory. We can use both who To know how many users are currently online
relative and absolute paths for changing the finger To know the current working directory
directory. touch To know who is online, when the user is
pwd To know the current working directory logged in, and for how long his/her terminal
touch To create empty files and also change the has been idle.
modification and access time of a file date For displaying system date and time
Basic Unix Commands 55

Command Function Command Function


cat For displaying contents of files, creating files, uname For displaying information of the current
and concatenating files system such as its hardware platform, name
rmdir For removing a directory provided it is empty of the operating system, and its release level.
cp For copying files as well as an entire directory unlink To remove the specified file, including symbolic
links
rm For deleting files as well as an entire directory
bc To activate a basic calculator that is meant for
cal To display the calendar of the specified month doing simple calculations. It can also be used
and year. By default, it displays the calendar to compute square root, sine value, cosine
of the current month. value, natural logarithms, and exponential
mv For moving files from one directory to another values.
as well as for renaming files exit To log out from the Unix system, a shell, or
cal-y To display the calendar of the current year a shell script

■ EXERCISES ■

Objective-type Questions
State True or False
3.1 The ls command shows the list of files and 3.10 We can delete more than one file with a single rm
directories that are sorted alphabetically by default. command.
3.2 The option used with the ls command to see the 3.11 With the rm command, we can forcibly delete a
names of the files and directories in reverse order file even if we do not have its write permission.
is -R. 3.12 With the mv command, we can move a file from
3.3 We can create only one directory at a time using one directory to another but cannot rename it.
the mkdir command. 3.13 The hard link should be created within the
3.4 The cd command, if given without any ar- current directory structure.
guments, will take us to our home directory. 3.14 We can log out of the Unix system using Ctrl-d.
3.5 With the touch command, we can only change 3.15 Through the cal command, we cannot see the
the timestamps of the files but cannot create files. calendar of the previous month.
3.6 While creating a file with the cat command, we 3.16 The mail status of the user can be seen through
need to use Ctrl-d to specify the end of the file. the finger command.
3.7 With the rmdir command, we can remove the 3.17 The uname command can be used to know the
non-empty directory as well. version and release of the operating system.
3.8 If we use the -i option with the cp command, it 3.18 The wild-card character ‘?’ represents a single
will prompt us before overwriting the destination character.
file if it already exists. 3.19 The bc or the basic calculator command can be
3.9 The cp command is used for making a copy of used to find the square root of a number.
the files; we cannot use it for copying an entire 3.20 The unlink command cannot delete symbolic
directory with its files and subdirectories. links.

Fill in the Blanks


3.1 The option used with the ls command to see 3.4 The command used to know our current working
hidden files is . directory is .
3.2 The option used with mkdir for creating directories 3.5 The format of time expression used to change
with specified permission is . modification or access time in the touch
3.3 With the cd command, we can give absolute as command is .
well as path names. 3.6 The option used with the rmdir command to
56 Unix and Shell Programming

delete an empty parent directory is . 3.12 The command used to display the calendar of the
3.7 The option is used with the rm current year is .
command to recursively delete all the files and 3.13 The command used to display information of
subdirectories of the specified directory. the logged-in user, including home directory,
3.8 The command used to create a link for a file is login shell, mail status, and phone number
known as . is .
3.9 There are two types of links to a file: 3.14 The option used with the ls command to display
and . the inode number of files is .
3.10 The option used with the date command to 3.15 The option used with the cat command that
display only the time is . displays non-printing characters in the file is
3.11 The function used to find the natural logarithm in .
the bc command is .

Multiple-choice Questions

3.1 The command bc-l sets the scale to 3.6 Apart from displaying contents of the files, the
(a) 20 (c) 10 command used for concatenating files is
(b) 5 (d) 6 (a) concat (c) merge
3.2 The tput cup 7 5 command moves the cursor to (b) cat (d) add_files
the 3.7 There are two types of links of files—hard and
(a) seventh row and fifth column (a) tough (c) volatile
(b) fifth row and seventh column (b) robust (d) symbolic
(c) top left corner of the screen 3.8 The echo command ~ will display
(d) right bottom corner of the screen (a) error
3.3 The command date +%M will display (b) list of files and directories
(a) month in character form (c) home directory of the user
(b) month in numerical form (d) profile file
(c) minutes 3.9 The following command is used to display the
(d) a.m./p.m. names of the files and directories that consist of
3.4 The option used in the cp command for interactive at least two characters:
copying is (a) ls??* (c) ls *
(a) -i (b) -r (c) -c (d) -d (b) ls (d) ls ?*
3.5 The following option is used in the cat command 3.10 The option used in the ls command to show files
to suppress messages when a non-existent file is and directories that are sorted on their modi-
used in the command: fication time is
(a) -o (b) -v (c) -n (d) -s (a) -m (b) -a (c) -t (d) -u

Programming Exercises
3.1 What will the following commands do? txt/college/students
(a) $ls [a-d]?? (j) $ rm -r college
(b) $ls [a-z][0-9]* (k) $ mv mbacourse.txt management.txt
(c) $ls -Rt (l) $ ln -f juice.txt energy.txt
(d) $mkdir -m 740 apple (m) $ finger Charles
(e) $mkdir -p fruits/delicious/apple (n) $ bc
(f) $touch 07151000 mbacourse.txt scale = 2
(g) $ cat mbacourse.txt lawcourse.txt 17/3
(h) $rmdir -p fruits/delicious/apple (o) $cal 10 2012
(i) $ cp /fruits/delicious/apple/juice. 3.2 Write the command for the following tasks:
Basic Unix Commands 57

(a) To display the list of files and directories (i) To change the password
that begin with a vowel (j) To create a link of the file mbacourse.txt in
(b) To change the access time of the file the name management.txt (If a file by the
mbacourse.txt to Feb 10 09:15 name management.txt already exists, we
(c) To show the contents of the file mbacourse. should be asked for a confirmation before
txt along with line numberings overwriting its contents.)
(d) To concatenate the contents of the two files (k) To get the list of all online users with their
mbacourse.txt and lawcourse.txt and activity and column headers
store them in a third file career.txt (l) To display day, month, and year in the
(e) To remove the empty subdirectories, students format 17 Nov 2012
and teachers, from the college directory (m) To log out from the Unix system
(f) To copy the entire directory teachers along (n) To show all the names of the files and
with its subdirectories in the name faculty directories that begin with any character
(g) To forcibly remove the file mbacourse.txt from a through z followed by exactly three
from the college directory characters
(h) To move the file mbacourse.txt from the (o) To find the square root of number 17 (The
current directory to the professional sub- result should be displayed up to five places
directory of the college directory of decimals.)

Review Questions
3.1 Explain the following commands with their syntax 3.4 What do you mean by escape characters?
and examples. Explain their usage through the echo command.
(a) ls (d) rmdir 3.5 Explain the term globbing with examples.
(b) who (e) cp 3.6 What is the use of the date command? Name the
(c) touch options that are used with the date command to
3.2 Explain the differences between the following: display only the year, hour in military format,
(a) Hard and symbolic links and only the day.
(b) who and finger commands 3.7 Explain the command used to exploit terminal
(c) cat and touch commands capabilities.
(d) rm and rmdir commands 3.8 Explain with examples the command that is used
3.3 What is the use of the bc command? Explain a to display the calendar of the desired month and
few functions that are associated with it. year.

Brain Teasers
3.1 In the long-listing command ls –li, if you find the communication network? If yes, mention the
two or more files having the same inode number, command.
what does it mean? 3.5 Consider the following cat command:
3.2 Identify the error in the following command and $ cat chirag notes.txt
correct it to display all the files that consist of It displays an error indicating that the file notes.
exactly four characters. txt does not exist. How can you avoid this error
$ ls **** message?
3.3 Identify the error in the following command and 3.6 If on using s() function in the bc command
correct it to display the hardware platform of the for finding sine value, a wrong answer was
current machine. obtained, identify the error.
$ uname -v 3.7 You want to change your password but the follow-
3.4 Can you display the node name, that is, the ing command is not working. Where is the error?
name by which your machine is connected in $password
58 Unix and Shell Programming

3.8 Is there any way to copy the content of the files to 0 places of decimal. What change is required
a.txt and b.txt to a file c.txt without deleting to be made in order to get the result up to 20
the earlier content of file c.txt? If yes, what is decimal places ?
that? $ bc
3.9 What should the command given to display the 17/3
hardware platform and name of the operating 3.13 What is the mistake in the following command
system on a machine be? for changing the modification time of the file?
3.10 You wish that a confirmation prompt appears a.txt to Oct 15 04:15?
before deleting the files. However, by using the $ touch –a 10150415 a.txt
following command, the confirmation message 3.14 The following command to recursively
is not prompted. Where and what is the error? copy the content of the directory projects to
$ rm -f a*.* experiments is not working. Identify the error
3.11 The following command creates a hard link of and correct it.
the file a.txt in the name b.txt. What changes $ cp projects experiments
are required to be made to this command in order 3.15 The following date command is not displaying
to create a symbolic link instead of a hard link? century in four digits. Identify the error and
$ ln a.txt b.txt correct it.
3.12 The following bc command displays the result $ date +%y

■ ANSWERS TO OBJECTIVE-TYPE QUESTIONS ■


State True or False 3.13 True 3.4 pwd Multiple-
3.1 True 3.14 True 3.5 MMDDhhmm choice
3.2 False 3.15 False 3.6 -p Questions
3.3 False 3.16 True 3.7 -r 3.1 (a)
3.4 True 3.17 True 3.8 ln 3.2 (b)
3.5 False 3.18 True 3.9 hard, 3.3 (c)
3.6 True 3.19 True symbolic 3.4 (a)
3.7 False 3.20 False 3.10 %x 3.5 (d)
3.8 True 3.11 l() 3.6 (b)
3.9 False Fill in the Blanks 3.12 cal -y 3.7 (d)
3.10 True 3.1 -a 3.13 finger 3.8 (c)
3.11 True 3.2 -m 3.14 -i 3.9 (a)
3.12 False 3.3 relative 3.15 -v 3.10 (c)
Advanced Unix C HA PT E R

Commands

4
After studying this chapter, the reader will be conversant with the following:
• Advanced commands used in the Unix operating system such as setting access permissions for the
existing files and directories, setting default permissions for the newly created files and directories,
creating groups, changing ownerships of the files, and sharing files among groups
• Sorting content and performing input/output (I/O) redirections, that is, diverting the output of a command
to a file or providing input to a command from a file
• Cutting or slicing the file vertically, pasting content, splitting files, counting characters, words, and lines
in files or other content, and using a pipe operator, that is, sending the output of a command as input to
another command
• Displaying the top and bottom contents of a file, presenting content page-wise, and displaying the
manual of any command
• Comparing files, eliminating and displaying duplicate lines in two files, and displaying and suppressing
the unique and common content in two files
• Printing documents, setting reminders of appointments, carrying out conversions between DOS and
Unix files, and measuring time usage in the execution of commands

4.1 OVERVIEW
The advanced Unix commands help us perform several tasks such as setting access permissions
for the existing files and directories, setting default permissions for the newly created files and
directories, changing ownership of the files, and sharing files among groups. These commands
also include sorting file content, performing input/output (I/O) redirections, and piping the
output of a command as input to another command. Unix also offers commands for operations
such as cutting or slicing the file vertically, pasting content, splitting files, counting characters,
words, and lines in files, extracting the top and bottom contents of files, presenting content
page-wise, and displaying manual commands. These commands also include comparing files,
eliminating and displaying duplicate lines in two files, suppressing the unique and common content
in two files, printing documents, setting reminders of appointments, carrying out conversions
between DOS and Unix files, and measuring the time usage in the execution of commands.
60 Unix and Shell Programming

The list of advanced commands that will be covered in this chapter is as follows:
chmod, umask, chown, chgrp, groups, input/output redirection in Unix, pipe operator, cut,
paste, split, wc, sort, head, tail, diff, cmp, uniq, comm, time, pg, lp, .profile, calendar,
script, dos2unix, and man.

4.2 FILE ACCESS PERMISSIONS


The data in the Unix system is contained in files. We may restrict or permit access to this
data by restricting or permitting access to the files containing the data. There are three types
of Unix files: ordinary, directory, and special files.
We may use file permissions to avoid any accidental modifications. We can retain the
ability to read the file while restricting the ability to write in them. Similarly, we can also
restrict other users in a multi-user environment from reading our files.
There are three classes of system users.The first is the user. The user is usually the system
user who created the file. The user has full control over restricting or permitting access to the
file at any time. In addition to individual file ownership, it is possible for one or more system
users to own the file collectively in a kind of group ownership. A system user who is not the
file owner may access the file if this user belongs to the group of system users who are allowed
to access the file. The last category of system users is the one who is neither the owner nor part
of the group and is known as the other user. Hence, there are three classes of system users:
1. User refers to the system user who created the file and is also sometimes called owner.
2. Group refers to one or more users who may access the file as a group.
3. Other refers to any other users of the system.
There are several permissions for system usage. System users with a read permission
may read the contents of an ordinary file while users with a write permission may write in a
file and change its contents. Write permission is also required to delete the file using the rm
command (Table 4.1).
Table 4.1 Access modes and permissions

Access mode Ordinary file Directory file


Read Allows examination of file contents Allows listing of files within the directory
Write Allows changing of contents of the file Allows creation of new files and removal of
old ones
Execute Allows execution of the file as a command Allows searching of the directory

We can view the permissions of a file or directory through the long listing command. The
following example shows the long listing of file mce1.

Example $ ls –l mce1
This statement requests the long directory listing for the ordinary file called mce1. We might
get the output shown in Fig. 4.1.
The dash (-) in the file type field indicates that it is an ordinary file. The access permissions
field tells us what kinds of access permissions are granted. The number, 1, indicates that
there is only one link for this file from the directory, which means that this file only has one
Advanced Unix Commands 61

– -rw-rw-rw- 1 chirag it 120 Mar 15 12:20 mce1

File type Permissions Links Owner Group Size Date and time filename
of last modification
Fig. 4.1 Output of the long listing command for the file mce1

name associated with it. The word chirag is the owner’s name; it is the group name that has
access to this file; 120 refers to the file size; Mar 15 12:20 is the date and time the file was last
modified; and mce1 is the filename.
We have seen that long listing shows the permissions for all the three system users—User,
Group, and Other—besides other information such as name of the file (or directory), size,
date, and time of last access. Assume that the permissions for the file mce1 are as follows:
r w x r - x r - - 1 chirag it 120 Mar 15 12:20 mce1
The first three characters, r, w, and x, are the permissions for the User. This is followed by the
permissions for the Group members. The last three characters represent the permissions for the
Other member. The aforementioned output indicates that the User has all the three permissions,
r w x (read, write, and execute), for the file mce1. The permissions r – x indicate that the
Group members have read and execute permissions for the file mce1. The missing permission is
represented by a hyphen (-). The Other users have only r, that is, read permission for the file mce1.
Suppose the permissions for the file mce1 are as follows:
r - x - - x - - - 1 chirag it 120 Mar 15 12:20 mce1

The permissions indicate that the User has r - x, that is, read and execute permissions for
the file mce1. The Group members have - - x, that is, only execute permission for the file,
and the Other members have no permission (- - -), that is, the Other members cannot read,
write, or execute the file mce1.
Let us take a look at how we can assign and remove permissions from a file or directory.

4.2.1 chmod: Changing File Access Permissions


chmod stands for change mode and the command is used for changing the access permission for
files and directories. Only the owner or super user can change the access permission for files.
Syntax chmod [option] mode files

Here, option refers to the following elements given in Table 4.2.


The keyword mode refers to the three access permissions given in Table 4.3.
Examples
(a) $chmod 751 a.txt
This command assigns permission 7 to user (i.e., owner), 5 to group, and 1 to other.
Permission 7 means 4(r) + 2(w) + 1(x), that is, the user has all the three permissions,
read, write, and execute for the file a.txt. Similarly, the group members have 4(r) + 1(x),
that is, read and execute, but no write permission for the file a.txt. However, the other
users can only execute th e file. Refer to Fig. 4.2 to view the output of the command.
(b) $chmod 760 a.txt
62 Unix and Shell Programming

Table 4.2 Brief description of options used with the chmod Table 4.3 Brief description of modes used with
command the chmod command
Option Description Mode Description
u Represents User or the owner of the file r or 4 Represents read permission
g Represents Group w or 2 Represents write permission
O Represents Other x or 1 Represents execute permission
A Represents all (User, Group, and Other). It is the
default option
This command assigns permission 7,
+ Adds access permission
4(r) + 2(w) + 1(x), that is, read, write, and
- Removes access permission
execute permissions for the file a.txt to
= Assigns permission to u, g, o, or a the user (or owner) of the file. Permission
6, 4(r) + 2(w), that is, read and write
permission is assigned to the group members of the file, and 0 or no permission to
other users. The other users cannot read, write, or execute the file a.txt. Refer to
Fig. 4.2 to view the output of the command.
(c) $chmod o+r a.txt
This command adds the read permission to the other members for the file a.txt. Other
existing permissions are left undisturbed. Refer to Fig. 4.2 to view the output of the
command.
(d) $chmod u-x,g-w+x,o+wx a.txt
It removes the execute permission of the user (i.e., owner), removes the write permis-
sion of the group members, adds execute permission to the group members, and adds
write and execute permissions to the other users. The existing permissions are left
undisturbed. Refer to Fig. 4.2 to view the output of the command.
Note: There should not be any space after the comma (,) or while specifying permissions of the user, group,
and others in the command.

(e) $ chmod u=rwx,g=rx, o=x a.txt


This command assigns permission, rwx, that is, read, write, and execute permission
to u, that is, the user or owner of the file. It also assigns, rx, that is, read and execute
permission to the group members of the file and x, that is, execute permission to the
other users. Previous permissions assigned to user, group, and other members will be
removed. Refer to Fig. 4.2 to view the output of the command.
(f) $ chmod u=w a.txt
This command assigns write permission to the user, that is, owner of the file a.txt.
Previous permissions assigned to the user will be removed. Existing permissions to
the group and other users will be undisturbed. Refer to Fig. 4.2 to view the output
of the command.

4.2.2 umask: Setting Default Permissions


The umask command sets the default permissions for the files that will be created in the
future.
Advanced Unix Commands 63

$ chmod 751 a.txt Syntax umask ugo


$ ls -al a.txt Here, u, g, and o refer to the permissions that
-rwxr-x--x 1 bintu None 21 Dec 30 19:10 a.txt
we do not want the user, group, and others
$ chmod 760 a.txt
to have for the new files. Yes, you have read
$ ls -al a.txt correctly, umask is not for assigning but for
-rwxrw---- 1 bintu None 21 Dec 30 19:10 a.txt
removing permissions of the three categories
$ chmod o+r a.txt of system users—user (owner), group, and
$ ls -al a.txt other—for the future files.
-rwxrw-r-- 1 bintu None 21 Dec 30 19:10 a.txt To understand this better, let us first create
$ chmod u-x,g-w+x,o+wx a.txt an empty file called chirag using the touch
$ ls -al a.txt command and then try to list it.
-rw-r-xrwx 1 bintu None 21 Dec 30 19:10 a.txt
Example
$ chmod u=rwx,g=rx,o=x a.txt
$ touch chirag
$ ls -al a.txt
-rwxrw-x--1 bintu None 21 Dec 30 19:10 a.txt $ ls -l chirag
-rw-r--r-- 1 mce it 26 Oct 27
$ chmod u=w a.txt
10:12 chirag
$ ls -al a.txt
--w-r-x--x 1 bintu None 21 Dec 30 19:10 a.txt The permission of this file is 644. Whenever
Fig. 4.2 Output of application of the chmod command on we create a file, Unix uses the value stored
file a.txt in a variable called umask to decide the
default permissions. umask stands for user file
creation mask, and is used for defining the
permissions to mask or hide, that is, the permissions that we want to deny. The current value
of umask can be easily determined by typing umask followed by the Enter key.
$ umask
0022

The first 0 indicates that what follows is an octal number. The three digits that follow the first
zero refer to the permissions to be denied to the owner, group, and others. This means that for
the owner no permission is denied, whereas for both the group and others, write permission
(2) is denied.
Whenever a file is created, Unix assumes that the permissions for this file should be 666.
However, since our unmask value is 022, Unix subtracts this value from the default system-
wide permissions (666) resulting in a value 644. This value is then used as the permissions
for the file that we create.
This is the reason why the permissions turned out to be 644 or rw-r--r-- for the file
chirag that we created.
Similarly, the system-wide default permissions for a directory are 777. This implies that
when we create a directory its permission would be 777 − 022, that is, 755.

Note: If a directory does not have an execute permission we will never be able to enter data into it.

To change umask value:


$ umask 342
64 Unix and Shell Programming

This would ensure that from this point onwards, any new file that we create would have the
permissions 324 (666 − 342) and any directory that we create would have the permissions
435 (777 − 342).

4.2.3 chown: Changing File Ownership


The chown command is used for changing the owner and group owner of a file.
Syntax chown [-R] new_owner[:[new_group]] filenames

The options and arguments of this command are briefly explained in Table 4.4.
Table 4.4 Brief description of options used in the chown command

Option Description
-R The command applies recursively to the files and subdirectories of the current directory.
new_owner It is the new owner of the files, that is, new_owner will become the new owner of the files
and hence gets all the permissions to access the file and modify its access permissions.
new_group It is the group name to which we want to assign the files.
filenames These are the files whose ownership we wish to change.

To change both the owner and the group of the file, new_owner must be followed by a
colon and a new_group with no space in between.

Note: If no new_group is specified after the new_owner and colon, the owner and group of the file is changed
to new_owner and group of new_owner, respectively.
If the new_owner is missing but colon and new_group are specified then only the group of the files is changed,
that is, the command will act as the chgrp command. We will learn about the chgrp command next.

Examples By default, when we create or copy a file, we become its owner. For example,
suppose we have a file named notes.txt and we want to change its ownership to another
person named Ravi.
Let us first view the current owner of the file by giving the following command:
(a) $ ls –l notes.txt
-rwxrwxr-x 1 chirag it 120 Mar 15 12:20 notes.txt
We can see that chirag is the current owner of the file. Now chirag can give the following
command to give the ownership of the file notes.txt to Ravi.
(b) $ chown ravi notes.txt
To see whether the ownership is changed, let us again give the ls –l command.
(c) $ ls –l notes.txt
-rwxrwxr-x 1 ravi it 120 Mar 15 12:20
notes.txt

We can see that the owner of the file notes.txt is changed from chirag to ravi.
Now, chirag will no longer be able to change the permissions of the file notes.txt and
only ravi can do so.
Advanced Unix Commands 65

Note: This process is one way because we must either be the owner of the file or the super user to
change its ownership. After we give the file to ravi, we cannot get its ownership back until and unless
ravi issues the chown command to return the ownership to us.

Examples The following are a few more examples.


(a) $ chown chirag:mba notes.txt
This command will change the owner and group of the file notes.txt to chirag and mba,
respectively.
Note: The user chirag, and group mba must exist before giving the command. The commands to add a new
user and group to the system are useradd and groupadd, respectively (discussed in Chapter 15).

(b) $ chown chirag: notes.txt


This command will change the owner of the file notes.txt to chirag and the group of
the file is changed to the one to which chirag belongs.
(c) $ chown :mba notes.txt
This command will change the group of the file notes.txt to mba.
(d) $ chown -R chirag projects
This command will change the ownership of the files and subdirectories of the directory
projects to chirag.

4.2.4 chgrp: Changing Group Command


The chgrp command is used for changing the group of the specified number of files. The files
will thereby be made accessible to the specified group.
Syntax chgrp [-R] [-h] new_group filenames

The options and arguments of this command are briefly explained in Table 4.5.
Table 4.5 Brief description of the options used in the chgrp command

Option Description
-R It recursively changes the group of the files and subdirectories of the specified directory.
-h If the specified file is a symbolic link, its group is changed. In the absence of a -h option,
the group of the file referenced by the symbolic link is changed and not the symbolic link.
new_group It is the group name we want to assign the files to.
filenames Specifies the files whose group we want to change.

By default the file we create gets group ownership in the group we belong to, that is, the
group to which the owner belongs becomes the default group ownership of the file.
For example, if we belong to the group it, our file will also have the same group ownership,
as can be seen by the following command:
$ ls –l notes.txt
-rwxrwxr-x 1 chirag it 120 Mar 15 12:20 notes.txt

The following command is used to change the group ownership of a file named notes.txt
from group it to group hospital:
66 Unix and Shell Programming

$ chgrp hospital notes.txt

Note: The group hospital must exist before giving this command.

Now, the group ownership may appear as follows:


$ ls –l notes.txt
-rwxrwxr-x 1 chirag hospital 120 Mar 15 12:20 notes.txt

Note: Since we are still the owner of the file, we can again change its group ownership any time.

Examples
(a) $chgrp -R hospital projects
This command changes the group of all the files and subdirectories present in the projects
directory to hospital.
(b) $chgrp -h hospital finance.txt
This changes the group of the symbolic file finance.txt to hospital.

4.2.5 groups: Displaying Group Membership


The groups command is used for finding the group to which a user belongs.
Syntax groups username1 [ username2 [username3 …]]

Example
(a) % groups chirag
mba
This example asks the group name of the user, chirag. The output mba signifies that the
user chirag belongs to the group named mba.
We can also find the group membership of more than one user simultaneously as follows:
(b) % groups chirag ravi
chirag : mba
ravi : other
This command asks the group names of the two users, chirag and ravi. The output indicates
that the user chirag belongs to the mba group and the user ravi belongs to the other group.

4.2.6 groups: Sharing Files Among Groups


Files can be shared with a group of users so that they can simultaneously read, work, and
operate the files(s). For this to happen, a group needs to be created by the system administrator.
To create a group, we give the command with the following syntax:
Syntax groupadd group_name

Example % groupadd bankproject

This will create a group by the name bankproject. After creating a group, the next step is to
set the group ownership of the file(s) to the given group using the chgrp command.
Syntax $ chgrp groupname filename
Advanced Unix Commands 67

Example
(a) $ chgrp bankproject accounts.txt
This will set the group owner of the file accounts.txt to our newly created group bankproject.
Similarly, we need to change the group ownership of all the files that we wish to share with
the users of our group bankproject. Thereafter, we need to set the file permissions so that
everybody in the group can read and write the file through the following syntax:
Syntax $ chmod g+rw filename

We can also assign access permissions to the group in the following way:
Syntax $ chmod 770 filename
This example assigns read, write, and execute permissions to the owner and group members
of the file and no permission to the other users.

4.3 INPUT/OUTPUT REDIRECTION IN UNIX


The input to a command or a shell is usually provided through the standard input, that is, the
keyboard, while the output of the command is displayed on the standard output, that is, on
the terminal screen. By default, each command takes its input from the standard input and
sends the results to the standard output.
By making use of the I/O redirection operators, we can change the location of providing
input to a command and displaying the output of the command. Let us first understand the
output redirection operator.

4.3.1 Output Redirection Operator


The output redirection operator is used to redirect the output (from a command) that is
supposed to go to a terminal by default, to a file instead. This process of diverting the output
from its default destination is known as output redirection. For redirecting the output, the
operator that we have to use is the ‘>’ operator in shell command. The ‘>’ symbol is known
as the output redirection operator and we can use it to divert the output of any command to
a file instead of the terminal screen.
Syntax command [> | >>] output_file
Here, output_file is the name of the file where we wish to direct and save the output of the
command.
Example $ ls > kk
On using this command, nothing will appear on the output screen and all output, that is, the
list of files and directories from the ls command is redirected to the file kk.
On viewing the contents of the file kk, we get the list of files and directories in it.
Note: If the file kk does not exist, the redirection operator will first create it, and if it already exists, its contents
will be overwritten.
In order to append output to the file, the append operator, >> is used as shown in the following
command:
$ ls >> kk
68 Unix and Shell Programming

4.3.2 Input Redirection Operator


In order to redirect the standard input, we use the input redirection operator, the < (less than)
symbol.
Syntax command < input_file

Here, input_file is the name of the file from where the data will be supplied to the command
for the purpose of computation.
Examples
(a) $ sort < kk
The sort command in the example receives the input stream of bytes from the file kk.
We can also combine input and output redirection operators.
(b) $ sort < kk > mm
On using the command, nothing will appear on the terminal screen; instead the content
of the file kk will be sorted and sent directly to the file mm.

4.4 PIPE OPERATOR


The pipe operator, represented by the symbol ‘|’, is used on the command line for the purpose
of sending the output of a command as an input to another command. The pipe operator is
different from the output redirection operator, ‘>’ in a way that the output indirection operator
‘>’ is mostly used for sending the output of a command to a file, whereas the pipe operator
is used for sending output of a command to some other command for further processing.
Syntax command1 | command2 [| command3…]

Example $ cat notes.txt | wc


4 20 75

Here, the output of the cat command is sent as input to another command, wc. The wc command
counts the lines, words, and characters in the file notes.txt whose content is passed to it.
We can combine several commands with pipes on a single command line as follows:
$ cat notes.txt | sort| lp
This command sorts the content of the file notes.txt and sends the sorted content to the
printer for printing.
Note: The pipe operator provides a one-way flow of data that is from left to right, whereas the redirection
operator enables two-way flow of data.

4.5 cut: CUTTING DATA FROM FILES


The cut command is used for slicing (cutting) a file vertically.
Syntax cut [-c –f] file_name

Here, –c refers to columns or characters and –f refers to the fields, that is, words delimited
by whitespace or tab.
Advanced Unix Commands 69

Examples
(a) cut -c 6-22,30-35 bank.lst
This command retrieves 6-22 characters and 30-35 columns (characters) from the file
bank.lst and displays them on the screen.
Let us look at another example.
(b) $ cut -f2 bank.lst
We get the content of the second field of the file bank.lst displayed on the screen.
Let us assume the file bank.lst has the following content.
101 Anil
102 Ravi
103 Sunil
104 Chirag
105 Raju
Note: The fields in the file bank.lst are separated by a tab space.

Here, the cut command will display the second field of the file bank.lst, that is, we will get
the output shown in Fig. 4.3.
The fields in the file bank.lst are delimited by a tab. If they are
$ cut -f2 bank.lst
separated by a delimiter other than tab or white space, then the
Anil output of the cut command will be different.
Ravi
Sunil Let us assume the file bank.lst has the following content.
Chirag
Raju 101,Anil
102,Ravi
Fig. 4.3 Output
103,Sunil
displaying second field of
104,Chirag
the file bank.lst
105,Raju

We can see that the fields of the file bank.lst are delimited by a comma (,) and not by a tab or
white space. The following command will not display anything on the screen as the default
delimiter for identifying fields is either white space or tab.
$ cut -f2 bank.lst
Hence, the file bank.lst will be considered to be consisting of a single field on each
line.
To specify the delimiter when the fields are delimited by some other character other than
tab or white space as in the aforementioned file, we use -d (delimiter) to specify the field
delimiter as shown in the following example:
cut -f2 -d "," bank.lst
This statement will show the second field of the file bank.lst where the fields are delimited
by commas (,).
Assume that the fields are delimited by a comma (,). The following statement cuts the
fields, starting from the first, from the file bank.lst:
$ cut -d"," -f1- bank.lst
70 Unix and Shell Programming

Assuming that the fields are delimited by commas (,), the following statement cuts the first
field, fourth field, and so on, from the file bank.lst:
$ cut -d"|" -f1,4- bank.lst

Can we cut the fields of two separate files and paste them to make a third file? Yes, of course.
Let us see how.
Assume there are two files, Names and Telephone, with the following contents.
The Names file consists of employee codes and names as follows:
101 Anil
102 Ravi
103 Sunil
104 Chirag
105 Raju

The Telephone file consists of employee codes and telephone numbers as follows:
101 2429193
102 3334444
103 7777888
104 9990000
105 5555111

Let us cut the second field from both the files and paste them to make a third file, that is, cut
the employee names from the Names file and telephone numbers from the Telephone file and
paste them to create a third file.
To cut out the second word (field) from the file Names, we give the following
command:
$ cut -f2 Names

We get the output as shown in Fig. 4.4.


Similarly, to cut the telephone numbers, that is, the second field from
Anil
Ravi the Telephone file, we give the following command:
Sunil
Chirag $ cut -f2 Telephone
Raju
2429193
Fig. 4.4 Output 3334444
showing second 7777888
field of file Names 9990000
5555111

We can save the output by redirecting the standard output to a file.


$ cut -f2 Name > names.txt
$ cut -f2 Telephone > numbers.txt

The names and telephone numbers will be saved in two files, names.txt and numbers.txt,
respectively. To paste the content of the two files, we need to understand the paste command.
Let us now study this command.
Advanced Unix Commands 71

4.6 paste: PASTING DATA IN FILES


It is used to join textual data together and is very useful if we want to put together textual
information located in various files.
Syntax paste [-s] [-d "delimiter"] files

These options are briefly explained in Table 4.6.

Table 4.6 Brief description of the options used in the paste command

Option Description
-s The paste command usually displays the corresponding lines of each specified file.
The -s option refers to a serial option and is used to combine all the lines of each file
into one line and display them one below the other.
-d This option is for specifying the delimiter to be used for pasting lines from the specified
files. The default delimiter used to separate the lines from the files is the Tab character.

Anil 2429193 Example Consider the file names.txt mentioned in


Ravi 3334444 Section 4.5.
Sunil 7777888
Chirag 9990000
Raju 5555111 $ paste names.txt numbers.txt

Fig. 4.5 Pasting of two files names. The output will be as shown in Fig. 4.5.
txt and numbers.txt with the We can see that the corresponding lines of the files
default tab character in between names.txt and numbers.txt are pasted with a tab character
in between. By default, the paste command uses the tab
Anil:2429193 character for pasting lines; however, we can specify a
Ravi:3334444
Sunil:7777888 delimiter of our choice with the -d command as shown in
Chirag:9990000
Raju:5555111 the following example.
$ paste -d"|" names.txt numbers.txt
Fig. 4.6 Two files names.txt
and numbers.txt pasted with This joins the two files with the help of the | delimiter
the ‘|’ symbol in between and not tab (i.e., between names and telephone numbers,
there will be a ‘|’ symbol instead of the tab
$ paste -s names.txt numbers.txt character, as shown in Fig. 4.6).
Anil Ravi Sunil Chirag Raju The example shown in Fig. 4.7 serially
2429193 3334444 7777888 9990000 5555111
pastes the contents from the files. It combines
Fig. 4.7 Two files names.txt and numbers.txt all the lines of each file into one line and
pasted one below the other displays them one below the other.

4.7 split: SPLITTING FILES INTO LINES OR BYTES


The split command is used to split a file into pieces.
Syntax split [-b n [K | M]] [ -l n] [-n] file_name dest_file

The options and arguments are briefly explained in Table 4.7.


72 Unix and Shell Programming

Table 4.7 Brief description of the options and arguments used in the split command

Option Description
-b n It splits the specified file into pieces that are n bytes in size.
-b nK It splits the specified file into pieces that are n kilo bytes in size.
-b nM It splits the specified file into pieces that are n mega bytes in size.
-l n It splits the specified file into n number of lines (default option). The default value of n is 1000.
-n It is the same as -l n.
File_name It is the name of the file to be split.
dest_file It is the name of the file in which the split pieces will be stored. If the dest_file is, say, demo, the
split pieces will be stored in the files demoaa, demoab, demoac, and so on.

$ split numbers.txt trial $ split -b 20 numbers.txt temp


$ ls trial* $ ls temp*
trialaa tempaa tempab
$ cat trialaa $ cat tempaa
2429193
2429193 3334444
3334444 7777
7777888
9990000 $ cat tempab
5555111 888
9990000
Fig. 4.8 File numbers.txt split into the 5555111
file trialaa Fig. 4.9 File numbers.txt split into two 20-byte
files, tempaa and tempab
$ split -1 2 numbers.txt temp
Example When the split command is
$ ls de*
tempaa demoab demoac given without any option, the file is split
into pieces that are 1000 lines each, that is,
$ cat demoaa
the default option is –l as shown in Fig. 4.8.
2429193
3334444 We can see that the file numbers.txt is
$ cat tempab
split into a single file trialaa consisting of
777888 the complete content of the file numbers.txt
9990000
(because the size of the file numbers.txt is
$ cat demoac lesser than 1000 lines).
5555111
The example in Fig. 4.9 splits the file
Fig. 4.10 File numbers.txt split into three files, numbers.txt into files that are 20 bytes each.
demoaa, demoab, and demoac, which are two The file numbers.txt is split into two
lines each pieces, tempaa and tempab, where tempaa
contains the first 20 bytes of the file numbers.txt while the file tempab contains the remaining
number of bytes.
The example in Fig. 4.10 splits the file numbers.txt into pieces, each of which consists
of two lines.
The file numbers.txt is split into three files with the following names: demoaa, demoab, and
demoac. Each of these files contains two lines (as shown in Fig. 4.10).
Advanced Unix Commands 73

4.8 wc: COUNTING CHARACTERS, WORDS, AND LINES IN FILES


The wc (word count) command is usually used to find the number of lines in any file. By
default, it displays all the three counts—characters, words, and lines—of any given file.
Syntax wc [-l -w -c] [filename]

Here, -l counts the number of lines.


-w counts the number of words delimited by white space or a tab.
-c counts the number of characters.
Example $ wc phone.lst
12 124 650 phone.lst
This command displays the count of lines, words, and characters in the file phone.lst, that
is, the numerical values 12, 124, and 650 represent the count of lines, words, and characters,
respectively in the file phone.lst.
If we wish to view only the number of lines in the file, we need to only use the –l option,
as in the following example:
$ wc -l phone.lst
12 phone.lst
As we can see, the command displays the count of the number of lines in the file phone.lst.
Similarly, the -w option will give the total number of words in a file, and the -c option gives
the total number of characters in a file.

4.9 sort: SORTING FILES


It is used for sorting files either line-wise or on the basis of certain fields, where the fields refer
to the words that are separated by one of the following: white space, tab, or special symbol.
Syntax sort [-n][-r][-f][-u] filename

The options and arguments shown here are briefly explained in Table 4.8.
Table 4.8 Brief description of the options used in the All lines in the filename will be
sort command arranged in alphabetical order on the
basis of the first character of the line.
Option Description The other syntax for using the sort
-n Sorts numerical values instead of ASCII, command is as follows:
ignoring blanks and tabs
Syntax sort +p1 - p2 filename
-r Sorts in reverse order
-f Sorts upper and lower case together, that This limits the sort to be applied on the
is, ignores the difference in upper and basis of the characters beginning from
lower case field p1 and ending at field p2. If p2 is
omitted, then sorting will be done on
-u Displays unique lines, that is, it eliminates
duplicate lines in the output
the basis of the characters beginning
from field p1 till the end of the line.
filename Represents the file to be sorted
Examples
(a) $ sort +2 -4 bnk.lst
74 Unix and Shell Programming

This command skips the first two fields and uses the third and fourth fields for sorting
the file bnk.lst.
(b) $ sort +3 -4 bnk.lst
This command skips the first three fields and uses the fourth field for sorting the file
bnk.lst.
(c) $ sort +2 bnk.lst
This command skips the first two fields and uses the third and the rest of the fields up till
the end of the line for sorting the file bnk.lst.
(d) $ sort bnk.lst -o bank.lst
This command sorts the file bnk.lst and stores the result in bank.lst.
(e) $ sort +0 -1 bnk.lst
This command sorts the file bnk.lst on the basis of the first field.
(f) $ sort +1 -4 bnk.lst
This command sorts the file bnk.lst from the second to the fourth fields.
(g) $ sort +2b bnk.lst
This command sorts the file bnk.lst on the third field after ignoring leading blank spaces.
The -f option is used to ignore the upper and lower case distinction.
(h) $ sort +2bf bnk.lst
The command will sort the third field after ignoring leading blank spaces and sort the
upper and lower case data together.
The -n option is used for sorting the file on the basis of numerical values rather than
ASCII values.
(i) $ sort -n +2 -3 a.bat
The command sorts the file a.bat on the third field, on the assumption that it is a
numerical field.
The -r option is used for sorting a given file in reverse order.
(j) $ sort -r link.lst
The command will sort the file link.lst in the reverse order. The -u option will eliminate
duplicate lines in the sorted output.
(k) $ sort -nu +2 -3 a.bat
The command sorts the file a.bat on the third field after eliminating duplicate lines.

4.10 head: DISPLAYING TOP CONTENTS OF FILES


The head command is used for selecting the specified number of lines from the beginning of
the file and displaying them on the screen.
Syntax head –[n] file name

Here, n is the number of lines that we want to select.


Example head bnk.lst

When used without an option, this displays the first ten records (lines) of the specified file.
head -3 bnk.lst

It displays the first three lines of the file bnk.lst.


Advanced Unix Commands 75

We can also specify more than one file.


head -3 bnk.lst notes.txt

It will display the first three lines of both the files, bnk.lst and notes.txt, one after the other.

4.11 tail: DISPLAYING BOTTOM CONTENTS OF FILES


The tail command is used for selecting the specified number of lines from the end of the file
and displaying them on the screen.
Syntax tail –[ncbr] filename

The options of the command are briefly explained in Table 4.9.


Table 4.9 Brief description of the options used in Note: There is one more option that is used with
the tail command the tail command. +n gives an instruction to skip
Option Description n–1 lines and select the rest until the end of the file.

-n Selects the last n lines Examples


-c Selects the last c number of characters (a) $ tail -3 bnk.lst
-b Selects a specified number of disk blocks It will display the last three lines.
-r Sorts the selected lines in reverse order (b) $ tail -10 bnk.lst
It will display the last 10 lines.
(c) $ tail +10 bnk.lst
It will start extracting from the tenth line (it will skip nine lines) up to the end of the file.
(d) $ tail –50c bnk.lst
It will display the last 50 characters of the file bnk.lst.
(e) $ tail –2b bnk.lst
It will display the last two disk blocks of the file bnk.lst. A disk block is usually 512
bytes big.
(f) $ tail –2r bnk.lst
It will display the last two lines of the file bnk.lst in reverse order, that is, the last line
will appear first followed by the second last line.
(g) $ head -25 a.txt | tail +20 > b.txt
It extracts lines numbering from 20 to 25 from the file a.txt.
The head utility extracts the first 25 lines from the file a.txt and pipes them to the tail
utility, which skips the first 19 lines and extracts lines 20 to 25. The results are then
stored in file b.txt.

4.12 diff: FINDING DIFFERENCES BETWEEN TWO FILES


The diff command is used for comparing two files. If there are no differences between the
two files being compared, the command does not display any output. Otherwise it displays
the output indicating the changes that need to be made to the first file to make it same as the
second file.
Syntax diff file1 file2
76 Unix and Shell Programming

All the differences found in the two files are displayed in a format consisting of two numbers
and a character in between. The number to the left of the character represents the line number
in the first file, and the number to the right of the character represents the line number in the
second file. The character can be any of the following:
1. d: delete
2. c: change
3. a: add
Example Assume we have two files, users.txt and customers.txt, with the following
content.
users.txt customers.txt
John John
Peter Charles
Troy Troy

Now on comparing the two files, we get the following output.


$ diff users.txt customers.txt
2c2
< Peter
--
> Charles

Note: The < character precedes the lines from the first file and > precedes the lines from the second file.

This output indicates that the two files differ by only one line. It indicates that if the second
line, Peter, in the first file (users.txt) is changed to the second line, Charles, of the second
file (customers.txt), both files will be exactly the same.
To better understand the diff command, let us twist the content of the first file users.txt
as follows:
users.txt
John
Peter
Charles

Keeping the content of the file customers.txt same as before, when we compare the two
files, we get the following output.
$ diff users.txt customers.txt
2d1
< Peter
3a3
> Troy

The output indicates that to make the file users.txt the same as customers.txt, we have to
delete the second line, Peter, and add the third line, Troy, from customers.txt after the third
line in users.txt.
Advanced Unix Commands 77

4.13 cmp: COMPARING FILES


The cmp command compares two files and indicates the line number where the first difference
in the files occurs. The cmp command does not display anything if the files being compared
are exactly the same.
Syntax cmp [[-l][-s]] file1 file2 [skip1] [skip2]

The related options and arguments are briefly explained in Table 4.10.
Table 4.10 Brief description of the options and arguments used in the cmp command

Option Description
-l It prints the byte number and the differing byte values in octal for each difference.
-s It displays nothing but the return exit status on the screen. The status returned can be
any of the following:
0: If the two files are identical
1: If the two files are different
>1: If an error occurs while reading the files
file1 and file2 These are the files to be compared.
skip1 and skip2 These are the optional byte offsets from the beginning of file1 and file2 respectively,
where we wish to begin the comparison of files. The offset can be specified in
decimal, octal, and hexadecimal. The offsets in hexadecimal and octal formats have
to be preceded by ‘0x’ and ‘0’, respectively.

Example Consider we have two files, users.txt and customers.txt, with the following
content.
users.txt customers.txt
John John
Peter Charles
Troy Troy

The following are examples of commands that are used to compare the two files.

$cmp users.txt customers.txt

The cmp command compares the files users.txt and customers.txt and displays the
following output.
users.txt customers.txt differ: byte 6, line 2

The output indicates that the byte location where the first difference between the two files
(users.txt and customers.txt) occurs is 6.
The following example shows the list of byte locations and the differing byte values in
octal format for every difference found in the two files:
$cmp -l users.txt customers.txt
78 Unix and Shell Programming

$ cmp -1 users.txt customers.txt We get the output shown in Fig. 4.11.


6 120 103 The output displays the byte locations of the
7 145 150 difference between the two files and also shows that
8 164 141
9 145 162 the users.txt file is smaller than the customers.txt
10 162 154
11 12 145 file as it encounters its end of file (EOF) marker while
12 124 163 being compared with the content of customers.txt.
13 162 12
14 157 124 The following example shows the status returned on
15 171 162 comparing the two files.
16 12 157
cmp: EOF on users.txt $ cmp -s users.txt customers.txt
Fig. 4.11 List of byte locations and the
This example returns the exit status. On displaying
differing bytes in the two files users.txt
the exit status value (Fig. 4.12), we get a value one,
and customers.txt
confirming that the two files are not the same but
different.
$ cmp -s users.txt customers.txt
$ echo $? The following example compares the two files after
1 skipping the offset of 12 and 14 bytes from the two
$ cmp -s users.txt customers.txt 12 14
$ echo $? files respectively.
0
$ cmp -s users.txt customers.txt 12 14
Fig. 4.12 Comparison between files and display
of status On displaying the return status (Fig. 4.12), we get an
output 0, which confirms that the two files are exactly
the same after giving the offset values.

4.14 uniq: ELIMINATING AND DISPLAYING DUPLICATE LINES


The uniq command is used to find and display duplicate lines in a file. In addition, we can
use it to eliminate duplicate lines and display only unique lines.
Syntax uniq [-c | -d | -u ] [ -f fields ] [ -s char ] [input_file [ output_file ] ]

The related options and arguments are briefly explained in Table 4.11.

Table 4.11 Brief description of the options and arguments used in the uniq command

Option Description
-c It precedes each line with a count of the number of occurrences.
-d It displays only repeated lines (duplicate) in the input.
-u It displays only unique lines in the input.
-f fields It ignores the first given number of fields on each input line.
-s char It ignores the first given number of characters of each input line. If this option is used along
with the -f option, the first given number of characters after the first fields will be ignored.
input_file It is the name of the file whose content we need to compare.
output_file It is the name of the file where the output of the command will be stored. If no output file is
specified, the output will appear on the standard output.
Advanced Unix Commands 79

Example $ uniq a.txt > b.txt

This command removes duplicate lines in the file a.txt and saves it in another file b.txt.
Let us assume the file a.txt contains the following content:
a.txt
It may rain today
I am leaving now
It may rain today
Lovely weather
I am leaving now

The following is the command for removing all duplicate lines from a file.
$ sort a.txt | uniq
This command sorts and removes all the duplicate lines in the file a.txt and displays only
the unique lines on the screen. We get the following output.
I am leaving now
It may rain today
Lovely weather

The following command is used to display only the unique lines.


$ sort a.txt | uniq -u
The command sorts and displays only the unique lines in the file a.txt on the screen. We get
the following output.
Lovely weather

The following command is used to display all the duplicate lines in a file.
$ sort a.txt | uniq -d

We get the following output.


I am leaving now
It may rain today

The following command is used to display the count of duplicate occurrences in a file.
$ sort a.txt | uniq -c

We get the following output.


2 I am leaving now
2 It may rain today
1 Lovely weather

4.15 comm: DISPLAYING AND SUPPRESSING UNIQUE OR COMMON


CONTENT IN TWO FILES
The comm command displays or suppresses the content common to two files. The output is
displayed in a column format, where the first column represents the output related to the first
file and the second column displays the output related to the second file.
80 Unix and Shell Programming

Syntax comm [-1] [-2] [-3 ] file1 file2

The options and arguments are briefly explained in Table 4.12.


Table 4.12 Brief description of the options and arguments used in the comm command

Option Description
-1 It suppresses the display of the content that is unique to file1. It also displays the unique
content in file2.
-2 It suppresses the display of the content that is unique to file2. It also displays the unique
content in file1.
-3 It suppresses the display of the content that is common to both file1 and file2, that is, it
displays the unique content in file1 and file2.
file1 and file2 These are the two files being compared.

Note: When the comm command is executed without any options, the output will comprise three columns,
where the first column displays content unique to the first file, the second column displays content unique to
the second file, and the third column displays content common to both the files.

Examples Suppose we have two files, users.txt and customers.txt, with the following
content.
users.txt customers.txt
John John
Peter Charles
Troy Troy

(a) This example compares the two files (users.


$ comm users.txt customers.txt
John txt and customers.txt) and displays the
Charles output in three columns. The first column
Peter
Troy displays content unique to the first file, the
$ comm -1 users.txt customer.txt
John second column displays content unique to
Charles the second file, and the third column displays
Troy
$ comm -2 users.txt customers.txt content common to both the files (Fig. 4.13).
John
Peter $comm users.txt customers.txt
Troy
$ comm -3 users.txt customers.txt (b) This example compares the two files, users.
Charles txt and customers.txt, suppresses the
Peter
content that is unique in users.txt, and also
Fig. 4.13 Output of the comm command on displays the unique content in customers.txt
comparing two files (Fig. 4.13).
$comm -1 users.txt customers.txt
(c) This example compares the two files, users.txt and customers.txt and suppresses the
content that is unique in customers.txt and also displays the unique content in users.
txt (refer to Fig. 4.13).

$comm -2 users.txt customers.txt


Advanced Unix Commands 81

(d) This example compares the aforementioned two files and suppresses the content that is
common in customers.txt and users.txt (Fig. 4.13).
$comm -3 users.txt customers.txt

4.16 time: FINDING CONSUMED TIME


The time command executes a specified command and also displays the time consumed in its
execution. The time usage of the specified command is displayed on the screen.
Syntax time command [arguments]

In the aforementioned syntax, command is the command whose time usage is to be


determined.
Examples We can determine the time taken to perform the sorting operation by preceding
the sort command with the time command.
(a) $ time sort -o newlist invoice.lst
real 0m1.18s
user 0m0.73s
sys 0m0.38s

The real time refers to the time elapsed from the invocation of the command till its
termination. The user time shows the time spent by the command in executing itself
while sys indicates the time used by the Unix system in invoking the command.
(b) Let us see how much time it takes to store the recursive long listing of files and directories
sorted on modification time in a file.
$ time ls -ltR >k.out
real 0m0.04s
user 0m0.01s
sys 0m0.01s

Real time The real time represents the time taken by the command (from its initiation to
termination) to execute.
User time The user time represents the time taken by the command to execute its own
code, that is, the code run in user mode. It represents the actual CPU time used in executing
the command. For small programs that take milliseconds to execute, this time is often
reported as 0.0.
Sys time The sys time is the amount of CPU time spent in the kernel for running the
command. It represents the CPU time spent in executing the system calls that are invoked by
the command within the kernel.
The time command can be used to isolate the commands that are time consuming so that
they can be run in the background. We will learn the process of executing the commands in
the background in Chapter 6.

Note: The combination of user and sys time is known as CPU time.
82 Unix and Shell Programming

4.17 pg: SHOWING CONTENT PAGE-WISE


This command displays the specified long file page-wise, that is, one screen page at a time.
It also enables us to navigate to the previous or following screen. In addition, we can search
for the desired pattern in the given file. On giving this command, it shows the first screen
page of the given file and shows the colon (:) at the bottom of the screen where we can use
the following character(s) to view the desired content in the given file.
Syntax pg [-number] [+ linenumber] [+/pattern/] [filename]

Here, -number specifies the screen size in lines. The default screen size is 23 lines. +line_
number shows the file from the given line number. +/pattern/ shows the file where the given
pattern begins. filename specifies the filename that we wish to view page-wise along with its
path.
Table 4.13 Brief description of the list of commands The list of commands that can be
given on execution of the pg command given on execution of the pg command
are briefly explained in Table 4.13.
Command Description
h Displays help information
Examples
q or Q Quits the pg command
(a) $ pg letter.txt
<blank> or Moves to the next page
This command displays the file
<newline>
letter.txt one screen page at a
$ Moves to the previous page time.
f Skips the next page (b) $pg letter.txt -10
/pattern Searches forward for the given pattern and This command displays the
displays it content of the file letter.txt
?pattern Searches backward for the given pattern one screen page at a time where
and displays it a page consists of 10 lines.

(c) $pg letter.txt +25


This command displays the content of the file letter.txt page-wise from the 25th
line.
(d) $pg letter.txt +/happy/
This command displays the content of the file letter.txt from the location where the
word happy occurs in the file.

4.18 lp: PRINTING DOCUMENTS


The term lp stands for line printer and the command is used for printing files.

Syntax lp [ -d printer_destination ] [ -n number_of_copies] [ -q priority ]


[ -H ] [ -P page_list ] file(s)

The options used in this command are briefly explained in Table 4.14.
Advanced Unix Commands 83

Table 4.14 Brief description of the options used in the lp command

Option Description
-d It is used for defining the printer destination, that is, the name of the printer we wish to print
the file(s) with.
-n It is used to define the number of copies to print. The valid range is from 1 to 100.
-P It is used to define the pages of a selected file that we wish to print. The page list contains the
page numbers and page range separated by commas (,). Examples: 1, 5, 9–11, 20.
-i It is used to identify the job ID assigned to the print command. On giving the lp command,
it notifies the job ID assigned to the task.
-H It is used to control the printing job. The values used with this option are as follows:
1. Hold: Holds the printing job
2. Resume: Resumes the printing job
3. HH:MM: Holds the job till the specified time
4. Immediate: Prints the job immediately
-q - It is used to set the priority of the print job. The valid values are from 1 (indicates lowest
priority) till 100 (indicates highest priority). The default priority value is 50.

Examples The following are the examples of the lp commmand.

(a) $lp notes.txt


This command prints the file notes.txt on the default printer.
(b) $lp -d Deskjet1001 notes.txt
This command prints the file notes.txt on the printer named Deskjet1001.
(c) $lp -d Deskjet1001 -n 2 notes.txt
This command prints two copies of the file notes.txt on the printer named
Deskjet1001.
(d) $lp -d Deskjet1001 -P 2, 5-7, 10, 15- notes.txt
This command prints pages 2, 5, 6, 7, 10, and from 15 till the end of the file notes.txt
on the printer Deskjet1001.

Note: The hyphen after 15 suggests from page 15 onwards.

(e) $lp -d Deskjet1001 notes.txt accounts.txt


This command prints the file notes.txt and accounts.txt on the printer Deskjet1001.
(f) $lp -i 1207 -H hold
The print job number 1207 is held for a while.
(g) $lp -i 1207 -H resume
The print job number 1207 is resumed for printing.
(h) $lp -i 1207 -q 100
The print job number 1207 is given the highest priority.
A command that goes along with the lp command is the cancel command. Let us now
discuss this briefly.
84 Unix and Shell Programming

4.19 cancel: CANCELLING PRINT COMMAND


The cancel command cancels existing print jobs.
Syntax cancel [ id ] [ printer_destination ]

The options and arguments used in this command are briefly explained in Table 4.15.
Table 4.15 Brief description of the options used in the
cancel command

Option Description
id It indicates the print job ID that we wish
to cancel.
printer_ It removes all jobs from the specified
destination printer destination.

Examples
(a) $cancel Deskjet1001
This command cancels all print jobs sent for printing at the Deskjet1001 printer.
(b) $cancel 1207
This command cancels the print job with ID 1207.

4.20 UNDERSTANDING .profile FILES


The .profile file exists in our home directory and is the start-up file that is automatically
executed when we log in to the Unix system. The file can be used to customize our
environment, setting PATH variable and terminal type, and also to write the commands and
scripts that we wish to automatically execute when we log in.
Note: The Unix operating system executes several system files including the .profile file before returning
the command prompt to the user.

The most basic variables used in the .profile file to set up an environment for us are as follows:
1. The PATH variable defines the search path to find the commands and applications that we
execute. Through the PATH variable, the commands and scripts can be executed in directories
other than their source directories (directories where the command or script exists).
2. $HOME is the name of the directory from where we begin our Unix session.
3. ENV refers to the environment variables.
We will learn about these variables in detail in Chapter 5.
Using any editor, we can add commands to the .profile file, which we wish to execute
automatically when we log in. Chapter 8 will help you use different editors. A new command
added to .profile will come into effect either when we log out and log in again or when we
run the .profile file at the command prompt through the following command:
$.$HOME/.profile

www.allitebooks.com
Advanced Unix Commands 85

4.21 calendar: GETTING REMINDERS


The calendar command reads the calendar file and displays appointments and reminders for
the current day.
Syntax calendar

Example For this command to work, we need to create a file named calendar at the root
of our home directory and write our appointments or reminders in the following format.
10/7/2012 Today is Board Meeting
10/8/2012 Visiting Doctor

Now, if today is 7 October 2012, and we execute the calendar command, the line Today is
Board Meeting will appear on the screen.

Note: To avoid executing the calendar command every day, add it at the end of our .profile file that we
just discussed.

4.22 script: RECORDING SESSIONS


The script command is used for recording our interaction with the Unix system. It runs in
the background recording everything that is displayed on our screen.
Syntax script [-a] filename

The options and arguments used in the command are briefly explained in Table 4.16.

Table 4.16 Brief description of the options used in the script command

Option Description
-a It appends the session into the filename. If this option is not specified, the filename
will be overwritten with the new data.
filename This gives the name of the file where our session will be recorded. If we do not
provide a filename to the script command, it places its output in a default file
named transcript.

Example The following example will begin recording the session in the file transact.txt:
$ script transact.txt

To exit from the scripting session, either press Ctrl-d or write exit on the command prompt
followed by the Enter key.
Figure 4.14(a) shows how the session is recorded in the file transact.txt. The commands
executed, cat, sort, mkdir, rmdir, etc., are recorded into the file transact.txt. To stop
recording, Ctrl-d keys are pressed. To confirm if the session is properly recorded in the file,
we execute the cat command to view the contents of the file transact.txt. Figure 4.14(b)
confirms that the session is correctly recorded in the file transact.txt.
86 Unix and Shell Programming

$ script transact.txt $ cat transact.txt


Script started, file is transact.txt Script started on 21 February 2012
10:24:40 PM IST
$ cat bank.lst
101 Aditya 0 14/11/2012 current $ cat bank.lst
102 Anil 10000 20/05/2011 saving 101 Aditya 0 14/11/2012 current
103 Naman 0 20/08/2009 current 102 Anil 10000 20/05/2011 saving
104 Rama 10000 15/08/2010 saving 103 Naman 0 20/08/2009 current
105 Jyotsna 5000 16/06/2012 saving 104 Rama 10000 15/08/2010 saving
105 Jyotsna 5000 16/06/2012 saving
106 Mukesh 14000 20/12/2009 current 106 Mukesh 14000 20/12/2009 current
107 Yashasvi 14500 30/11/2011 saving 107 Yashasvi 14500 30/11/2011 saving
108 Chirag 0 15/12/2012 current 108 Chirag 0 15/12/2012 current
109 Arya 16000 14/12/2010 current 109 Arya 16000 14/12/2010 current
110 Puneet 130 16/11/2009 saving 110 Puneet 130 16/11/2009 saving
$ sort -r bank.lst $ sort -r bank.lst
110 Puneet 130 16/11/2009 saving 110 Puneet 130 16/11/2009 saving
109 Arya 16000 14/12/2010 current 109 Arya 16000 14/12/2010 current
108 Chirag 0 15/12/2012 current 108 Chirag 0 15/12/2012 current
107 Yashasvj 14500 30/11/2011 saving 107 Yashasvj 14500 30/11/2011 saving
106 Mukesh 14000 20/12/2009 current 106 Mukesh 14000 20/12/2009 current
105 Jyotsna 5000 16/06/2012 saving 105 Jyotsna 5000 16/06/2012 saving
104 Rama 10000 15/08/2010 saving 104 Rama 10000 15/08/2010 saving
103 Naman 0 20/08/2009 current 103 Naman 0 20/08/2009 current
102 Anil 10000 20/05/2011 saving 102 Anil 10000 20/05/2011 saving
101 Aditya 0 14/11/2012 current 101 Aditya 0 14/11/2012 current

$ mkdir projects
$ mkdir projects
$ rmdir projects
$ rmdir projects
$ ^d
$ Script done, file is transact.txt
$ Script done on 21 February 2012
10:25:19 PM IST

(a) (b)
Fig. 4.14 Recording a session (a) Recording in the file transact.txt (b) Recorded session

4.23 CONVERSIONS BETWEEN DOS AND UNIX


There is a difference in format between Unix and DOS files. DOS (or Windows) files end
with both the line feed and carriage return, whereas Unix files end only with the line feed
character. It also means that in DOS files the new line character comprises carriage return
and line feed, whereas in Unix files, the new line character comprises only the line feed
character. The following are the two commands used in conversions between DOS and
Unix files:

1. dos2unix: Converts text files from DOS to Unix format


2. unix2dos: Converts text files from Unix format to DOS format

The syntax for the dos2unix command is as follows:

Syntax dos2unix [-b] file1 [file2]

The options and arguments used in this command are briefly explained in Table 4.17.
Advanced Unix Commands 87

Table 4.17 Brief description of the options used in the Examples


dos2unix command
(a) $ dos2unix a.txt b.txt
Option Description This converts the DOS file a.txt into b.txt.
-b It creates a backup of file1 with the name The new line characters consisting of the
file1.bak before converting it into Unix format. carriage return and line feed character
File1 It is the file in DOS format. in a.txt will be converted to line feed
File2 It is the file in which we wish to store the Unix character and stored in file b.txt, which is
format of the file. If file2 is not used, the original a file in Unix format.
file file1, will be converted into the Unix format. (b) $ dos2unix -b a.txt
The command converts the DOS file a.txt
Table 4.18 Brief description of the options and into the Unix format. The original DOS
arguments used in the unix2dos command format will be backed up and stored as
a.txt.bak.
Option Description
The syntax for the unix2dos command is as
-b It creates a backup of file1 by name file1.
follows:
bak before converting it into DOS format.
file1 It is a file in the Unix format. Syntax unix2dos [-b] file1 [file2]
file2 It is the file in which we wish to store the The options and arguments used in this
DOS format of the file. If file2 is not used command are briefly explained in Table 4.18.
then the original file, file1 will be converted
into the DOS format. Examples
(a) $ unix2dos a.txt b.txt
The command converts the Unix file a.txt into b.txt. The new line characters consisting
of line feed character in a.txt will therefore be converted to a combination of carriage
return and line feed character and stored in file b.txt, which is a file in DOS format.
(b) $ unix2dos -b a.txt
The command converts the Unix file a.txt into DOS format. The original Unix format
will be backed up and stored as a.txt.bak.

4.24 man: DISPLAYING MANUAL


The term man stands for manual and displays the online documentation of the given Unix
command. This command is meant for helping the user by providing usage, syntax, and
examples of using the given command.
Syntax man [-] [-k pattern] command

Here, – (hyphen)displays the information without stopping; -k pattern searches all the
commands documented in the man pages that contain the specified pattern, and displays the
list of matching commands.
Example $ man cp

This example displays the manual of the cp command. If the manual consists of several pages,
the first page will be displayed and we can press the spacebar to move on to the next page.
$ man -k backup
88 Unix and Shell Programming

$ man -k backup
/usr/dt/man/windex: No such file or directory
/usr/man/windex: No such file or directory
/usr/oenwin/share/man/windex: No such file or directory

$ catman -w
/usr/lib/getNAME: gnome-session-save.l - repeated date

$ man - k backup
asadmin-backup-domain asadmin-backup-domain (las) - performs a backup
on the domain
asadmin-list-backups asadmin-list-backup (las) - lists all backups
and restores
asadmin -restore-domain asadmin-restore-domain (las) - restores files
from backup
backup-domain asadmin-backup-domain (las) - performs a backup on the domain
list-backups asadmin-list-backups (las) - lists all backups and restores
nisbackup nisbackup (lm) - backup NIS+ directories
nistrestore nisrestore (lm) - restore NIS+ directory backup
restore-domain asadmin-restore-domain (las) - restores files form backup
tdbackup tdbbackup(lm) - tool for backing up and for validating the in
egbrity of samba \&. tdb files

Fig. 4.15 Manual containing the specified pattern

This example searches the documentation in the man pages and displays the list of commands
that contain the pattern backup. In case we get an error—windex directory not found—as
shown in Fig. 4.15, we need to create the windex directory by giving the catman –w command.
The windex directory once created will show the manual entry of the desired pattern.
Figure 4.15 shows the manual entry having the pattern backup.

4.25 CORRECTING TYPING MISTAKES


While typing commands on the terminal screen, it is inevitable that we might commit typing
mistakes. To correct the typing mistakes, the default key combinations listed in Table 4.19 are used.

Table 4.19 Default key combinations

Keys Description
Ctrl-h It erases text.
Ctrl-c The Interrupt key terminates any currently running process and returns to the prompt.
Ctrl-d It represents the exit or end of a transaction. The keys are used to indicate that the entering of text is complete.
Ctrl-j It represents the Enter key.
Ctrl-s It suspends the output temporarily and is usually used to stop the scrolling of screen output.
Ctrl-q Its function is opposite to that of Ctrl-s. It resumes the scrolling of output.
Ctrl-z It temporarily suspends a program and provides another shell prompt. In order to resume, it uses the jobs
command to find the program’s name and restarts it with the fg command.
Ctrl-u It kills the command line, that is, clears the complete line.
Ctrl-\ It terminates the running command and creates a core file containing the memory image of the command.
Advanced Unix Commands 89

We can change these default keys for erasing characters and killing a line through the stty
command. The stty command is discussed in detail in Chapter 10.
This chapter dealt with numerous advanced Unix commands. It covered the essential
commands for changing the permissions of files and directories, changing ownership and
groups, sharing files among groups, pipe operators, etc. In addition, the chapter covered
commands such as cut, paste, head, and tail that are used to extract desired regions from
given files. For comparing files, diff, cmp, uniq, and comm commands were discussed.
Commands for printing, measuring the time consumed in running certain commands,
showing calendar, recording sessions, and configuring the environment through .profile
have also been explained in detail.

■ SUMMARY ■

1. There are three classes of system users in Unix: The append operator, ‘>>’ is used for appending the output
Owner, Group, and Other. The read permission has a of a command to a file, that is, without overwriting its older
value = 4, the write permission has a value = 2, and the content. To redirect the standard input, we use the input
execute permission has a value = 1. redirection operator, that is, the ‘<’ (less than) symbol.
2. Unix assumes the default permissions of a directory 5. The pipe operator ‘|’ is used for sending the output of
to be 777 and that of a file as 666 and subtracts the one command as the input to another command.
permissions specified in the umask command to define 6. The difference between the pipe operator and the
their permissions at the time of their creation. output indirection operator ‘>’ is that the output
3. By default, each command takes its input from the indirection operator ‘>’ is mostly used for sending
standard input and sends the results to the standard the output of a command to a file, whereas the pipe
output; however, through I/O redirection, we can operator is used for sending output of a command to
change the default location of input and output. another command for further processing.
4. The ‘>’ (greater than) symbol is known as the output 7. DOS (or Windows) files end with both the line feed and
redirection operator and we can use it to divert the output carriage return, whereas Unix files end only with the
of any command to a file instead of the terminal screen. line feed character.

■ F U N C T ION SPECIFICATION ■

Command Function Command Function


chmod It changes file/directory permissions. cut It slices (cuts) a file vertically. Files can be
umask It stands for user file creation mask and cut on the basis of characters and fields too.
sets the default permissions of the files that paste It joins content from different files.
will be created in the future. wc (word It calculates the number of characters,
chown It transfers ownership of a file to another count) words, and lines in a file.
user. Once the ownership of a file is head It selects the specified number of lines and
transferred to another user, one cannot characters from the beginning of the given
change its permissions until they become file; the default number of lines selected is 10.
the owner again. tail It selects a specified number of lines and
chgrp It changes the group ownership of the file. characters from the bottom of the specified
groups It creates a group. file; the default number of lines selected is 10.
sort It sorts files either line-wise or on the basis pg It displays a long file page-wise, that is, one
of certain fields. screen page at a time.
90 Unix and Shell Programming

Command Function Command Function


man It displays the online documentation or amount of time that the command takes to
manual of the given Unix command. execute its own code. The sys time repre-
diff It displays the difference between two files sents the time taken by Unix to invoke the
in a format that consists of two numbers command.
and a character in between. The number to lp It stands for line printer and prints files.
the left of the character represents the line Using the lp command, we can define
number in the first file, and the number to the printer we wish to use through the -d
the right of the character represents the line option, the number of copies through the
number in the second file. -n option, the pages to print through the -p
uniq It finds and displays duplicate lines in a file. option, and priority through the -q option.
In addition, it can be used to display only cancel It cancels existing print jobs.
the unique lines in a file. The -u option of profile file It exists in the home directory and is the
the uniq command removes all duplicate start-up file that automatically executes
lines from a file. The -d option of the uniq when we log in to the Unix system. It can be
command is used to display all duplicate used to customize our environment. It can
lines in a file. also be used to write the commands and
split It splits a file into a specified number of scripts that we wish to execute automat-
lines or bytes. ically when we log in.
cmp It compares two files and indicates the line calendar It reads the calendar file and displays
number where the first difference in the files appointments and reminders for the current
occurs. It does not display anything if the day.
files being compared are exactly the same. script It records our interaction with the Unix
comm It displays or suppresses the content com- system. It runs in the background recording
mon to two files. everything that shows up on the screen.
time It displays the time usage by a specific dos2unix It converts text files from a DOS to a Unix
command. The real time is the elapsed time format.
from the invocation of the command till its unix2dos It converts text files from a Unix format to a
termination. The user time represents the DOS format.

■ EXERCISES ■

Objective-type Questions
State True or False
4.1 The three classes of system users that are used in 4.5 If we transfer the ownership of our file to
assigning permissions to the files and directories another person, we can no longer change its file
are Owner, Group, and Family. permissions.
4.2 To delete a file, a write permission is not required 4.6 Either the owner or the super user can change the
but an execute permission is required. ownership of a file.
4.3 By using the umask command, we can specify 4.7 We can make a group of users share permissions
the permissions that we want to deny. on a given set of files.
4.4 The system-wide default permission for a dir- 4.8 The sort command can sort the file on the basis
ectory is 666. of a given field in the file.
Advanced Unix Commands 91

4.9 We cannot sort a file in the reverse order through in the file occurs.
the sort command. 4.15 The cmp command displays a message ‘exactly
4.10 The ‘<’ symbol is the output redirection operator same’ if the files compared are exactly the
and the ‘>’ symbol is the input redirection operator. same.
4.11 The ‘>>’ symbol redirects the output of a command 4.16 The comm command either displays or hides the
to a file after overwriting its earlier content. content common to two files.
4.12 Several commands can be attached using the 4.17 The time command displays the system time and
pipe operator. even allows it to be modified.
4.13 The default number of lines into which the split 4.18 The real time is the elapsed time from the
command splits a file is 100 lines. invocation of the command till its termination.
4.14 The cmp command compares two files and in- 4.19 The calendar command displays the calendar of
dicates the line number where the first difference a specified month and year.

Fill in the Blanks


4.1 There are three types of system users: owner, 4.11 The diff command displays the differences
group, and . between two files that are being compared in a
4.2 The command used to change the permission of format that consists of two and a
the file or directory is . in between.
4.3 The symbol x in the chmod command represents 4.12 The command is used for identifying
permission. and displaying duplicate lines in a file.
4.4 The command used to get the specified nu- 4.13 The option of the uniq command
mber of lines from the beginning of a file is removes all duplicate lines from a file.
. 4.14 The file can be used to customize
4.5 The option used with the tail command to skip our environment.
the specified number of lines is . 4.15 For recording our interaction with the Unix
4.6 The option used with the tail command to get system, command is used.
the specified number of characters from the end 4.16 The option of the lp command is
of file is . used for defining the destination printer while
4.7 Unix assumes the default permissions for a file to the option is used for defining the
be . number of copies to be printed.
4.8 The input redirection operator is represented as 4.17 The command is used to display
. appointments and reminders for the current day.
4.9 To display content one screen page at a time, 4.18 The command converts text files
command is used. from the DOS format to the Unix format.
4.10 The command is used for displaying 4.19 The command is used for cancelling
the documentation of a command. a print job.

Multiple-choice Questions
4.1 The command used for setting default permissions 4.4 The command used for comparing two files is
of files and directories is (a) comp (c) uniq
(a) chmod (c) default (b) compare (d) diff
(b) umask (d) chstat 4.5 The option of the uniq command that removes
4.2 The three types of system users are User, all duplicate lines is
Group, and (a) -d (c) -r
(a) Other (c) Community (b) -u (d) -m
(b) Society (d) Everyone 4.6 The command used to change the group of a file
4.3 The option used with the chgrp command to is
change the group of a symbolic link is (a) groups (c) chgrp
(a) -s (b) -l (c) -g (d) -h (b) chmod (d) ls -g
92 Unix and Shell Programming

4.7 The statement $chown :accounts a.txt will selects and displays lines in reverse order from
change the bottom to the top is
(a) group of the file (a) -t (c) -b
(b) owner of the file (b) -r (d) -c
(c) nothing 4.10 The statement $ head -c 10 a.txt b.txt
(d) owner and group of the file displays
4.8 The option used with the sort command to re- (a) the first 10 lines of a.txt file only
move duplicate lines in a sorted output is (b) the first 10 lines of a.txt and b.txt files
(a) -d (b) -q (c) -u (d) -n (c) the first 10 characters of a.txt file only
4.9 The option used with the tail command that (d) the first 10 characters of a.txt and b.txt files

Programming Exercises
4.1 What will the following commands do? (d) To display the first two lines of the files
(a) $chmod 410 management.txt mbacourse.txt and management.txt
(b) $umask 233 (e) To display lines starting from the fifth till the
(c) $chgrp jobs mbacourse.txt end of the file in mbacourse.txt
(d) $head -c 100 mbacourse.txt management (f) To show the content of the file finance.txt
.txt located in accounts directory page-wise
(e) $tail -2 management.txt (g) To sort the file a.txt in reverse order and
(f) $man -K disk store it in file b.txt
(g) $cut -d"," -f3 bank.lst (h) To cut the first and third fields of the file
(h) $paste -d"<>" names.txt numbers.txt letter.txt that is delimited by a tab space
(i) $sort a.txt > b.txt (i) To create a group by the following name:
(j) $ split -5 numbers.txt temp latestprojects
(k) $ cmp -s a.txt b.txt 3 5 (j) To compare two files, accounts.txt and
(l) $ time ls | sort | lp finance.txt, and show the changes that need
(m) $ lp -d Epson100 -P 10-15, 20 a.txt to be made in the file accounts.txt to make
(n) $ comm a.txt b.txt it similar to finance.txt
4.2 Write the command for the following tasks: (k) To display all duplicate lines in the file
(a) To assign read, write, and execute permissions accounts.txt
to the owner; read and write permission to the (l) To remove all duplicate lines in the file
group; and only read permission to others for accounts.txt and save it in another file
the file mbacourse.txt correct.txt
(b) To set permissions for the directories to be (m) To split a file accounts.txt into the files
created in the future as read, write, and execute accountaa, accountab, accountac, and so
for the owner; read and write for the group; on, each consisting of 20 bytes
and only read for others (n) To compare two files, a.txt and b.txt, and
(c) To change the ownership of the file mbcourse. display the first character that is different in
txt to charles the two files

Review Questions
4.1 Explain the following commands with syntax and (b) What is the difference between the cmp and
examples. diff commands?
(a) pg (c) dos2unix 4.3 (a) Explain the different options used in the lp
(b) wc (d) tail command while printing a file.
4.2 (a) What is the difference between the chown and (b) Explain how a file is sorted.
chgrp commands? 4.4 What is the difference between the following pairs
Advanced Unix Commands 93

of commands that are used for extracting content (b) head and tail commands
from the files? 4.5 Briefly explain how the file access permissions are
(a) cut and split commands handled in the Unix operating system.

Brain Teasers
4.1 Suppose you want to assign read, write, and 4.6 Correct the mistake in the following command
execute permissions to the user, that is, the owner to cut the first and third fields of the file a.txt
of the file a.txt using the following command. delimited by the ‘|’ symbol
What is wrong with the following command? $ cut -f1,3 a.txt
Correct the mistake. 4.7 Is there a way to split a file a.txt into pieces
$ chmod o=rwx a.txt that are 10 kB each? If yes, what is that?
4.2 Correct the following command to change the 4.8 When you compare two files, a.txt and b.txt
owner and group of the file a.txt to user chirag with the cmp command, no output appears on the
and accounts respectively. screen. What does this mean?
$ chown accounts:chirag a.txt 4.9 Correct the mistake in the following command
4.3 Correct the mistake in the following command to suppress the display of the content, that is,
in order to change the group of the symbolic file commands in the files a.txt and b.txt.
b.txt to accounts. $ comm -1 a.txt b.txt
$ chgrp accounts b.txt 4.10 Correct the mistake in the following command in
4.4 Can you sort the file a.txt on the second and order to print two copies of the file a.txt.$ lp
third field skipping the first field? How? a.txt -q 2
4.5 The following command overwrites the content 4.11 What will happen if you add the calendar
of the file a.txt. What command will you use command in the .profile file?
to avoid the accidental overwriting of an existing 4.12 Correct the mistake in the following command to
file? extract line numbers 10 to 15 from the file a.txt.
$ ls > a.txt $ head -10 a.txt | tail +15

■ ANSWERS TO OBJECTIVE-TYPE QUESTIONS ■


State True or False 4.14 True 4.7 666 Multiple-choice
4.1 False 4.15 False 4.8 < Questions
4.2 False 4.16 True 4.9 pg
4.1 (b)
4.3 True 4.17 False 4.10 man
4.2 (a)
4.4 False 4.18 True 4.11 numbers, 4.3 (d)
4.5 True 4.19 False character 4.4 (d)
4.6 True 4.12 uniq
4.5 (b)
4.7 True Fill in the Blanks 4.13 -u 4.6 (c)
4.8 True 4.1 Other 4.14 .profile
4.7 (a)
4.9 False 4.2 chmod 4.15 script 4.8 (c)
4.10 False 4.3 execute 4.16 -d, -n 4.9 (b)
4.11 False 4.4 head 4.17 calendar 4.10 (d)
4.12 True 4.5 +n 4.18 dos2unix
4.13 False 4.6 -c 4.19 cancel

You might also like