Top Banner
1 Unix Introduction to Unix Operating System. Welcome to this course on Unix Operating System.
62

Unit1_Unix_OS31.pdf

Apr 14, 2018

Download

Documents

Santanu Das
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 1/62

1

UnixIntroduction to Unix Operating System.

Welcome to this course on Unix Operating System.

Page 2: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 2/62

2

2ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Course Objective

To introduce the Unix Operating System.

To develop an ability to use general Unix commands.

To develop ability to write programs in shell.

To develop ability in using Unix utilities.

To introduce the security issues in Unix.

By the end of the course, you will have

• Knowledge of Unix Operating System.

• Knowledge about the different Unix commands.

• Knowledge about writing programs in shell.

• Knowledge about the different Unix utilities used in Software Development.

• Knowledge about the different security issues in Unix.

NOTE:

This course does not cover

• Differences in command behavior in various Unix flavors.

• Introduction/Implementation of System Calls.

• System Administration in Unix.

• Hacking/tricky issues in using Unix commands.

• Awk and sed programming.

Page 3: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 3/62

3

3ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

References

Brain W. Kernighan and Rob Pike, The UNIX Programming Environment .

Stephen Prata, Advanced UNIX - A Programmer’s Guide .

Stephen G. Kochan and Patrick H. Wood , Exploring the Unix System .

James R. Groff, Paul N. Weinberg ,Understanding Unix - A conceptual guide.

Maurice J. Bach, The Design of the Unix Operating System 

Page 4: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 4/62

4

4ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Session Plan

Day 1

 – Introduction to Unix.

 – Unix System Architecture.

 – Processes in Unix.

 – Basic Unix Commands and Utilities.

 – Regular Expressions.

 – The vi editor.

 – Text Processing Commands.

•Day 1

•Introduction to Unix.

•To understand

•Need for Unix in today’s world.

•Chronology of events that led to the development of Unix.

•Different flavours of Unix.

•Features of Unix.•Unix System Architecture.

•To understand

•Major Components of Unix.

•How Unix Works.

•Unix File System.

•Directory Organization.

•Processes in Unix.

•To understand the term Process in Unix and the various commandsassociated with it.

•Basic Unix Commands and Utilities.

•To understand Unix commands and utilities required for basic operations inUnix.

•Regular Expressions.

•To understand and match expressions viz wildcards, etc.Day 2

•The vi editor.

•To understand

•Types of Editors.

•Different modes.

•Cursor Movement Commands.

•Text Processing Commands.

•To understand the concept and usage of pipes and redirection inUnix.

Page 5: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 5/62

5

5ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Session Plan

Day 2

 – Types of Shell.

 – Shell Programming.

 – File Handling Commands.

•Day 2

•Types of Shells in Unix.

•To understand the different types of shells and their features.

•Shell Programming.

•To develop an ability for writing shell scripts.

•File Handling Commands.

• To understand the commands associated with files . viz. file, touch,etc .

Page 6: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 6/62

6

6ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Session Plan

Day 3

 – Unix Utilities.

 – Unix Program Development Tools.

 – Unix Communication.

 – Security Features

•Day 3

•Unix Utilities.

•To understand the different types of utilities like compression, tar, etc.

•Unix Program Development Tools.

•To understand tools like cc, make, SCCS, etc

•Unix Communication.

•To understand commands for communication: write, wall, ftp, telnet,etc..

•Security Features.

•To understand Security features: Permissions, Passwords, etc.

Page 7: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 7/62

7

7ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Introduction to Unix.

What is Unix?

Need for Unix in today’s world?

Evolution of Unix.

Flavors of Unix.

Unix is a very popular multi-user, multitasking, time-sharing operating system.

Unix has become the operating system of choice for various engineering and scientific applications. Theneed for Unix could be determined by the different categories of application it suffices. viz. networking,programming, multimedia, high-performance computing to name a few.

The chronology of events that led to the evolution of Unix are as follows:

1965 : AT&T, GE, IBM and Project MAC join together to develop a time-sharing system named MULTICS(Multiplexed Information and Computing Service).

1969 : AT&T Bell Labs drops out of MULTICS project. Ken Thompson writes first version of UNICS on aPDP-7 machine. UNICS is pun on MULTICS and stands for Uniplexed Information and ComputingServices. UNICS is changed to UNIX.

1973 : Re-written in high level language C, thus making it machine-independent.

1977-1981 : Unix was widely available at minimal cost and became popular for scientific applications.

1982 : Unix System III is released.

1984-85 : Unix System V is released.

1992-93 : 4.4 BSD is released.

1994 : Linux 1.0 is released.

2001 : Linux 2.4 is released.

Unix is available in many flavors like :

AIX (Advanced IBM Unix)

HP-UX ( Hewlett Packard Unix)

MINIX (Minimal Unix)

SCO UNIX

SOLARIS

XENIX

SUN OSLINUX

Page 8: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 8/62

8

8ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Features Of Unix.

The important characteristic features are

 – Portability.

 – Security.

 – Background Processing.

 – Pipes.

 – Redirection Tools.

 – Software Development Tools.

 – Stable and Reliable

Portability : As Unix has been re-written in C , hence it can run on machines frommicrocomputers to mainframe computers.

Security : Unix is a very secure operating system. Without the proper username andpasswords, it is not possible to work on Unix.

Background Processing : Unix employs a technique of Background Processing,wherein jobs/tasks are executed in the background with the minimal interaction fromthe user.

Pipes : Using the concept of pipes, a Unix user/administrator can easily link and workwith multiple commands at the same time.

Redirection Tools : These tools allow data to be re-directed between files as per therequirement of the user.

Software Development Tools : Unix supports a wide variety of tools using which theuser could create and work on different programs .viz. C Language on the UnixOperating system. Unix supports any language that has an interpreter or compiler.

Stable and Reliable : Unix is a very reliable and stable Operating System. It is lessprone to crashes.

Page 9: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 9/62

9

9ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Features Of Unix (Contd...)

 – Easy to write programs.

 – Hierarchical File System.

 – Shells.

 – Communication.

Easy to write programs : Its easy to write programs in Unix as it hides machinearchitecture from the user.

Hierarchical File System : Unix employs a hierarchical file system which is easy toimplement and maintain.

Shells : Unix has different types of shells .viz. Bourne, C, Korn, etc.

Communication : Unix has commands which allow communication between differentusers connected to the system.

Page 10: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 10/62

10

10ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

System Architecture.

Major components of Unix are :

 – Kernel

 – Shell

 – Utilities

 – User Applications

Kernel, the most important part of OS, interacts directly with the hardware providingcommon services to programs and insulating them from hardware. It is a programthat monitors and controls the resources of a computer and allocates them amongits users in an optimal manner.

The shell provides a processing environment for the user programs. It acts like acommand translator.

The shell offers the following facilities:

File name short-hands through metacharacters

Input/Output (I/O) redirection

Personalizing the environment

Defining new commands.

The most powerful feature of the shell is, it is programmable. Shell scripts, which are aseries of shell commands, can be executed using the shell.

Utilities includes programs that interact with the kernel by invoking a well-defined set ofsystem calls. Typically, these programs are Unix commands like ex, wc, grep etc.,and may include certain user-developed programs also.

User Applications are the programs which the user writes.

Page 11: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 11/62

11

11ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

System Architecture.

HARDWARE

KERNEL COMPILERS

OTHER

APPLICATIONS

DATABASEPACKAGES

UNIX

COMMANDS

SHELLSHELL

SHELLSHELL

UserUser

UserUser

UserUser

UserUser

Unix follows a layered approach.

Layer 1 : This layer is the hardware over which the different OS layers are built.Hardware is not a part of the Unix OS.

Layer 2 : This layer is the Kernel, which is the most important part of the Unix system.

It is a collection of programs which directly communicate with the hardware. It is thatpart of the Unix system that is loaded into memory when the system is booted. Itmanages the system resources, allocates time between users and the processes,decides priorities of different processes, etc.

Layer 3 : This layer contains programs that interact with the Kernel by invoking a well-defined set of system-calls. Typically, these programs are Unix commands like wc,grep, etc.

Layer 4 : This layer is called Shell. Technically another Unix command, it is theinterpreter of user requests. It takes a command from the user, deciphers it, andcommunicates with the Kernel to see that the command is executed. It is actually theinterface between the user and the kernel, which effectively insulates the user from theknowledge of kernel functions. It also has a programming capability of its own.

The kernel and shell together make the Unix system work. The shell provides aprocessing environment to the user programs.

Page 12: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 12/62

12

12ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Operating System Services.

Kernel.

Hardware Transparency.

Interrupts and Exceptions.

Processor Execution Levels.

Memory Management.

Kernel: 

The Kernel performs various primitive operations on behalf of user programs:

• Control of execution of processes - creation, synchronization, termination,suspension and communication of processes.

• Scheduling of processes - deciding of time-sharing of processes.

• Memory management - Allocation of memory for executing processes. Twomethods are used:

• Swapping : Entire process is written to swap device to accommodateanother process.

• Paging : Pages of memory is written to swap device.

• File system service - Allocation of secondary storage efficiently for user data files.

• Controlled access to peripherals, which are treated as special files.

Hardware Transparency.

Processes usually run in one of the two modes:

User mode - cannot access kernel instructions and dataKernel mode - can access kernel and user addresses and privileged instructions not

allowed for user mode.

The hardware only views processes in terms of kernel or user modes. It does notdistinguish between the user processes and the OS has internal bookkeeping todistinguish between processes.

Page 13: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 13/62

13

13ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Operating System Services (Contd...)

Please refer to the notes page below for an explanation of the earlier slide

Interrupts and Exceptions 

Interrupt : Interrupt is a signal by which priorities and instruction-scheduling sequence can be controlled.The kernel saves the current context (a frozen image of what the processes was doing) when aninterrupt occurs. After servicing the interrupt the context is restored and the process proceeds. Theorder in which an interrupt is serviced depends upon its priority.

Exception : An exception is an unexpected event like reference to an illegitimate address caused by eventsexternal to a process - exceptions usually occur in the middle of a process where as interrupts occurbetween them. Different algorithms are used to handle interrupts/exceptions efficiently.

Processor execution levels 

Based on the priority level of an interrupt the kernel masks certain interrupts in order that a critical processmay proceed without error . The priority levels in descending order are:

Machine error

Clock

Disk

Network Devices

Terminals

Software Interrupts

Memory Management 

The concept of a Virtual Machine is central to memory management under Unix. The compiler generatesaddresses for a virtual machine as if no other program will execute simultaneously on the physicalmachine. Virtual addresses generated by the compiler need not be identical to the actual physicaladdresses in the machine. The memory management coordinates with the machine hardware to set-up a virtual to physical address translation that maps the compiler-generated address to the physicalmachine address.

Page 14: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 14/62

14

14ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

The User Perspective.

Processing environment

Unix File system

Building block primitives

From the users viewpoint the following aspects of Unix will be highlighted:

• Processing environment.

• File system.

• Building block primitives.

Page 15: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 15/62

15

15ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Processing Environment.

User Program.

Process.

Shell.

A program is just an executable file whereas a process is an instance of a program inexecution.

The creation / termination of processes, the synchronization and reaction to interruptsis controlled through system calls. An example of a program using the kernelthrough system calls is the shell - the COMMAND INTERPRETER. The shell is auser program that can be tailored to the needs of the user. The shell can alsoexecute commands asynchronously i.e. without waiting for the prior command toterminate, background execution. The shell provides a processing environment forthe user programs.

Page 16: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 16/62

16

16ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Structure of Unix File System.

 / (root)

bin dev etc tmp lib usr

sh lstty lp

passwd terminfo user1

user2

home

The Unix file system follows a tree structure with root represented by ‘/’.

Every non-leaf node is a directory of files and the leaf nodes can be directories,regular files or special device files.

The main features of a Unix file system:

• Hierarchical structure

• Consistent treatment of file data

• Create and delete files

• Dynamic growth of files

• Protection of file data

• Treats peripheral devices as files

• Keeps track of files using i-node numbers.

• Information of files is kept in the i-node block.

File access permissions can be set 3 classes of users:

• File Owner

• File Owner’s Group

• Others

Read (r), Write (w), Execute (x) permissions can be set for each file.

Page 17: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 17/62

17

17ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Inodes and Linking.

0412file2

0221file1

INODE

NUM

FILENAME

A directory

#0221#0412

0981file3

0221name1

INODE NUMFILENAME

Another directory

• File1 and name1 are links

with same inode numbers

Inode

block

The information pertaining to the Unix fi les is kept in the i-node (identification node)block, which is reserved exclusively for the use of kernel. Every file in the file systemwill invariably have an entry in this area. Every file is associated with an i-nodenumber, called the i-number.

The command ls is used to list the contents of the current directory. The option –i usedwith ls would indicate the i-node number for a file.

Page 18: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 18/62

18

18ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Absolute Path and Relative Path.

The Absolute Path.

 – The entire pathname starting from root(/).

 – Example : /home/trng10/file1.

The Relative Path.

 – The path relative to your present working directory.

There are 2 ways to access any file in Unix. They are :

•Absolute Path.

•Relative Path.

In Absolute Path method, the complete path from the root to the required file ismentioned.

E.g. To access a file called file1, present in directory trng10, we need to write

 /home/trng10/file1

In Relative Path method, the path from the current position to the required file ismentioned.

.. represents parent directory.

. represents present working directory itself.

E.g. If the current directory is /home/trng10, then to go to the previous directory , we

write ..

Page 19: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 19/62

19

19ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

How Unix Works.

USER TYPES COMMAND

SHELL EXECUTES

UTILITY TO CARRY

OUT COMMAND

SHELL ASKS FOR A COMMAND

USER INTERACTS WITH

UTILITY

SHELL PROMPTS FOR

NEXT COMMAND

USER TYPES CONTROL-D

LOGOUT

LOGIN

The user connects to the Unix Server using an utility called telnet, and by mentioningthe IP address of the Unix Server.

The Server requests for the username and password. The user needs to give the validusername and password, for log-in.

Once the username and the password is right, the shell-prompt is displayed, where-in

the user could execute the commands.

Page 20: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 20/62

20

20ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Login Sequence.

Login id and password.

The /etc/passwd file.

The /etc/profile and .profile file.

Shell prompt.

Environment variables.

 – The $HOME, $PATH, $PS1, $TERM and other variables.

Connect to a machine from the classroom PC.

And show the trainees how to login .

Show the /etc/passwd file.

The .profile file contains the settings for the user to customize his shell.

Page 21: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 21/62

21

21ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

The Other System Settings.

The $PATH variable.

Aliases.

The tty, stty commands.

The [ctrl-c ] , [ctrl-d ] and other keys.

Show them the variables like $HOME, $PATH , $PS1 etc.

Show some simple aliases using alias command.

Tell them the importance of /etc/profile file.

Show them the man command.

Page 22: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 22/62

22

22ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Processes in Unix.

Understanding the term Process.

Process related commands.

 – ps command.

A Process is a program which is under execution. When any program (i.e. andexecutable file) is executed, it gives rise to a process. In other words, a process issaid to be born when the program starts execution, and remain alive as long asthe program is active. The process is said to die when the program completesexecution.

Since Unix is a multi-tasking system, multiple processes could run at the same time.

Each process is identified by a number called Process Identifier (PID), which theKernel allocates when the process is born.

Just as a file has a parent, every process also has its own creator, which itself isanother process. The process responsible for giving birth to another process iscalled the parent process, and the born process created is called child process.

The parent process is identified by a number called PPID ( Parent Process Identifier).

There are different commands associated with processes.

ps command : This command lists out the processes associated with a user at aparticular terminal.

Page 23: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 23/62

23

23ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Processes in Unix (Contd…)

Please refer to the notes page below for an explanation of the earlier slide

E.g.

$> ps

PID TTY TIME CMD

20353 pts/2 00:00:00 bash

20446 pts/2 00:00:00 ps

To see the details about the ancestry (PID and PPID) of a process, ps command hasan option –f.

E.g.

$> ps -f

UID PID PPID C STIME TTY TIME CMD

ajit 20353 20352 0 16:31 pts/2 00:00:00 -bash

ajit 20539 20353 0 16:34 pts/2 00:00:00 ps –f

Page 24: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 24/62

24

24ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Building Block Primitives.

Redirection of I/O.

Pipes.

The Unix philosophy supports building complex programs from simple primitives. Thefollowing are supported:

Redirection of I/O 

Both the standard input and the standard output can be re-directed from/into files:

$> cat prog1 > prog2 

Here the contents of prog1 file are redirected to fie prog2.

$> cat>prog1<prog2 

The contents of the file prog2 is transferred to the file prog1.

Pipes 

A pipe is a mechanism that allows a stream of data to be passed between the readand the write processes. It allows the output of one command to become input ofanother command. In other words, the standard output file of the command getsredirected to standard input file of the next command .

Example 

$> ls | wc -l Counts the number of lines in the directory display.

Note: In most situation pipes makes it unnecessary to create temporary files.

Page 25: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 25/62

25

25ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Unix Command Structure.

Unix Command line structure.

 – command [options] [arguments]

Unix is all about working on commands.

The user can execute commands or create programs containing the commands for his/herapplication.

Shell reads lines - either executes them or interprets them.

 – Each i/p line is scanned and split into tokens

 – parameters are substituted (subject to quoting)

 – filenames are generated – I/O are redirected (optional)

 – commands are executed

For executing a command, the user simply needs to write the command and press the Enterkey for its execution.

Example.

$> pwd 

 /home/ajit

Some commands have options which allow a detailed display.

Example.

$> ls -l 

ls is a command in Unix to display the contents of the current directory. Attaching an option l ,displays in detail the contents.

Some commands have arguments which allow a display, as per user requirement.

Example.

$> cal 01 2004 

cal is a command to display the calendar information. The above statement would display thecalendar for January 2004.

Here 01 and 2004 represents arguments and are not options.

Page 26: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 26/62

26

26ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

General Unix Commands.

ls - List contents of a directory.

 – ls [option] [filenames]

cat - Concatenate and display files.

 – cat [option...] [file ...]

ls (List) – List contents of a directory.

Output is sorted alphabetically by default.

Option -a list includes files starting with (.)

Option -i lists the i-node numbers.

Option -l lists in long formats - includes the mode, number of links, owner, group,size in bytes, time of last modification and name of the file.

Option -r lists the contents in reverse order.Example:

$> ls 

a.out dem2 file1 makefiles sds

asc demo1 file2 name shell1.sh

cat - Concatenate and display files.

The command cat with filename, would display the contents of the file.

The command used with sign “>” , is used for creating a file, or over-writing if it alreadyexists.

The command used with sign “>>” , is used for appending a file.

Examples.

$> cat prog1

Output : Displays the contents of the file prog1.

$> cat > prog2 

Output : Creates a file by the name prog2.

$> cat >> prog1

Output : Concatenates the contents of the file prog1.

Page 27: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 27/62

27

27ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

General Unix Commands.

cp - Copy files.

 – cp [option...] [file1] [file2]

mv - Move or rename files.

 – mv [option...] [file1] [file2]

cp - Copy files.

Copies the contents of file1 to file2. The file1 is kept intact.

If a directory is mentioned in place of file2, then file1 is copied into the mentioneddirectory.

Directory must exist before the execution of the command.

Examples

$> cp demo1.c demo.c Output : Copies the contents of file demo1.c to a file demo.c

$> cp demo1.c cprogs 

Output : Copies the contents of file demo1.c to a directory cprogs.

mv - Move or rename files.

Moves the contents of file1 to file2. The file1 is deleted.

If a directory is mentioned in place of file2, then the file1 is moved to the mentioneddirectory.

Examples

$> mv s2.c s3.c 

Output : The contents of the file s2.c is moved to file s3.c. It could also be said that thefile s2.c is renamed as s3.c, as the file s2.c no longer exists.

$> mv s2.c cprogs 

Output : The contents of the file s2.c is moved to the directory cprogs.

Page 28: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 28/62

28

28ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

General Unix Commands.

pwd - Display Working directory.

cd - Change Working directory.

 – cd [Directory]

pwd - Display Working directory.

Example.

$> pwd 

Output :

 /home/ajit

cd - Change Working directory.

This command is used to change the working directory.

Example.

$> cd cprogs 

Output : Changes the working directory to the directory named cprogs.

$> cd

Output : Changes the working directory to the home directory.

Page 29: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 29/62

29

29ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

General Unix Commands

mkdir - Make Directories.

 – mkdir [options] DirectoryName

rmdir - Remove Directories.

 – rmdir [options] DirectoryName

mkdir - Make Directories.

Creates directories by the name requested by the user.

Standard entries in a directory ( ., .. ) are made automatically.

Option -m allows user to define the mode to be used for directory.

Example:

$> mkdir javaprogs 

Output : Creates a directory by name “javaprogs”.

rmdir - Remove Directories.

Removes the specified directory if it is empty .i.e. it does not contain any files ordirectories.

Example:

$> rmdir javaprogs 

Output : Removes the empty directory “javaprogs”.

Page 30: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 30/62

30

30ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

General Unix Commands

rm - Remove files or directories.

 – rm [option...] file ...

ln - Make link to files.

 – ln file1 [file2 ...] target

rm - Remove files or directories.

Removes one or more files from a directory.

Removal of a file requires write permission in its directory.

Option -f force the removal of files that do not have write permission.

Option -r recursively delete the entire contents of directories specified and thedirectories themselves.

Option -i interactively ask to delete each file.Example:

$> rm dept.dat 

Output : Deletes the file dept.dat.

ln - Make link to files.

A link is a directory entry to a file.

Any changes to a file are effective, independent of the name by which the file isknown.

Links are of two types : Hard Link and Symbolic Link.

Example:

$> ln cprog1 cprog1.bat 

This is an example of hard link. cprog1 should already exist and cprog1.bat should notexist already. If cprog1 is removed, cprog1.bat is not deleted. cprog1 andcprog1.bat have same i-node numbers ( that could be seen in the long listing).

$> ln –s cprog2 /tmp/cprog2.bat 

This is an example of symbolic link. cprog2 should already exist and cprog2.bat in tmpdirectory should not exist. This creates a link to cprog2.bat under tmp directory. Ifcprog2 is removed cprog2.bat stays as an invalid link. cprog2 and cprog2.bat havedifferent inode numbers ( that could be seen in the long listing).

could be seen in the long list of the symbolically linked file entries.

Page 31: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 31/62

31

31ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

General Unix Commands

cal – Calendar.

 – cal [options] [arguments]

who – Displays the users connected

 – who [options]

cal – Displays Calendar.

Examples.

$> cal 

Output : Displays the calendar for the present month.

$> cal 2004 

Output : Displays the calendar for the year 2004.

$> cal 04 2004 

Output : Displays the calendar for the month April 2004.

who – Displays the users connected to the system.

Example

$> who 

deepak pts/0 May 11 09:20 (172.21.102.80)

isjan8 pts/1 May 11 14:19 (172.21.103.107)

comp10 pts/7 May 11 14:50 (172.21.102.186)

osjan6 pts/2 May 11 15:09 (172.21.106.100)

pract1 pts/9 May 11 15:26 (172.21.106.107)

Page 32: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 32/62

32

32ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

General Unix Commands

more – Allows display page wise.

 – more [files]

whoami – Display username.

more – Allows display page-wise

Example

/home/ajit>more grt1 grt4

Output : Displays each file pagewise. On pressing the Enter key, the 2nd file getsdisplayed.

whoami – Displays the username.Example

$> whoami 

ajit

The command if used with spaces, would give details about the user.

$> who am i 

ajit pts/2 Jun 27 17:15 (myshec25336d.ad.infosys.com)

Page 33: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 33/62

33

33ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Standard Files.

Standard Input (0).

 – This file is opened by shell to accept information.

Standard Output (1).

 – This file is opened by shell to direct output.

Standard Error (2).

 – This file is opened by shell for writing error messages.

Standard Input (0)

Takes the keyboard or files as input.

Example

$> mail user1 < s1 #s1 is the name of the file.

The contents of s1 is mailed to user1.

Here the input for the command “mail” is coming from file user1

Standard Output (1)

This is the default file.

Example

$> ls 1> infy #Redirects the output to a file calledinfy.

Is same as

$> ls > infy

Standard Error (2)

Is used for redirecting the errors.Example

$> ls abcd11 2> def #Redirects the errors into a file calleddef

$> cat def

ls: abcd11: No such file or directory

Page 34: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 34/62

34

34ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Special Files.

 /dev/tty

 – It is used to display on the terminal.

 /dev/null

 – This file is used to suppress the output from being displayed.

 – Used for suppressing error messages from being displayed on the screen in

programs.

/dev/tty

Allows display of output on one’s own terminal.

Example

$> ls > /dev/tty

Output : Outputs the directory listing on the terminal screen.

/dev/null

Suppresses output from being displayed.

Example

$> ls > /dev/null

$>

Output : Output of “ls” will not be displayed.

Page 35: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 35/62

35

35ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Regular Expressions.

What is it?

String of ordinary and metacharacter which can be used to match more than

one type of pattern.

Uses character set

 – * , [], ^, $, {}, etc.

A regular expression  is a string of ordinary and metacharecter which can be used tomatch more than one type of pattern.

Page 36: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 36/62

36

36ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

The Shell Metacharacters.

* - Matches all filenames in current directory.

? - Matches a single character.

[abc] - Matches a single character – either a, b or c.

[!abc] - Matches a single character – which is not a, b or c.

[a-c] – Matches a single character which is within the range of a and c.

^abc – Matches the pattern abc at the beginning of the line.

abc$ - Matches the pattern abc at the end of the line.

* - Matches all filenames in current directory.

$> ls a* 

Output : Displays the files and directories starting with the letter “a”.

? - Matches a single character.

$> ls f? Output : Displays the files and directories starting with the letter “f”, followed by asingle character.

[abc] - Matches a single character – either a, b or c.

$> ls f[abc] 

Output : Displays the files and directories starting with the letter “f”, followed by asingle letter which is either ”a”, “b” or “c”.

[!abc] - Matches a single character – which is not a, b or c.

$> ls f[!abc] 

Output : Displays the files and directories starting with the letter “f”, followed by asingle letter which is not ”a”, “b” or “c”.

[a-c] – Matches a single character which is within the range of a and c.

$> ls f[a-c] 

Output : Displays the files and directories starting with the letter “f”, followed by asingle letter which is in the range within ”a” and “c”.

Page 37: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 37/62

37

Unix.The Vi Editor.

Page 38: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 38/62

38

38ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Editor in Unix

Need for editor in Unix.

Types of editor

 – Line Editor.

• ed : UC Berkeley

• ex : Powerful than ed, Bell Systems

 – Full Screen Editor.

• vi (stands for visual).

• vim – vi improved

• emacs (GNU)

Type of editors available under UNIX

Line editors

ed : UC Berkeley

ex : Powerful than ed, Bell Systems

Screen editors

Vi (stands for visual) is the full screen editor

VIM - Vi Improved

Emacs (GNU’s)

Page 39: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 39/62

39

39ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

The vi Editor.

The important characteristic features are:

 – Omnipresent.

 – Fast.

 – Powerful UNDO features.

Omnipresent : vi Editor works on different Unix flavors.

Fast : In vi editor, Editing and various operations are very fast.

Powerful UNDO features : In vi Editor, texts in lines could be undone with very lesseffort.

Page 40: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 40/62

40

40ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

The vi Editor.

The limitations are:

 – Less user-friendly.

 – Highly Case-sensitive.

 – Keystrokes could have more than one meaning.

Less User-friendly : As vi Editor does not have a graphical user interface, hence theuser needs to have a knowledge of the commands before the user works on theeditor.

Highly case-sensitive : vi Editor is highly case-sensitive. i.e. a letter in small case

would has a different meaning in comparison with the same letter in upper case.E.g. “a” appends the data after the current character, whereas “A” appends the data

after the current line.

Keystrokes : As vi Editor operates in various modes, a letter could have differentmeaning in different modes.

E.g. In command mode, “a” stands for append operation, whereas ininsert mode, “a” stands for data.

Page 41: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 41/62

41

41ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

The vi Editor.

Modes of working:

 – Command Mode.

 – Insert Mode.

 – Escape Mode.

vi works in 3 different modes

Command mode

• Keys are interpreted as commands.

E.g. “a” is used for appending, “i” is used for inserting, “x” is used for deleting.

Insert mode

• Keys echoed in edit buffer. The letter entered represents data.

Escape mode

• Keys interpreted as commands of ex mode.

E.g. :wq is used for saving and exiting from vi editor.

Page 42: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 42/62

42

42ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

vi Operating modes.

Command mode

Insert mode

i, I , o, O, a, A ..

esc

Escape mode

Enter:

:q

Whenever a particular file is opened using the vi editor, it opens up in the Commandmode. In this mode, the keyboard-character represents command and not data. Auser cannot enter the data in the Command mode. The user has to switch over tothe insert mode to enter the data. This could be done using the letters like “a”, ”i”,etc. Once the user is in insert mode, the data could be entered. The user couldswitch back to the Command mode. For saving or exiting the application the user

has to get into the Escape mode by pressing ‘Esc’ key.

Page 43: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 43/62

43

43ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Vi editor commands.

To move around

 – h, j, k, l, ^D, ^ U, G, 0, ^, $, w, b

Inserting/Deleting text

 – i, a, I, A, r, R, o, O, dd, dw, c$, D, x, X.

Changing/Replacing text.

 – cc, cw, c$, ~, J, u, . , yy, yw, p, P

File manipulation.

 – :w, :wq, ZZ, :w!, :q, :q! , :![command ]

The instructor should show few commands, to make the audience aware of the usagein vi editor. Rest of the commands could be given as a self-study and practice forthe audience.

Page 44: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 44/62

44

44ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Searching a pattern.

 / pattern 

 – Searches forward for first occurrence of a pattern.

?pattern 

 – Searches backward for first occurrence of a pattern.

n

 – Repeats the last search.

N

 – Repeats the last search command in opposite direction.

Page 45: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 45/62

45

45ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Pattern Substitution.

:s/ptn1/ptn2

 – Replaces first occurrence of ptn1 with ptn2.

: s/ptn1/ptn2/g

 – Replaces all occurrences in the current line.

: m, n s/ptn1/ptn2/g

 – Replaces all occurrences in lines m to n.

: ., $ s/ptn1/ptn2/g

 – Replaces all occurrences from current line to end of file.

Page 46: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 46/62

46

46ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Customizing vi.

The set command.

 – :set all

 – :set nu

The abbr command.

 – :abbr itl “Infosys Technologies Ltd”

The map command.

 – :map ^X :wq

Using the set command, the user could customize the shell.

Page 47: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 47/62

47

47ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Vi Startup File.

.exrc

 – vi reads .exrc before loading.

 – settings are permanent for a vi session.

The .exrc file contains the customizations for the vi editor.

Page 48: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 48/62

48

48ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Handling multiple files.

vi file1 file2 file3

 – Opens all files one after another.

: n

 – Permits editing of next file in the buffer.

: rew

 – Permits editing of first file in buffer.

: args

 – Displays names of all files in the buffer.

: f

 – Displays the name of the current file.

Page 49: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 49/62

49

Unix.Advanced Unix Commands

Page 50: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 50/62

50

50ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Advanced Unix Commands.

Text Processing Utilities.

Process Oriented Commands.

Page 51: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 51/62

51

51ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Text Processing Utilities.

tr - Translate characters.

 – Translates character/characters.

 – tr [option...] string1 [string2]

cmp

 – Compare 2 files.

 – cmp file1 file2

tr - Translate characters.

tr is a filter program that doesn't recognize file arguments.

Option. -c complement the content of string1;that is, all characters except those instring1 are considered.

Option -d delete all the input characters specified in string1.Any string2 is ignored.

Option -s compress all strings of repeated output characters that are in string2 into

single character.Example:

$> cat prog1 | tr “a” “b” 

Output:

This will convert any occurrence of the letter “a” in the file to the letter “b”.

$> cat prog1 | tr -s “ ” “:” 

Output:

This will convert all multiple occurrences of white spaces between the words in the textinto one and replaces spaces with a “:”.

cmp - Compares 2 files.

Compares two files and if they are different, it displays the byte and the line number of

the first difference.Option -l prints the byte number and the differing bytes for each difference

Option -s returns an exit code 0 for identical files, 1 if files are different and 2 if file isinaccessible.

Example:

$> cmp prog1 prog2 

Page 52: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 52/62

52

52ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Text Processing Utilities (Contd…)

cut

 – Cuts selected fields of each line of a file.

 – cut –flist [-d char] [file1 file2 ...]

paste

 – Merging the corresponding lines of given files.

 – paste file..

cut - Cuts selected fields of each line of a file.

List following -f is a list of fields assumed to be separated by a delimiter character.

Character following -d is the delimiter.

Example:

$> cut -f1 -d “:” prog1

Output: This statement extracts the first field from file prog1. The delimiter between thefields is a “:”.

$> cut -c1 prog1

Output: This statement extracts the first character of all the lines from file prog1.

paste - Merging the corresponding lines of given files.

This command allows horizontal pasting of files.

Example.

$> cat file1

1000

2000

$> cat file2 

80009000

$> paste file1 file2 

1000 8000

2000 9000

Page 53: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 53/62

53

53ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Text Processing Utilities (Contd…)

sort

 – Ordered arrangement.

 – sort [option] file

comm

 – Compares 2 sorted files.

 – comm file1 file2

sort - Ordered arrangement.

Option -d dictionary order

Option -n arithematic order

Option -r reverse order

Example:

$> cat file3 

frankfurt

mysore

canberra

$> sort file3 

canberra

frankfurt

mysore

Page 54: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 54/62

54

54ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Text Processing Utilities (Contd…)

Please view the notes page below for an explanation of the earlier slide

comm - Compares 2 sorted files & displays a three-columnar output.

The first column contains lines unique to the first file, while the second column showslines unique to the

second file. The third column displays lines common to both the files.

Example:

$> cat data1

argentina

india

myanmar

$> cat data2 

brazil

denmark

india

$>comm data1 data2 

argentina

brazil

denmark

india

myanmar

Page 55: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 55/62

55

55ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Text Processing Utilities (Contd…)

head

 – Display the first count lines of files.

 – head [-count] [file ...]

tail

 – Display the last count lines of a file.

 – tail [+/-[number] [lbc]] [option] file

head - Display the first count lines of fileIf count is not specified, then ten lines are printed.

If no files are specified, head reads from the standard input.

Example:

$> head -5 prog1

Output : The first 5 l ines of the files would be displayed.

tail - Display the last count lines of a file.

Copies the last part of the named file to the standard output.

Displays lines of file beginning with +number from the beginning or -number from end.

Number is counted in units of lines (l), disk block size (b), or characters (c).

By default, number is counted in units of lines.

Option -f enables tail to follow the growth of a file.

Example:

$> tail -5 prog1

Output : The last 5 lines of the files would be displayed.

Page 56: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 56/62

56

56ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Text Processing Utilities (Contd…).

wc

 – Counts and displays the number of lines, words and characters in a file.

 – wc [option...] filename...

grep

 – Pattern searching in a file.

 – grep [option...] pattern [file...]

wc - Counts and displays the number of lines, words and characters in a file.

Option -l display the number of lines

Option -w display the number of words

Option -c display the number of characters (includes whitespace characters ).

Example:

$> wc cprog1

Output :

3 9 60 cprog1

$> wc -c prog2 

Output :

60 cprog1

grep - Pattern searching in a file.

Display the lines containing the pattern on the standard output.

Option -c report only the number of matching lines

Option -l list only the names of files containing pattern.

Option -v display all lines except those containing pattern.

Example:$> grep “the” prog2 

Output:

Displays all the lines in the file prog2 containing the word “the”.

$> grep -c “the” prog2 

Output:

Displays the number of lines in the file prog2 containing the word “the”.

Page 57: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 57/62

57

57ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Text Processing Utilities (Contd…).

diff

 – Reports the differences between files.

 – diff [option...] file1 file2

uniq

 – To remove adjacent repeated lines.

 – uniq [option] [file]

diff - Reports the differences between files.

Option –b ignore trailing whitespaces and compare other strings of space characters.

Example:

$> diff prog1 prog2 

uniq - To remove adjacent repeated lines.

Option -u display only the lines not repeated in the file named input.

Option -d display only the lines that are repeated in the file input.

Option -c precede each line displayed by the number of times it occurs.

+n ignore the first n characters including whitespace

-m ignore the first m fields and any blanks.

Example:

$> uniq -u prog1

Page 58: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 58/62

58

58ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Process Oriented Commands.

nohup

 – Continue execution even after logout.

 – nohup command [arguments] &

nice

 – Execute commands with lower priority.

 – nice [option] command [arguments] 

NOHUP

Continue to execute the named command  and optional command arguments after youlog out.

NICE

Execute a command and arguments with lower priority (i.e., be nice to other users)

Option: 

-n Run command with a niceness of n ( 1-19). Default is 10. Higher n implies lowerpriority. A privileged user can raise the priority by specifying a negative n ( e.g., -4)

Page 59: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 59/62

59

59ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Process Oriented Commands (Contd…).

kill

 – Send signal to processes.

 – kill -[option] pid

kill - send signal to processesKill sends signal to the process with process ID as pid. If no option is specified ,itsends signal 15 (software termination signal) to the process.

Option - 1 Hangup signal.

Option - 2 Interrupt signal.

Option - 9 terminate the process.Option -15 Software termination signal.

Example:

$ sleep 20 &

[1] 280#280 is the PID

$ kill -9 280

[1] + killed sleep 20 &

Page 60: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 60/62

60

60ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Summary

Background

Features of Unix

Unix System Architecture

Unix File System

Processes

General Unix commands and utilities.

Regular Expressions.

Page 61: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 61/62

61

61ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Summary (Contd...)

Vi Editor

Modes of operation

Pattern Searching

Text Processing Commands

Process oriented commands.

Page 62: Unit1_Unix_OS31.pdf

7/27/2019 Unit1_Unix_OS31.pdf

http://slidepdf.com/reader/full/unit1unixos31pdf 62/62

62ER/CORP/CRS/OS31/003

Version No: 2.00Copyright © 2004, Infosys

Technologies Ltd

Thank You!