UNIX Shell Programming
UNIX Shell Programming
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.
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
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.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.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.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.
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
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
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.
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
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.
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
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.
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.
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.
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.
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
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.
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.
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.
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
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.
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
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.
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
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.
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
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.
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.
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
To get all the files beginning with a specific character, we can give a command using the
following syntax:
Syntax $ ls charactername*
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.
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
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
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
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.
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.
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.
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.
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.
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
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).
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
Fig. 3.2 New lines displayed in the form of $ Fig. 3.3 Tabs and form feeds displayed as ^I and ^L
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
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.
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.
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.
(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.
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
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
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.
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.
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.
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
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]
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
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
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
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
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 ■
■ 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.
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
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.
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
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.
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.
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.
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).
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.
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
Note: The group hospital must exist before giving this command.
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.
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.
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.
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.
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.
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
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
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.
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.
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.
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.
When used without an option, this displays the first ten records (lines) of the specified file.
head -3 bnk.lst
It will display the first three lines of both the files, bnk.lst and notes.txt, one after the other.
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
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
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.
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
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
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 all the duplicate lines in a file.
$ sort a.txt | uniq -d
The following command is used to display the count of duplicate occurrences in a file.
$ sort a.txt | uniq -c
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
(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
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
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.
The options used in this command are briefly explained in Table 4.14.
Advanced Unix Commands 83
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.
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.
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
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.
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
$ 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
The options and arguments used in this command are briefly explained in Table 4.17.
Advanced Unix Commands 87
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
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.
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 ■
■ 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.
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