1. Winter Training, December 2011
Unix and Shell Programming
Department of COE and SE,
Delhi Technological University
Instructor: Divyashikha Sethia
2. Contents
UNIT 1: INTRODUCTION TO UNIX ..........................................................................3
UNIT 2: SHELL SCRIPTING..................................................................................... 63
UNIT 3: ADVANCED SHELL SCRIPTING, SED, AND AWK .................. 143
3. UNIT 1: INTRODUCTION TO UNIX
1. THE UNIX OPERATING SYSTEM – AN OVERVIEW.................................7
2. UNIX COMMANDS................................................................................................... 21
3. UNIX FILE SYSTEM ................................................................................................ 33
4. THE VI TEXT EDITOR ............................................................................................ 45
4.
5. COE
Unit 1, Lesson 1
LESSON 1 T HE UNIX OPERATING S YSTEM – AN
OVERVIEW
1. THE UNIX OPERATING SYSTEM – AN OVERVIEW .................................................7
1.0 OBJECTIVES ...............................................................................................................7
1.1 INTRODUCTION ...........................................................................................................7
1.2 INTRODUCTION TO THE COMPUTERS .........................................................................7
1.2.1 Typical hardware components of a computer.................................................8
1.3 OPERATING SYSTEM..................................................................................................8
1.3.1 Virtual Memory.....................................................................................................9
1.4 UNIX OPERATING SYSTEM .................................................................................... 10
1.4.1 History of UNIX ................................................................................................. 10
1.4.2 Importance of UNIX ......................................................................................... 11
1.5 UNIX OPERATING SYSTEM – ATTRIBUTES AND COMPONENTS ............................ 12
1.6 STARTING WITH UNIX............................................................................................. 14
1.7 CHANGING YOUR PASSWORD ................................................................................ 15
1.8 ENTERING COMMANDS IN THE UNIX SYSTEM ....................................................... 16
1.8.1 Command Options and Arguments ............................................................... 17
1.9 SUMMING UP........................................................................................................... 17
1.10 ANSWERS TO THE SELF CHECK QUESTIONS ........................................................... 17
1.11 TERMINAL QUESTIONS............................................................................................. 18
1.12 REFERENCES .......................................................................................................... 18
6.
7. COE
Unit 1, Lesson 1
1. The UNIX Operating System – An Overview
Use and influence of computers has been steadily increasing in the last few
decades. Today, computers play a pivotal role in all walks o f life. An operating
system (OS) is a core component of the computer system. An operating system lets
a computer function as multi-user, multitasking and multithreading environment, thus
augmenting the power of the computer. UNIX is an operating system that offers its
users all these capabilities along with numerous other features. In this lesson we will
look upon the features and components of the UNIX system that make it very useful
and popular. In the subsequent lessons we will explore the features and components
of UNIX in more details.
1.0 Objectives
After going through this lesson, you will be able to
Understand the concepts of the Operating System
Understand what is the UNIX Operating Systems
Understand the importance and popularity of UNIX Operating System
Understand how to start working on a UNIX machines
1.1 Introduction
In the modern age, we have seen the computer doing wonders, from children
playing games to the scientists launching satellites; we can clearly see that
the computers are playing a important role. It is the operating system that has
made the computing in the modern world possible and efficient.
1.2 Introduction to the computers
Unlike calculator, a computer carries out user specified tasks. An inherent
power provided by a computer is that it can be programmed to do variety of
tasks. Computers are mostly general purpose computers in the sense that a
computer can be used to play a game and the same computer can be used to
perform a circuit simulation.
A computer consists of hardware and software. A computer can be defined as
a programmable machine which responds and executes a list of instructions.
These lists of instructions are called programs. The hardware components are
the physical components and software is data o r instruction.
7
8. COE
Unit 1, Lesson 1
1.2.1 Typical hardware components of a computer
Hardware components in computer are what you can see and touch.
Memory: Enables the computer to store the temporary data and instructions.
This is used in the computer during the execution of various instruction sets.
While evaluating the following expression, the
intermediate results are stored in memory
Sum = 2 + 1 + 3 * 4
Mass storage devices: These are used for the bulk storage of data, such as,
disk drives and tape drives.
Input devices: Interface to take the instructions from the user to the computer.
Commonly used input devices are keyboard, mouse, web camera, etc.
Output Devices: Display the results of the instruction processing done by the
computer. Commonly used are display monitors and the printers.
Central Processing Unit (CPU): The brain of the computer in which all the
processing is done. It reads the data from memory or input and executes the
instructions. CPU consists of ALU (Arithmetic Logic Unit) and CU (Control
Unit). ALU is responsible for all calculations and CU is responsible for getting
instructions and data for execution.
Working with the hardware components alone is very difficult because their
controls are very cryptic. Instead, software components are used to drive the
hardware components. The operating system is also one such software.
1.3 Operating System
An Operating System (OS) is an important program that runs on the
computer. An operating system performs the very basic tasks, such as
recognizing inputs from the user, sending outputs to the display, keeping track
of file and directories on the disk, and controlling the peripheral devices such
as the disk drivers and printers.
8
9. COE
Unit 1, Lesson 1
The OS also works as a traffic cop - it makes sure that different program and
users running at the same time do not interfere with each other. The operating
system is also responsible for security and blocking unauthorized users.
Operating systems can be classified as follows:
Multi-user: Allows multiple users to use computers at the same time.
Multiprocessing: Supports running parts of a program in parallel.
Multitasking: Allows multiple programs to run concurrently on a single CPU.
Multithreading: Allows different parts of a single program to run concurrently.
Operating systems provide a platform on which other programs, called
application programs, can run. The application programs must be written to
run on a particular operating system. Your choice of operating system,
therefore, determines to a great extent the applications you can run. For PCs,
the popular operating systems are DOS, OS/2, Windows and Linux.
1.3.1 Virtual Memory
Programs that run on a computer may need more memory than what is
available physically on that computer. Many operating systems provide an
illusion to the user of much larger memory. This is done by loading only partial
program and data in physical memory. Only the parts that are needed for
current execution are brought into physical memory. So, bigger programs can
be run even if physical memory is small.
9
10. COE
Unit 1, Lesson 1
Self-Check Questions
1. A ____________ is a prerecorded set of instructions, which is executed b y the
computer to perform some task.
2. A computer is a specific purpose machine that can not be tweaked to perform
some other tasks. (True/False)
3. The operating systems keep the temperature inside the computer down, so that
the functioning is proper. (True/False)
4. A ___________ system allows running parts of a program in parallel, on more
than one CPU.
5. In a _______________ system, a large number of users can use the system
concurrently.
6. The ____________ memory is an imaginary memory which is used by the
Operating System to get a larger address space.
1.4 UNIX Operating System
1.4.1 History of UNIX
The UNIX operating system found its beginnings in MULTICS, which stands
for Multiplexed Operating and Computing System. The MULTICS project
began in the mid 1960s as a joint effort by General Electric, Massachusetts
Institute for Technology and Bell Laboratories. In 1969 Bell Laboratories
pulled out of the project.
One of Bell Laboratories people involved in the project was Ken Thompson.
He liked the potential MULTICS had, but felt it was too complex and that the
same thing could be done in simpler way. In 1969 he wrote the first version of
UNIX, called UNICS. UNICS stood for Uniplexed Operating and Computing
System. Although the operating system has changed, the name stuck and
was eventually shortened to UNIX.
Ken Thompson teamed up with Dennis Ritchie, who wrote the first C compiler.
In 1973 they rewrote the UNIX core (called kernel) in C. The following year a
version of UNIX known as the Fifth Edition was first licensed to universities.
The Seventh Edition, released in 1978, served as a dividing point for two
divergent lines of UNIX development. These two branches are known as
SVR4 (Release 4) and BSD.
Ken Thompson spent a year's sabbatical with the University of California at
Berkeley. While there are two graduate students, Bill Joy and Chuck Haley,
wrote the first Berkeley version of UNIX, which was distributed to students.
This resulted in the source code being worked on and developed by many
different people. The Berkeley version of UNIX is known as BSD, Berkeley
10
11. COE
Unit 1, Lesson 1
Software Distribution. From BSD came the VI editor, C shell, virtual memory,
Send mail, and support for TCP/IP.
1.4.2 Importance of UNIX
During past 25 years the UNIX OS has evolved into powerful, flexible, and
versatile and robust operating system. It serves as the operating system for
variety of computers , for single user personal computers , engineering
workstation , multi-user microcomputers, minicomputers, mainframes, super
computers and as well as special application devices . There are
approximately 20 million machines now running UNIX and more than 100
million users, and this popularity and rapid growth is estimated to be
increased further. The success of UNIX is due to many factors including its
portability to a wide range of machines, its adaptability and simplicity, the wide
range of tasks it can perform, its multi-user and multitasking nature, and its
suitability for networking. What follows is a description of the features that
have made UNIX system so popular.
Multi-user and Multitasking abilities
The UNIX OS allows the use of a single computer by many users. It is also a
multitasking system that is it allows more than one application to be run on the
same computer at the same time.
Powerful command set
The UNIX OS provides a consistent and powerful set of commands that has
made it very useful particularly for the technical people .
Combining commands
The UNIX provides constructs like pipes and redirection of commands which
enables the user to create his own powerful utilities from UNIX commands.
Excellent environment for Networking
UNIX offers program and utilities that provide the services needed to build
networked applications - the basis for distributed, networked computing. With
networked computing, information and processing is shared amongst different
computers in a network. It is useful in client server computing where the
machines on the network can be client and servers at the same time. UNIX
system is used as the base system for the development of the internet
services and the growth of internet.
Portability
The UNIX system is far easy to be ported to new machines than other
operating systems. The fact that, it is portable to almost any computer, results
from its being almost entirely written in C programming language.
11
12. COE
Unit 1, Lesson 1
1.5 UNIX Operating System – Attributes and Components
The UNIX operating system is made up of several major components. Some
of these components are the commands, the file system, the shell, the kernel
and the commands.
12
13. COE
Unit 1, Lesson 1
The Commands and User Programs
UNIX provides a number of built-in commands and in addition user programs
can also run.
The File System
The basic unit that stores information in the UNIX system is called a file. The
UNIX file system provides a logical method of organizing files. Files are
organized in a hierarchical file system where the files are grouped together in
a directory.
Example: Hierarchical File Structure
/dtu/COE_Course/COE_101/schedule
Here ―dtu‖ is the parent directory which is in ‗/‘
root and other directories are in it
An important simplifying feature of the UNIX system is the wa y it treats the
files. For example, physical devices are treated as files, this permits the same
command to work for an ordinary file or a device i.e. same command can be
used to write to a file and printer.
The Shell and shell scripts
The shell is the command interpreter in the UNIX operating system. It reads
the user specified commands and interprets them as requests to execute a
program or a set of programs, which it then arrange to carry them out. Shell
also provides a programming language. Shell scripts are covered in
subsequent chapters of this unit.
The kernel
The kernel is the core of the OS. The kernel interacts directly with the
hardware through a set of programs called the device drivers that are built into
the kernel. It provides the set of services that can be used by the other
programs; also it safeguards these programs from hardware layers. The major
functions of the kernel are to maintain the file system, manage memory,
access control to the computer, and handle the interrupts (these are the
signals to terminate the processes, ctrl + C is a common example)., error
handling, I/O handling which enables the computer interaction with the
peripheral devices such as printers, monitors, storage devices, etc.).
Programs use kernel through the system calls. For example, if the user wants
some file to be opened then the program generates a system call to open the
directories and then the files.
The figure below shows the relationship amongst various components of the
UNIX file system.
13
14. COE
Unit 1, Lesson 1
The User Commands
The Shell
The Kernel
Hardware
Components of UNIX operating system (shown in gray).
Self-check Questions
7. UNIX is a multi-user OS and also possesses multitasking abilities. (True/False)
8. The first version of the UNIX Operating System was known as _____________.
9. The file system in a UNIX Operating System is a hierarchical structure.
(True/False)
10. The ____________ in a UNIX Operating System is used to interact with the
hardware and executes the user commands and program.
11. The command interpreter in the UNIX system is called ___________.
12. The programs in the UNIX systems interact using the __________ calls with the
kernel to perform the tasks.
1.6 Starting with UNIX
This section is dedicated to the learning of how to log into a UNIX system and
how to change password on a UNIX system. We will touch the details of the
different types of system configurations and how we can log on to systems
having these configurations.
Selecting a login
Every UNIX user on a multi-user system is recognized by a login name which
is the only identity he has on the system. This is to be set before you use a
multi-user or a single user UNIX system, to log onto the system.
UNIX provides excellent built-in security. Therefore no users are permitted
unless they are identified. For this identification, each user has a login ID.
14
15. COE
Unit 1, Lesson 1
The login ID is typically allocated by an authority (known as the system
administrator). The system administrator is also responsible to add new users
to the system and provide them a login name and an initial work enviro nment
and password on the computer.
UNIX shows a login prompt initially. User needs to type-in his login ID. Then
the password prompt comes. After you correctly type in the password, you get
logged into the system. The example below shows this process.
login: akash
password:
―akash‖ is the user login name.
Note to keep password secure, it is not displayed when
you type it.
Connecting to the UNIX System
In a multi-user system you have to contact the system administrator as to how
you can connect to the system using your PC or terminal. Your PC can be
directly wired to a computer or it can be connected via LAN.
Direct Connect - This is a method of connecting to UNIX machines when
there is a single machine.
Dial-in Access - You can dial in to the UNIX network using a modem, use
terminal emulators to get the UNIX prompt.
Local Area Network (LAN) - LAN is a client server model. Connect to the
server using the client workstation and use the UNIX capabilities.
IP Networks
Using IP networks like internet one can connect to some remote machines
using telnet capability of UNIX.
1.7 Changing Your Password
Your password is very important information that you must not share with
anyone. You must change it regularly (say once in 2 months) and also should
remember it (you must not write it on paper). Your password should contain 6
to 8 letters and should not simply be your name, your date of birth, etc. Your
password should also contain at least one non alphabet (maybe a number).
To change the password of your login you can use the passwd command.
bash> password
password: Changing password for sushobhit
Old password:
New Password:
Re-enter new password:
bash>
15
16. COE
Unit 1, Lesson 1
There is a simple scheme to create complex passwords and still remember
them! All you do is to take the first letters of a line of your favorite poem or
song and add a number or symbol to make a complex password. Here is an
example: Say you pick the like ―Twinkle twinkle little star‖. Take the first
letters to makes a string Ttls. And suppose your favorite symbol is = (equal
sign) and favorite number is 2 so you append these to the string to make your
complex password as Ttls2=. You can see that for anyone else it will too hard
to find out while it is very easy for you to remember.
NOTE: If you forget your password it cannot be retrieved even by the system
administrator. The only remedy in such cases is that the system administrator
can reset the password.
Self-Check Questions
13. ________________ is the program which is used to connect to the UNIX system
from a remote system.
14. ___________________ in a multi-user system is the person who is responsible
for maintaining the system.
15. Get the odd one out
To connect to a UNIX system one of the following measures can be used
a. Dial-in access
b. IP Networks
c. LAN
d. System Calls
16. If you forget your password system administrator can give you permissions.
(True/False)
1.8 Entering Commands in the UNIX System
UNIX provides numerous commands. When the user types some command
on UNIX prompt then the shell invokes the program for the command, the
command program can invoke many system calls, these calls then interacts
with the hardware.
16
17. COE
Unit 1, Lesson 1
1.8.1 Command Options and Arguments
UNIX system has a standardized comma nd syntax that is applicable to almost
all the UNIX commands. Every command has some base functionality and
additional functionality that are provided by the command line arguments.
For Example, the ls command can be used to list the contents of a directory.
bash> ls
README 2134.tar.gz game_scores game_schedule
Now let‘s use ls command with some option
bash> ls –l
-rw-r--r-- 1 anmol friends 10777 Mar 30 16:26
README
-rw-r--r-- 1 achint friends 21483 Feb 28 17:39
2134.tar.gz
drwxr-xr-x 2 amit friends 4096 Dec 12 16:41
game_scores
This example shows the usage of –l argument of ls command, which outputs
thedrwx------ 3of ls command. 4096 May 10 2006
long format arat friends
game_schedule
Another command that is frequently used is ‗man‘ command. This is used to
displays the manuals of different commands.
1.9 Summing Up
An operating system is the most important software in any computer as it fills
the communication gap between a user and the underlying hardware. UNIX
operating system with its unique qualities and ease to adapt is a popular and
powerful operating system now days. In the chapters to follow we will explore
the powers of UNIX in some details.
1.10 Answers to the self check questions
1. program
2. False
3. False
4. multitasking
5. multi-user
6. virtual memory
7. True
8. MULTICS
9. True
10. Shell
11. Shell
12. System calls
17
18. COE
Unit 1, Lesson 1
13. telnet
14. system administrator
15. h
16. False
1.11 Terminal questions
1. List and expand briefly the components of the UNIX operating system.
2. What are the features of UNIX operating system that are the cause of its
popularity amongst the users?
3. Explain briefly the possible modes to log onto a UNIX system
1.12 References
1. http://www.uwsg.iu.edu/usail/concepts/unixhx.html
18
19. COE Unit 1, Lesson 2
LESSON 2 UNIX COMMAND
2. UNIX COMMANDS ......................................................................................................... 21
2.0 OBJECTIVES ............................................................................................................ 21
2.1 INTRODUCTION ........................................................................................................ 21
2.2 THE C OMMANDS CLASS .......................................................................................... 21
2.3 CONNECTING TO UNIX ........................................................................................... 22
2.3.1 telnet command ................................................................................................ 22
2.3.2 rlogin command ................................................................................................ 22
2.4 FILE MANAGEMENT ................................................................................................. 22
2.4.1 mv command..................................................................................................... 23
2.4.2 cp command...................................................................................................... 23
2.4.3 rm command ..................................................................................................... 23
2.5 A COMMUNICATION RELATED COMMAND - FTP ....................................................... 23
2.6 INFORMATION .......................................................................................................... 24
2.6.1 man command .................................................................................................. 24
2.6.2 du – Disk usage ................................................................................................ 25
2.6.3 df – Disk free ..................................................................................................... 25
2.6.4 quota................................................................................................................... 25
2.6.5 who – Finding out who is logged on .............................................................. 25
2.7 PRINTING ................................................................................................................. 26
2.7.1 lpr – Printing ...................................................................................................... 26
2.7.2 lprm – Removing a printing job ...................................................................... 26
2.7.3 lpq – Checking the printing queue ................................................................. 26
2.8 PROCESS CONTROL................................................................................................ 26
2.8.1 ps – Finding the process ................................................................................. 26
2.8.2 & - Running process in background .............................................................. 27
2.8.3 Cntrl-z – Suspending a processes................................................................. 27
2.8.4 Jobs – Finding the process in background................................................... 27
2.8.5 Kill – Killing a process...................................................................................... 27
2.8.6 nice – reducing the priority of process .......................................................... 27
2.9 MISCELLANEOUS COMMANDS ................................................................................. 28
20. COE Unit 1, Lesson 2
2.9.1 alias / unalias command.................................................................................. 28
2.9.2 cal (calendar) command.................................................................................. 28
2.9.3 clear command ................................................................................................. 28
2.9.4 crontab command............................................................................................. 28
2.9.5 csh command.................................................................................................... 28
2.9.6 history command .............................................................................................. 29
2.9.7 date command .................................................................................................. 29
2.9.8 echo command ................................................................................................. 29
2.9.9 grep command.................................................................................................. 29
2.9.10 unset command ................................................................................................ 29
2.9.11 tar command .................................................................................................... 29
2.9.12 tee command .................................................................................................... 29
2.9.13 touch command ................................................................................................ 29
2.10 SUMMING UP........................................................................................................... 30
2.11 ANSWERS TO THE SELF-CHECK QUESTIONS ........................................................... 30
2.12 TERMINAL QUESTIONS ............................................................................................ 30
21. COE Unit 1, Lesson 2
2. Unix Commands
UNIX as any other operating system provides a set of commands to its users, using
which, the users can perform the tasks they want. There is a huge variety of
commands that UNIX provides its user. In the present lesson we will discover and
read about the usage of many of the commands in UNIX.
2.0 Objectives
After going through this lesson, you will be able to
Use the UNIX commands to perform tasks
Understand how to send and receive mails on UNIX
Understand the file management basic command
Understand the information and communication system using the UNIX
2.1 Introduction
UNIX provides a number of commands. For the ease of understanding we can
divide these commands into various categories.
2.2 The Commands class
UNIX commands can be grouped amongst few broader classes:
Starting and Ending
These are the commands which are basically used to logon to the UNIX
system, or to initiate working on to the UNIX system.
File Management
File is the basic data holding entity in the UNIX systems. There is a set of
commands that can be used to maintain the file system so as to keep the data
stored in the files, secured, updated and maintained.
Communication
UNIX provides communications at many levels, including mails, writing
messages, exchanging files, etc.
21
22. COE Unit 1, Lesson 2
Information
UNIX provides a number of commands to get information about the system
like who are logged in, how much disk space is available, etc.
Printing
In UNIX user can give the print command and also can monitor the status of
the job or can remove the job if required from the queue.
Job and Process control
As there are lots of processes which are going on in a UNIX system, it is
sometimes required to get the information related to the user jobs running on
the system. For this purpose UNIX provides a set of commands to monitor,
kill, prioritize and resuming the jobs.
In the present chapter we will look at some of these commands in detail and
the other commands will be discussed in the chapters to follow.
2.3 Connecting to UNIX
Before we learn anything in details the very first thing we will look at is the
process that a user has to adopt to start with the UNIX system.
2.3.1 telnet command
The telnet command is used for logging into a remote system. The telnet
command presents the same login and password prompts as done on a local
system.
2.3.2 rlogin command
The rlogin command is used to connect to a remote computer. It is
comparatively easier to use then telnet. Here is the syntax of rlogin command:
rlogin [-l username] hostname
In this the username is taken by default the username of the current user.
Hostname is the name of the UNIX machine that is to be logged on.
2.4 File Management
A file is a basic data storage entity in a UNIX system. There is a set of
commands that can be used to maintain this system. We will be having an
introductory flavor of these commands in this chapter with the complete
discussion being taken up in the chapter on file system. Readers are advised
to have a look at the man pages of each of these commands and try to
understand what exactly these commands are used for.
22
23. COE Unit 1, Lesson 2
2.4.1 mv command
The mv command moves a file. The command can also be used to rename a
file. Here is a simple example of mv command.
bash> ls
tempPresentation.txt
bash> mv tempPresentation.txt
finalPresentation.txt
bash> ls
finalPresentation.txt
2.4.2 cp command
The cp command copies a file. Here is a simple example of the cp command.
bash> ls
tempPresentation.txt
bash> cp tempPresentation.txt
finalPresentation.txt
bash> ls
tempPresentation.txt finalPresentation.txt
2.4.3 rm command
The rm command removes a file. Here is an e xample of the rm command.
bash> ls
tempPresentation.txt finalPresentation.txt
bash> rm tempPresentation.txt
bash> ls
finalPresentation.txt
2.5 A communication related command - ftp
The ftp (file transfer protocol) command is used for copying files from a
remote computer to another computer. While mv and cp works on the same
system at a time you might need to get files from across systems at the same
time ftp can be used for that.
In the example below we can see how ftp can be used to connect to a remote
machine. In this example user ‗achint‘ gets file from machine mitserv.
23
24. COE Unit 1, Lesson 2
bash> ftp mitserv
Connected to mitserv
Name: achint # User types his
login id
31 Please specify the password.
Password: # password will not
be visible
230 Login successful.
Remote system type is UNIX.
ftp> get myPresentation.txt # Now you are in ftp.
See the prompt
250KB data transfer successful
ftp> quit
The ftp prompt provides few limited commands as listed below:
bash> # You are out of ftp
now.
bin – Changes the file transfer type to support the binary image transfer.
get – Used to ‗get‘ the files from remote machine
mget- multiple get commands
ls – Used to list the contents of a directory on a remote machine
cd – Used to change directories on the remote machine
pwd – Used to get the present working directory on remote host
lpwd – Gives the current working directory in local host.
2.6 Information
The information UNIX commands, regarding other users, disk quota and other
things can be retrieved using some of the UNIX commands. In this section we
will be discussing about some of these commands.
2.6.1 man command
UNIX traditionally provides the manual pages (called ‗man‘ pages) for all the
built-in commands and for system calls.
You can learn a lot by referring to the manual pages for commands.
The general syntax of the command is
man [-] [-k keywords] topic/command
The example below shows a part of the manual page of ‗du‘ command.
bash> man du
24
25. COE Unit 1, Lesson 2
2.6.2 du – Disk usage
This command is used to find out how much disk space is been occupied at
present by the files and directories of the user.
2.6.3 df – Disk free
The df command tells how much disk space is left which can be used.
2.6.4 quota
This command is used for knowing as to how much disk space the files are
occupying on the file system.
2.6.5 who – Finding out who is logged on
The who command displays the information like the usernames, terminal IDs
and process IDs of other users and processes running on the computer.
General syntax of the command is:
who [-q] [am i]
Following example shows the output of who command.
bash> who
singhs :0 May 28 14:05
achint pts/0 May 28 14:06 (lx-ptiwari:0.0)
anmol pts/1 May 28 14:12 (lx-ptiwari:0.0)
Self-Check Questions
1. The commands below are used to connect to the remote computers:
i. telnet
ii. rlogin
iii. rm
2. It is not possible to logon to another machine with another username by any
means. (True/False)
3. If some files are needed to be transferred from a remote location to the current
location, we can use the ________________ command for this purpose.
4. If a user needs to know the usage of the write command, he can use the
____________ command to know how the command works.
5. There is a restriction on the usage of the disk space by a user or a group on the
UNIX system and this disk space restriction can be found by using the command
_____________.
6. To know as to how much total disk space your files and directories have taken,
issue __________ command.
25
26. COE Unit 1, Lesson 2
7. On a multi-user system, there are more than one people logged onto a machine
and this sometimes chokes that machine off. To get in information as to who all
are logged onto the machine we can use ______________ command.
2.7 Printing
UNIX provides commands that for printing documents. Additionally, it is
possible to control the printer queue and also to kill the processes if required
to cancel the printing job.
2.7.1 lpr – Printing
This command can be used to print some text in a file. This is used to specify
a printer otherwise it issues a print job to the default printer set by the user.
2.7.2 lprm – Removing a printing job
The lprm command can be used to cancel the print jobs that have been
queued or printing. It can be used to cancel printing jobs on the specified
printer or to cancel the job on the default printer.
2.7.3 lpq – Checking the printing queue
This command shows the printer queue status on the named printer. Jobs
queued on the default destination will be shown if no printer or class is
specified on the command-line.
2.8 Process Control
When you run a program in UNIX, the program‘s copy starts to run. This
running program copy is called a process. The concept of process is
fundamental to UNIX OS. So, you should find out and understand details
about processes. If you run the same commands twice, each time a new
process is started.
Every process is identified by a unique process ID and this ID can be used to
refer to this process or to perform any further operations on the process, like
killing the process. We will have a look at the commands which can be used
to control the processes.
2.8.1 ps – Finding the process
This command is used to list all the processes being run on the machine.
bash> ps –ef
PID PPID User Process …
233 230 achint ls –l
345 342 anmol ps –ef
26
27. COE Unit 1, Lesson 2
2.8.2 & - Running process in background
By put ‗&‘ at the end of any command, that command runs in the background.
Time consuming commands can be put into background so that you can
continue working on the same terminal.
2.8.3 Cntrl-z – Suspending a processes
If some command is by mistake issued and you want to suspend this
command and do something else first. Then you can use Cntrl-z to suspend
this process and get the CPU free for some other more important work.
2.8.4 Jobs – Finding the process in background
To find the processes running in the background you can use the jobs
command. This is different from the ps command.
2.8.5 Kill – Killing a process
If some process is running for long time or is producing some unwanted
results you can use the ‗kill‘ command to kill the process.
The syntax of command is
Kill [-signal] [process id]
Sometimes a process may still not get killed and you still want to kill it, you
can send the -9 signal to kill it.
2.8.6 nice – reducing the priority of process
This command can be used to reduce the priority of a command and let other
commands run earlier than the command.
The syntax of command is
nice command [command option]
Self-Check Questions
8. If a print job is fired it is not possible to abort the printing. (True/False)
9. To know as to what all are the print processes that are at the printer in queue, we
can use ____________ command.
10. To print some text in a file, use ______________ command.
11. To change the priority of a job we can use the _________ command.
12. If some process is fired which is not required at the moment and we need to fire
another process, then we suspend the process using _______________
command and continue with the process later on.
27
28. COE Unit 1, Lesson 2
13. If it is required to know the processes running on to the system then we will issue
______________ command.
2.9 Miscellaneous commands
Besides the other commands that we have discussed in this lesson by now,
there are numerous other commands in UNIX with lots of options which can
be used to perform some amazing tasks. We will be discussing some of these
commands with useful and common options that are used. For other options
readers can refer the man pages of these commands.
2.9.1 alias / unalias command
To create or remove an alias for some command these commands are used.
The example shows the use
bash> alias rm ―rm –i‖
Creates an alias rm which calls rm –i
bash> unalias rm
Now rm will call rm command
2.9.2 cal (calendar) command
This command displays the calendar.
2.9.3 clear command
This command clears the screen
2.9.4 crontab command
It is sometimes required to run some commands at a specific date and time.
For this purpose ‗crontab‘ command can be used. See man crontab for see
details. The cron (see man cron) maintains a file which is managed using the
crontab command. This file contains the information about the command and
the time and date of the execution of the command. Here is an example:
bash> crontab –l
0 0 * * 5 echo ―This is a cron‖ | mail john
Contents of crontab file.
2.9.5 csh command
This command is used to run the C shell or to execute a C shell script.
The syntax for this command is
csh [filename]
28
29. COE Unit 1, Lesson 2
2.9.6 history command
This command is used to list the commands that you have typed so far.
2.9.7 date command
This command prints the system date and time. The date command has many
formatting arguments. See man date for details.
bash> date
Friday 25 Jan 2008
2.9.8 echo command
This command echoes back string given to it.
bash> echo ―My name is achint‖
My name is achint
2.9.9 grep command
This command is used to search a pattern in a file. We will see more details
on grep command in subsequent chapters. Here is a simple example.
bash> grep goto file.c
/*You should not use goto in c programming */
2.9.10 unset command
The unset commands removes a shell variable.
2.9.11 tar command
This command is used to create an archive of files or to extract files from an
existing archive. See man tar for details.
2.9.12 tee command
This command copies text from a pipe into a file. See man tee for details.
2.9.13 touch command
This command changes the date and time of a file without changing the files
content. The touch command creates a file if no t exiting.
29
30. COE Unit 1, Lesson 2
Self-Check Questions
14. An ____________ is a short command or word that points at some path, or
absolute command name.
15. To change the date and time stamp on a file without reading the file __________
command can be used.
16. To get the text from a pipe into a file ______ command can be used.
2.10 Summing Up
UNIX provides a rich set of commands for file management, printing, process
control, etc.
2.11 Answers to the self-check questions
1. telnet, rlogin.
2. False.
3. ftp
4. man.
5. quota
6. du.
7. Who
8. False
9. lpq
10. enscript
11. nice
12. cntrl-Z
13. ps
14. alias
15. touch
16. tee
2.12 Terminal Questions
1. Define and explain the various command classes
2. How is communication handled in UNIX? What is FTP?
3. Describe how File Management is implemented in UNIX
4. List the commands and their usage for various commands used in process
control
5. Explain the various print commands in UNIX
30
31. COE Unit 1, Lesson 3
LESSON 3 UNIX FILE S YSTEMS
3. UNIX FILE SYSTEM ....................................................................................................... 33
3.0 OBJECTIVES ............................................................................................................ 33
3.1 INTRODUCTION ........................................................................................................ 33
3.2 FILES ....................................................................................................................... 33
3.2.1 Filenames .......................................................................................................... 33
3.2.2 Filename Extensions ....................................................................................... 34
3.3 DIRECTORIES .......................................................................................................... 34
3.4 FILE TYPE................................................................................................................ 34
3.4.1 Links ................................................................................................................... 35
3.4.2 Special Files...................................................................................................... 35
3.5 PATH TO A FILE ........................................................................................................ 36
3.5.1 The root directory ............................................................................................. 36
3.5.2 Absolute Path.................................................................................................... 36
3.5.3 Relative Path..................................................................................................... 36
3.6 MANIPULATING FILES .............................................................................................. 36
3.6.1 Moving and Renaming Files and Directories ............................................... 36
3.6.2 Copying files and directories .......................................................................... 36
3.6.3 Removing Files and Directories ..................................................................... 37
3.6.4 Creating a directory.......................................................................................... 37
3.6.5 Listing the files .................................................................................................. 37
3.7 FILE PERMISSIONS .................................................................................................. 38
3.7.1 File Permissions ............................................................................................... 38
3.7.2 Permissions for directories ............................................................................. 39
3.7.3 Changing the permissions on the file ............................................................ 39
3.8 CHANGING FILE OWNER AND GROUP .................................................................... 40
3.9 FILE SEARCH........................................................................................................... 40
3.10 VIEWING BEGINNING AND END OF A FILE................................................................ 40
3.11 ANSWERS TO THE SELF CHECK QUESTIONS ........................................................... 41
3.12 TERMINAL QUESTIONS............................................................................................. 42
3.13 SUGGESTED READING MATERIAL........................................................................... 42
32.
33. COE Unit 1, Lesson 3
3. UNIX File System
In the UNIX operating system the basic storage block is known as a file. This lesson
focuses at understanding the concepts of file manipulation and handling.
3.0 Objectives
After going through this lesson, you will be able to
Understand the basic concepts of files and directories
Understand the paths and pathnames in UNIX systems
Understand the UNIX file types
Understand the basic UNIX commands related to the file system
Understand the file manipulation and file security
3.1 Introduction
In a UNIX operating system the basic structure that stores data is known as a
file. You can store data of any format in a file. Multiple files can be put
together in a directory. Apart from containing files, a directory can contain
other directories as well. A directory that is inside another directory is called a
subdirectory.
A file is analogous to a notebook. A directory is analogous to a bag that
contains files.
3.2 Files
A file contains a sequence of bytes stored on a storage device, such as a
disk. On the disk the file is not necessarily stored on a single sector but can
be scattered on the disk The OS, keeps track of the information that belongs
to a specific sequence of data.
3.2.1 Filenames
Each file has a name. Any name can be given to a file. The name of a file can
be changed anytime. Unlike windows, UNIX file names do not contain spaces.
An important thing to remember here is UNIX is case sensitive. Which means
‗A‘ is different than ‗a‘, so one should be very careful while using the cases for
separating the file names. So, myfile.txt and myFile.txt are different files.
33
34. COE Unit 1, Lesson 3
3.2.2 Filename Extensions
UNIX does not enforce any specific extensions on file names. This is unlike
Windows where extensions are used to invoke applications directly.
In UNIX you can choose any extension for your files. Even multiple extensions
are permitted (e.g.,data,tar.gz). Also files need not always have extensions
(e.g., myFileOf24Dec2007).
Since it is possible to not give extensions, one can create files where
extensions are misleading. For example, myProg.db may be a C program
while myData.cpp may be containing simple text data. Obviously this is not
desirable and one must be careful in putting proper extensions.
Though UNIX itself does not enforce any extensions, there are many
important utilities/programs that expect a specific file exte nsion. For example,
the C compiler expects files with .c or .h extensions.
3.3 Directories
Files are kept in directories. Directories are the groups of files in some logical
structure totally dependent on the application and the user requirements. A
directory can contain files and other subdirectories.
The figure below shows how the directory myData contains subdirectories
which in turn contains the files.
myDat
a/
Investmen Official
ts/ /
RBI ICI Sal custo Reports
Bonds CI es mers
pla
n
Each directory in UNIX contains two special subdirectories:
./ (The dot directory) This indicates the current directory itself.
../ (The dot dot directory) indicates the parent directory of current directory.
bash> pwd
Investments Shows current directory as Investments/
bash>cd ..
bash>pwd
myData Current directory after cd .. is myData/ (the parent)
3.4
My name is achint
34
35. COE Unit 1, Lesson 3
File Type
Regardless of the data contained in a file, UNIX associates a file type for each
file. There are 4 file types - ordinary files, directories, links and special files.
Ordinary file is any file that you commonly use. These include text files,
executable programs, shell scripts, etc. Also, we have already see what are
directories. Lets now see links and special files.
3.4.1 Links
A link is not a file but it is a second name to a file. Sometimes linking files is a
good option over copying because once copied, the copies can be changed
differently. On the other hand if you create a link then there is actually only
one copy of the file. A link is created using the ln command of UNIX. There
two types of links, soft link and hard link. See man ln for more details.
3.4.2 Special Files
UNIX represents even devices with files. These files are special files. For
example, the audio output is typically /dev/audio file. What can you do with
such a special file? Well, you can write into it or read from a special file and
UNIX hides the details on how it is actually working with the device. For
example, you can simply cat a music file to /dev/audio and it will be played!
Self-Check Questions
1. IT is possible to have multiple filename extensions in a file in UNIX. (True/False)
2. It is required to have a filename extension in a file in UNIX, which signifies the
properties of that file. (True/False)
3. Filename work and Work points to the same file in a UNIX file system.
(True/False)
4. Directories acts as a categorization structure of the data in a UNIX file system.
(True/False)
5. __________________ is a directory under the parent directory, which can be
used for the categorization of data further down the hierarchical file structure.
6. Which is not a UNIX file type?
a) Links
b) Symbolic Links
c) Program files
d) Directories
7. A ______________ (soft/hard) is only a te xt file that points to some other file
somewhere in the file system and does not contains the data.
35
36. COE Unit 1, Lesson 3
3.5 Path to a file
3.5.1 The root directory
UNIX OS treats the directory / as the root directory. The root directory is the
ultimate parent of all other directories on a UNIX system.
3.5.2 Absolute Path
Every file on a system has a path that starts from the root.
For example,
bash> pwd
/dtu/IT_Courses/IT_101/schedules.txt
This is the absolute path to the ―schedules‖ file
.
The pwd command always lists the absolute path.
3.5.3 Relative Path
When in a directory, if you know the relative position of a file, you need not
access that file using absolute path. You can simply use the relative path to
the desired file as well. This is shown in an example below:
You can also access files using relative paths. For example,
bash> pwd This is the relative path of
/dtu/It_Courses/IT_999 ―schedules.txt‖ with respect to
―/dtu/It_Courses/IT_999‖
bash> ls ../IT-102/schedule.txt
3.6 Manipulating Files
The file manipulation operations are – file deletion, file renaming and moving
files from one location to another.
3.6.1 Moving and Renaming Files and Directories
The mv command of UNIX moves files and directories to specified locations.
bash> mv –i data data.old Moves data to data.old
bash> mv –i data new
bash> mv –i oldDir newDir
Moves data into new/ directory
Moves oldDir to newDir
3.6.2 Copying files and directories
36
37. COE Unit 1, Lesson 3
The cp command of UNIX copies files and directories..
bash> cp old new Copies file old to new. Overwrites new if exists.
bash> cp –R /home/joe/bread /home/jam/food
Copies all files and subdirectories to the target
directory
3.6.3 Removing Files and Directories
Often you want to files or some directory (including its contents). For example
you may be cleaning your system. The rm command deletes files and
directories.
bash> rm file.txt my.txt Removes specified
files.
-f option indicates that rm will not give
bash> rm –f file.txt
error even if file given to be deleted
does not exist.
bash> rm –r directory1
-r option indicates delete all subdirectories as well.
Be careful with rm command. A file or directory once deleted cannot e
undeleted in UNIX. There is no such thing as trash can in UNIX. It is advisable
to use the –i option of rm command all the time. See man rm for details.
If a directory is empty, then it can be deleted using rmdir command. See man
rmdir for details.
3.6.4 Creating a directory
The mkdir command creates a new directory.
bash> mkdir project Will create directory project/
bash> mkdir /home/anmol/data
bash> mkdir ../../myDir Absolute path can be given to create a dir
Relative path can be given
3.6.5 Listing the files
The ls command of UNIX lists files and directories in the current directory. lt
has a large number of other options (see man ls).
37
38. COE Unit 1, Lesson 3
bash> ls -l achint is the file owner.
drwxr--r-- 1 achint editors 4096 drafts editors is the group. Size is
-rw-r--r-- 1 achint editors 30405 edition-32 8460 bytes
-r-xr-xr-x 1 achint editors 8460 final_draft
This field explains file permissions and file
type the fields are explained in table below
Self-Check Questions
8. The __________________ is the parent directory of all types of directories in the
UNIX file system.
9. The name of file starting from the root directory is called the _____________
pathname of the file.
10. The relative pathname of a file is the name of the file with respect to the parent
directory. (True/False)
11. Pick the odd one out
Following operations can be performed on the file system
a) Building
b) Listing
c) Renaming filenames
d) Copying
12. On using the ‗mv‘ command from one file to an existing file it ___________
(appends/overwrites) the contents of the moved file onto existing file.
13. To copy one directory to the other it is mandatory to use the option _______ with
the command ‗cp‘.
14. Command ‗rmdir‘ can be used to delete the complete hierarchical directory
structure. (True/False)
3.7 File Permissions
UNIX enforces permissions for files and directories. If you are the owner of a
file, you can put permissions whether the file should be readable by others or
not, and so on. Lets see more details about file permissions.
3.7.1 File Permissions
The user of the UNIX file system can belong to three classes:
The owner of the file
The group which the file belongs to
Other users
38
39. COE Unit 1, Lesson 3
bash> ls -l
drwxr--r-- 1 achint editors 4096 drafts
-rw-r--r-- 1 achint editors 30405 edition-32 These 3 indicate
-rwxr-xr-- 1 achint editors 8460 final_draft group people can
read/execute but
cannot write into
-rwxr-xr-- this file
These 3 indicates
First letter:
others can only
- means read this file.
ordinary file
d means These 3 letters
indicates file
directory
readable, writable
l means its a
and can be executed
link
by the owner.
3.7.2 Permissions for directories
For the directories read permissions enables the user to list the contents of
the directory; Write permissions allows the users to create a file or a directory
inside that directory and execute permissions allows to change the present
working directory to that directory.
3.7.3 Changing the permissions on the file
The chmod command changes the permissions for a file and directory. See
man chmod for details. There are several ways to change the permissions of
a file. Here are few examples:
bash>chmod ug+r w sample Permits user and group to read and write
bash> ls -ld sample in file
drw-rw---- 2 achint editor 96 Dec 8 12:53 sample
bash> chmod a-rwx sample Removes permissions for all
bash> ls -l sample
---------- 2 amol editor 96 Dec 8 12:53 sample
There is another form in which the permissions can be directly set for the files
by using an octal code. With three-digit octal notation, each numeral
represents a different component of the permission set: user class, group
class, and "others" class respectively.
For example, the number 764 in octal can be represented as following in
binary 111110100.
39
40. COE Unit 1, Lesson 3
The first octal digit when converted to binary represents the permissions for
owner (7 in octal is 111 in binary which implies rwx for owner).
The next octal digit when converted to binary represents the permissions for
the group (6 in octal is 110 in binary which implies rw- for group).
The last octal digit when converted to binary represents the permissions for
the others (4 in octal is 100 in binary which implies r-- for other).
3.8 Changing File Owner and Group
The chown command changes the owner of a file. See man chown for details.
The chgrp command changes the group of a file. See man chgrp for details.
3.9 File Search
The find command helps in locating files and directories. This is a powerful
command and has lots of options. See man find for details. Here is the syntax
of the find command.
find search_directory –name file_name [-print]
The find command searches through the contents of one or more directories
including all of their subdirectories.
bash> find / -name schedule -print
/dtu/IT_courses/IT_101/schedule Finds all the files in ‗/‘ named
/dtu/IT_courses/IT_102/schedule schedule
Another example in which same file name is searched in two directories:
bash> find . –type d –name abc -print
Finds ‗directory‘ abc and not file in the present directory
.
3.10 Viewing Beginning and End of a file
UNIX provides commands using which it is possible to display the contents of
the start or end of the file. These are head and tail commands.
head – Start of the file
tail – end of the file
40
41. COE Unit 1, Lesson 3
Example usage
bash> head –n 10 file
Shows the 10 starting lines of ‗file‘
Self-Check Questions
15. Pick the odd one out
The users in a UNIX file system can be categorized as:
a) Owners
b) Group
c) Friends
d) Other users
16. To change the file permissions from one set to another, the command
___________ can be used.
17. __________________ command is used to change the owner and the group of
the file.
18. The _______ command lets you search for files and directories.
19. The _______ command will be useful to show the last few lines of a file.
3.11 Answers to the self check questions
1. True
2. False
3. False
4. True
5. Subdirectory
6. Program files.
7. Soft link
8. Root.
9. Absolute path..
10. True
11. Building
12. overwrites.
13. –r
14. False
15. Friends
16. Chmod
17. Chown, chgrp
18. Find
19. tail
41
42. COE Unit 1, Lesson 3
3.12 Terminal questions
1. Write a detailed note about the hierarchical file structure.
2. Explain briefly the manipulating operations possible on the file structure
3. Write a brief note on the permissions on the files and directories in UNIX.
Also, explain how we can change permissions of the files in UNIX using the
chmod command. Use some relevant examples to explain the concepts.
4. Explain the UNIX system file types, also explain the salient features of each
file type
3.13 Suggested Reading Material
1. Unix Programming Environment, by Kernighan and Pike.
2. Design of Unix Operating System, by Maurice J. Bach
42
43. COE Unit 1, Lesson 4
LESSON 4 T HE VI T EXT EDITOR
4. THE VI TEXT EDITOR.................................................................................................... 45
4.0 OBJECTIVES ............................................................................................................ 45
4.1 INTRODUCTION ........................................................................................................ 45
4.2 FILES CONTAIN STREAM OF CHARACTERS .............................................................. 45
4.3 HOW VI HANDLES THE FILES ................................................................................. 46
4.4 INVOKING VI ............................................................................................................. 46
4.5 MODES OF VI ........................................................................................................... 46
4.5.1 Command mode ............................................................................................... 46
4.5.2 Edit mode........................................................................................................... 46
4.5.3 Switching between command mode and edit mode................................... 47
4.6 POSITIONING TE XT ON THE SCREEN ...................................................................... 47
4.6.1 Scrolling and moving the Screen ................................................................... 47
4.6.2 The GOTO Command ..................................................................................... 48
4.6.3 Searching........................................................................................................... 48
4.7 POSITIONING THE C URSOR : H, L, J, K COMMANDS................................................. 48
4.8 EDITING USING SCOPES .......................................................................................... 49
4.8.1 Delete Text (d, D) ............................................................................................. 50
4.8.2 Change Text (c, C) ........................................................................................... 50
4.8.3 Replace Command (r, R) ................................................................................ 50
4.8.4 Erase Command (x, X) .................................................................................... 51
4.8.5 Undo Command (u, U) .................................................................................... 51
4.9 TE XT INSERTION ...................................................................................................... 51
4.9.1 Append Command (a, A) ................................................................................ 51
4.9.2 Insert Command (i, I) ....................................................................................... 52
4.9.3 Open Command (o, O) .................................................................................... 52
4.9.4 Read Command (:r) ......................................................................................... 52
4.10 GLOBAL SEARCH AND REPLACE FOR TEXT ............................................................ 52
4.11 REARRANGING AND DUPLICATING TEXT................................................................. 53
4.11.1 Copying Text and Moving the Copy .............................................................. 53
4.11.2 Deleting Text and Moving It ............................................................................ 54
44. COE Unit 1, Lesson 4
4.12 NAMED BUFFERS .................................................................................................... 54
4.12.1 Using the named buffers ................................................................................. 55
4.13 MISCELLANEOUS INFORMATION .............................................................................. 56
4.13.1 Creating Line Numbers ................................................................................... 56
4.13.2 Lines and Sentences in VI .............................................................................. 56
4.13.3 Joining Lines ..................................................................................................... 57
4.13.4 Repeating a Command ................................................................................... 57
4.13.5 Editing Multiple Files Using vi......................................................................... 57
4.13.6 Mark Command ................................................................................................ 58
4.14 SAVING OR STORING A FILE.................................................................................... 58
4.14.1 Writing to the file ............................................................................................... 59
4.14.2 Exiting the vi editor ........................................................................................... 59
4.15 SUMMING UP........................................................................................................... 60
4.16 ANSWERS TO THE SELF-CHECK QUESTIONS ........................................................... 60
4.17 TERMINAL QUESTIONS ............................................................................................ 61
45. COE Unit 1, Lesson 4
4. The VI Text Editor
When you write programs, scripts or modify data, write mails, etc., you will need to
use text editor. This lesson focuses on the VI text editor; one of the most commonly
used text editors in UNIX systems.
4.0 Objectives
After going through this lesson, you will be able to
Understand how to open and edit files using vi
Understand various text insertion and deletion methods in vi
Understand the basic structure of vi text editor
Understand the commands to edit text using vi and scopes
Understand miscellaneous other features of vi
4.1 Introduction
vi is a visual, non-graphical and interactive text editor which allows a user to
create, modify, and store files on the computer.
Note that in this chapter, the cursor is shown by putting an underscore for a
character. For example: The cursor is at the letter ‗n‘ in the following line.
This is a line.
There's an editor out there that programmers have been using to edit their
programs for the last 24 years. It's called vi (say vee-eye) and it is it is quite
powerful.
http://www.websiterepairguy.com/articles/vi/12_learn_vi.html
4.2 Files contain stream of characters
When you type characters or numbers, etc. each key goes as an ASCII
character. For example, ‗a‘ gets recorded as ASCII 97. When you write lines
like these
This is line 1
This is line 2
These lines are stored as a stream of characters like ―This is line 1 nThis is
line 2‖. Here the n is a special character which signifies a new line.
45
46. COE Unit 1, Lesson 4
4.3 How Vi Handles The Files
When you open a file in vi, the file contents are read into a buffer. All text
editing jobs are done in memory as the buffer. The file on the disk is not
updated unless vi is explicitly asked to save the changes. This gives an option
to change the content of the buffer until you are not satisfied without changing
the file on the disk.
4.4 Invoking vi
The vi editor can be invoked using the following command
$ vi demo.txt
The figure below shows how the file looks when opened in vi.
The cursor
~
~ Tile(~) in vi represents an
~ empty line.
~
.
4.5 Modes of vi
. File
information
―myfile‖ [new
vi has two modes in which you will work.
file]
4.5.1 Command mode
The command mode is the default mode. All vi commands work only in the
command mode. In the command mode you cannot write text. You can only
move around in the text, delete text, modify existing text, search for text, etc.
4.5.2 Edit mode
In edit mode you can add new text in vi. In edit mode you cannot use any
commands to search or navigate in the text.
46
47. COE Unit 1, Lesson 4
4.5.3 Switching between command mode and edit mode
When in command mode, few commands take you to edit mode. For
example, in the command mode, if you press i, you will get to the edit mode
and can add text.
When in the edit mode, you can stop editing further and go to the command
mode by pressing the <Esc> key.
4.6 Positioning Text on the Screen
This is a
You are in command
line mode and cursor is at ‘a’.
press ‗i‘
This is a Cursor is at same position
line
but edit mode has started
This is da now press ‗d‘
Cursor is at letter ‗a‘ and
line
Press letter ‗d‘ is added.
‗esc‘
This is provides several ways you are in text you want to edit in a file.
vi da Now to reach the
line command mode
4.6.1 Scrolling and moving the Screen
By scrolling the screen we can reach the text desired. The table below
explains how one can scroll the screen.
Command Resulting Action
Cntrl+u Moves window upwards one complete screen
Cntrl+d Moves window downwards one complete screen
H Takes cursor to the top of the screen
L Takes the cursor to the bottom of the screen
M Takes the cursor to the middle of the screen
All these commands work only in the command mode.
47
48. COE Unit 1, Lesson 4
4.6.2 The GOTO Command
Sometimes you already know the line number where you want to reach. You
can use the GOTO in such cases. The table below explains the command and
the resulting action.
Command Resulting Action
G Moves cursor to the last line
<N>G Moves the cursor to the Nth line
Like 33G
:<N> Moves the cursor to the Nth line
Like :65
4.6.3 Searching
It is also possible to search for a pattern and by this the screen will be moved
to the occurrences of the desired pattern.
Here are the commands that work for search in vi..
Command Resulting Action
‗/pattern‘ Searches the pattern forward from current
cursor position
‗?pattern‘ Searches the pattern backward from current
cursor position
:set ic This makes the subsequent searches case
insensitive (ic in set ic stands for ignore case)
:set noic This makes the subsequent searches case
sensitive
Once you start a search you can repeat the search in a simple way. On
keying in ‗n‘ vi goes to the next instance of pattern in the file and using ‗N‘ it
searches in opposite direction.
4.7 Positioning the Cursor : h, l, j, k commands
This section explains finer control of the cursor.
You can move the cursor by use of "arrow" keys. You can also use the
"direction" keys "h" (move left by one character), "j" (move down to next lined),
"k" (move up to previous line), and "l" (move right by one character).
The "RETURN" key is similar to the "j" key in that it moves the cursor down
one line. However, the "RETURN" key always positions the cursor at the
beginning of the next line; whereas, the "j" key moves the cursor straight down
from its present position, which may be the middle of a line. Moving several
spaces may be accomplished by repeatedly pressing the "RETURN", direction
48
49. COE Unit 1, Lesson 4
or arrow key; such as, "k" "k" "k" to move upward 3 lines. You can also
precede any of these keys with a number and achieve the same results, "3k".
Self-Check Questions
1. If in a file cursor is resting at the 34 line and it is desired to be placed onto the 74
line then the command that is to be issued is _____________G.
2. On searching with ―?‖ and ―/‖, the search respectively will be done
______________ and ____________________. (backwards/forward).
3. To get the file statistics using the VI editor the command required to be issued is
___________.
4. On keying in ―N‖ while searching for a pattern using ―?‖ the cursor will reach the
next instance of the pattern ________________. (backward/forward)
5. To move to the 25 word in the line while the cursor is on 18 line the command
that can be issued is ___________.
6. To move to the beginning of the line on which the cursor is residing in a text file
the command that can be issued is __________.
7. The vi editor sets or creates a temporary buffer area while editing a file which is
stored on the disk and is used later on for the reference purpose by the editor.
(True/False)
4.8 Editing using scopes
vi commands have scope built into them. For example, when you say ‗dd‘
then first ‗d‘ indicates the delete operations and the second ‗d‘ tells it to apply
the command on a line. Similarly, ‗yy‘ yanks a line. But the commands like ‗d‘
and ‗y‘ can be given a scope and VI commands also have upper case
versions.
Scope Text Unit Encompassed
0 Beginning of line
$ End of line
W w Word right
B b Word left
E e End of word right
With the scopes we can use the operators to get more powerful outcomes.
We can further do editing very much locally using the combination of the
operators and scopes. In this section we will discuss this combination.
49
50. COE Unit 1, Lesson 4
4.8.1 Delete Text (d, D)
The delete command is used in command mode to remove portions of text
from the file being edited. The scope must be specified after the delete
operator. Some of the most common scopes used with the delete operator
shown in the next table.
Delete Resulting Action
operator
and
scope
dw Delete word forward
D( Delete complete sentence backward
d) Delete complete sentence forward
dG Delete from current line to end of file
dL Delete from current line to end of screen
d/^xyz Delete from current line to first occurrence of
pattern
dtx Delete from current place to first occurrence of ‗x‘
NOTE: The same scope prefixes can be used with all the scoped text editing
commands so we will not discuss them with any further commands b ut
different scopes or operators, if any will be discussed.
NOTE: It is important to remember that the current cursor position serves as
the starting point for the scope. This means if you do scoped deletion, it will
happen starting from the current point. For example, typing "2dd" will delete
two consecutive lines beginning with the current line.
4.8.2 Change Text (c, C)
You can use the change command to change the text in a line. Scopes are
applied in the same manner as they are used with the delete command.
On issuing the change text command, vi gets into the edit mode and after the
text insertion on issuing the <ESC> key it returns to the command mode. The
example shows how change command can be used.
This is the line to watch
Cursor is positioned
at‗t‘
On issuing the command
‗2cw‘ or change two words
and keying in ―new line‖
Text inserted in place of
4.8.3 Replace Command (r, R)
This is new line to watch two words
The replace command is used to replace portions of text on the screen. The
table shows the two variants of the replace command and their usage for
replacing text.
50
51. COE Unit 1, Lesson 4
Replace Text replacing action
command
r Used to replace a single character at a time
R Used to replace as many characters as
there are keystroke until user issue <ESC>
This is the line to watch out for.
Cursor positioned at ‗l‘
On issuing ‗r‘ command and
typing ‗m‘
‘l‘ is replaced by ‗m‘
This is the mine to watch out for.
4.8.4 Erase issuing ‗R‘ command,
On Command (x, X)
keying in ―kite‖ and <ESC> Complete word is
The erase command removes a character.
replaced
This is the kite to watch out for.
Erase Erase Action
Command
x Erase character on which cursor is
placed
X Erase character left to cursor
4.8.5 Undo Command (u, U)
Undo command reverses the effect of the editing operations done on a file.
‗u‘ reverses the effect of last editing command whereas ‗U‘ reverses the effect
of all the editing operations on the file since last save.
4.9 Text Insertion
vi editor provides several ways to insert the text in the file. We will be
discussing each of these methods in some detail but it is advisable for a newly
inducted candidate to take up one approach and use that to insert the text.
4.9.1 Append Command (a, A)
It is used to add to the existing text. It has two forms ‗a‘ and ‗A‘. These two
forms are explained in the figure below.
The student laughed.
On issuing ‗a‘ command and typing ‗s‘ and <ESC>
The students laughed.
Text appended after the cursor
51
The students laughed. Aloud.
52. COE Unit 1, Lesson 4
4.9.2 Insert Command (i, I)
This command is used to insert the text into a text file. This command has two
forms ‗i‘ and ‗I‘. In the figure below it is explained how to use this command.
The student laughed.
On issuing ‗i‘ command and typing ‗new
‘and <ESC>
The new student laughed. Text inserted before the
cursor
On issuing ‗I‘ command and typing appended at end of line.
Text
‗Again‘and<ESC>
Text appended in the
Again The student laughed.
beginning of line.
4.9.3 Open Command (o, O)
Open command opens a new line to add text. This has two forms ‗o‘ and ‗O‘,
in the figure below the usage is explained.
The student laughed.
On issuing ‘O’ command and typing ‘A new line is added’ and
ESC>
A new line is added
The student laughed. Text inserted above the current line
On issuing ‘o’ command and typing ‘Another line ’ and <ESC>
A new line added
The student laughed.
Another line Text appended in the beginning of the line.
4.9.4 Read Command (:r)
The read command is allows the user to copy of another file into the current
file. While in command mode and with the cursor on the line above where you
want the special file read in, type:
:r <File> Reads the file specified at cursor location in the current file
4.10 Global Search and Replace for text
52
53. COE Unit 1, Lesson 4
The example below shows different commands that can be used for searching
and replacing with different purpose.
:1,$s/oldText/newText/g
This command replaces all the
instances of oldText with
:1,15s/oldText/newText/g
newText in the file
This command replaces
:g/oldText/s//newText/gc
oldText with newText from line
number 1 to 15
This command asks before replacing text
each time
Self-Check Questions
8. To delete the word on which the cursor is placed ―D‖ command can be issued.
(True/False)
9. The change operator invokes the text insertion mode. (True/False).
10. The operator _______________ changes the text, yet does that in command
mode and not in text insertion mode.
11. The command ______________ replaces the characters on screen one at a time
as the user keys in the new characters.
12. To erase the character on which the cursor is place __________ command is to
be issued, whereas to delete the character prior to the character (left) on which
the cursor is placed _________ command needs to be issued.
13. To replace the name ―shahs‖ with ―mazes‖ in a text file the command to be issued
is ___________.
4.11 Rearranging and Duplicating Text
You can yank text for copying it at another place in the text file.
4.11.1 Copying Text and Moving the Copy
Step 1: Copying Text with the Yank Command (y, Y)
The yank command ‗y‘ can be used with the scopes and similar scopes can
be used as we have seen in delete command. Yanking places the yanked
content into an unnamed buffer. Some of the examples of yanking are:
This is the line to be yanked .
cursor is character ‗l‘
On issuing the
command
‗3yw‘ which means yank
3 words, it yanks 3
words starting from
current cursor position cursor is at first line
This is the line to be yanked
This is another line ‗3yy‘ will
Issuing command to yank
This is yet another line that can be yanked
yank 3 lines starting from 53
current line