You are on page 1of 62

Unix

Introduction to Unix Operating System.

Welcome to this course on Unix Operating System.

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.

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

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.

References
Brain W. Kernighan and Rob Pike, The UNIX Programming Environment. Stephen Prata, Advanced UNIX - A Programmers 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

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

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.

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

Day 1 Introduction to Unix. To understand Need for Unix in todays 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 commands associated with it. Basic Unix Commands and Utilities. To understand Unix commands and utilities required for basic operations in Unix. 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 in Unix.

Session Plan
Day 2 Types of Shell. Shell Programming. File Handling Commands.

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

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 .

Session Plan
Day 3 Unix Utilities. Unix Program Development Tools. Unix Communication. Security Features

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

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.

Introduction to Unix.
What is Unix? Need for Unix in todays world? Evolution of Unix. Flavors of Unix.

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

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. The need 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 a PDP-7 machine. UNICS is pun on MULTICS and stands for Uniplexed Information and Computing Services. 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 OS LINUX

Features Of Unix.
The important characteristic features are
Portability. Security. Background Processing. Pipes. Redirection Tools. Software Development Tools. Stable and Reliable

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

Portability : As Unix has been re-written in C , hence it can run on machines from microcomputers to mainframe computers. Security : Unix is a very secure operating system. Without the proper username and passwords, 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 from the user. Pipes : Using the concept of pipes, a Unix user/administrator can easily link and work with multiple commands at the same time. Redirection Tools : These tools allow data to be re-directed between files as per the requirement of the user. Software Development Tools : Unix supports a wide variety of tools using which the user could create and work on different programs .viz. C Language on the Unix Operating 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 less prone to crashes.

Features Of Unix (Contd...)


Easy to write programs. Hierarchical File System. Shells. Communication.

Copyright 2004, Infosys Technologies Ltd

ER/CORP/CRS/OS31/003 Version No: 2.00

Easy to write programs : Its easy to write programs in Unix as it hides machine architecture from the user. Hierarchical File System : Unix employs a hierarchical file system which is easy to implement and maintain. Shells : Unix has different types of shells .viz. Bourne, C, Korn, etc. Communication : Unix has commands which allow communication between different users connected to the system.

System Architecture.
Major components of Unix are :
Kernel Shell Utilities User Applications

Copyright 2004, Infosys Technologies Ltd

10

ER/CORP/CRS/OS31/003 Version No: 2.00

Kernel, the most important part of OS, interacts directly with the hardware providing common services to programs and insulating them from hardware. It is a program that monitors and controls the resources of a computer and allocates them among its users in an optimal manner. The shell provides a processing environment for the user programs. It acts like a command 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 a series of shell commands, can be executed using the shell. Utilities includes programs that interact with the kernel by invoking a well-defined set of system 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.

10

System Architecture.

User User
SHELL
UNIX

User User
SHELL
OTHER APPLICATIONS

COMMANDS
HARDWARE

DATABASE
PACKAGES

KERNEL

COMPILERS
SHELL

SHELL

User User

User User

Copyright 2004, Infosys Technologies Ltd

11

ER/CORP/CRS/OS31/003 Version No: 2.00

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 that part of the Unix system that is loaded into memory when the system is booted. It manages 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 welldefined 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 the interpreter of user requests. It takes a command from the user, deciphers it, and communicates with the Kernel to see that the command is executed. It is actually the interface between the user and the kernel, which effectively insulates the user from the knowledge 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 a processing environment to the user programs.

11

Operating System Services.


Kernel. Hardware Transparency. Interrupts and Exceptions. Processor Execution Levels. Memory Management.

Copyright 2004, Infosys Technologies Ltd

12

ER/CORP/CRS/OS31/003 Version No: 2.00

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. Two methods are used: Swapping: Entire process is written to swap device to accommodate another 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 data Kernel 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 not distinguish between the user processes and the OS has internal bookkeeping to distinguish between processes.

12

Operating System Services (Contd...)


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

Copyright 2004, Infosys Technologies Ltd

13

ER/CORP/CRS/OS31/003 Version No: 2.00

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 an interrupt occurs. After servicing the interrupt the context is restored and the process proceeds. The order 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 events external to a process - exceptions usually occur in the middle of a process where as interrupts occur between 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 process may 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 generates addresses for a virtual machine as if no other program will execute simultaneously on the physical machine. Virtual addresses generated by the compiler need not be identical to the actual physical addresses in the machine. The memory management coordinates with the machine hardware to setup a virtual to physical address translation that maps the compiler-generated address to the physical machine address.

13

The User Perspective.


Processing environment Unix File system Building block primitives

Copyright 2004, Infosys Technologies Ltd

14

ER/CORP/CRS/OS31/003 Version No: 2.00

From the users viewpoint the following aspects of Unix will be highlighted: Processing environment. File system. Building block primitives.

14

Processing Environment.
User Program. Process. Shell.

Copyright 2004, Infosys Technologies Ltd

15

ER/CORP/CRS/OS31/003 Version No: 2.00

A program is just an executable file whereas a process is an instance of a program in execution. The creation / termination of processes, the synchronization and reaction to interrupts is controlled through system calls. An example of a program using the kernel through system calls is the shell - the COMMAND INTERPRETER. The shell is a user program that can be tailored to the needs of the user. The shell can also execute commands asynchronously i.e. without waiting for the prior command to terminate, background execution. The shell provides a processing environment for the user programs.

15

Structure of Unix File System.

/ (root)

home bin sh

dev ls tty lp

etc

tmp

lib

usr

passwd terminfo

user1 user2

Copyright 2004, Infosys Technologies Ltd

16

ER/CORP/CRS/OS31/003 Version No: 2.00

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 Owners Group Others

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

16

Inodes and Linking. A directory


FILENAME INODE NUM FILENAME INODE NUM

Another directory

file1 file2

0221 0412 name1 file3 0221 0981

File1 and name1 are links with same inode numbers


Inode block #0412 #0221
Copyright 2004, Infosys Technologies Ltd 17 ER/CORP/CRS/OS31/003 Version No: 2.00

The information pertaining to the Unix files is kept in the i-node (identification node) block, which is reserved exclusively for the use of kernel. Every file in the file system will invariably have an entry in this area. Every file is associated with an i-node number, called the i-number. The command ls is used to list the contents of the current directory. The option i used with ls would indicate the i-node number for a file.

17

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.

Copyright 2004, Infosys Technologies Ltd

18

ER/CORP/CRS/OS31/003 Version No: 2.00

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 is mentioned. 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 is mentioned. .. 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 ..

18

How Unix Works.


LOGIN SHELL ASKS FOR A COMMAND USER TYPES COMMAND SHELL EXECUTES UTILITY TO CARRY OUT COMMAND USER INTERACTS WITH UTILITY SHELL PROMPTS FOR NEXT COMMAND USER TYPES CONTROL-D LOGOUT
Copyright 2004, Infosys Technologies Ltd 19 ER/CORP/CRS/OS31/003 Version No: 2.00

The user connects to the Unix Server using an utility called telnet, and by mentioning the IP address of the Unix Server. The Server requests for the username and password. The user needs to give the valid username 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.

19

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.

Copyright 2004, Infosys Technologies Ltd

20

ER/CORP/CRS/OS31/003 Version No: 2.00

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.

20

The Other System Settings.


The $PATH variable. Aliases. The tty, stty commands. The [ctrl-c] , [ctrl-d] and other keys.

Copyright 2004, Infosys Technologies Ltd

21

ER/CORP/CRS/OS31/003 Version No: 2.00

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.

21

Processes in Unix.
Understanding the term Process. Process related commands.
ps command.

Copyright 2004, Infosys Technologies Ltd

22

ER/CORP/CRS/OS31/003 Version No: 2.00

A Process is a program which is under execution. When any program (i.e. and executable file) is executed, it gives rise to a process. In other words, a process is said to be born when the program starts execution, and remain alive as long as the program is active. The process is said to die when the program completes execution. 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 the Kernel allocates when the process is born. Just as a file has a parent, every process also has its own creator, which itself is another process. The process responsible for giving birth to another process is called 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 a particular terminal.

22

Processes in Unix (Contd)


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

Copyright 2004, Infosys Technologies Ltd

23

ER/CORP/CRS/OS31/003 Version No: 2.00

E.g. $> ps PID TTY 20353 pts/2 20446 pts/2 TIME CMD 00:00:00 bash 00:00:00 ps

To see the details about the ancestry (PID and PPID) of a process, ps command has an option f. E.g. $> ps -f UID ajit ajit PID PPID C STIME TTY 20353 20352 0 16:31 pts/2 20539 20353 0 16:34 pts/2 TIME CMD 00:00:00 -bash 00:00:00 ps f

23

Building Block Primitives.


Redirection of I/O. Pipes.

Copyright 2004, Infosys Technologies Ltd

24

ER/CORP/CRS/OS31/003 Version No: 2.00

The Unix philosophy supports building complex programs from simple primitives. The following 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 read and the write processes. It allows the output of one command to become input of another command. In other words, the standard output file of the command gets redirected 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.

24

Unix Command Structure.


Unix Command line structure.
command [options] [arguments]

Copyright 2004, Infosys Technologies Ltd

25

ER/CORP/CRS/OS31/003 Version No: 2.00

Unix is all about working on commands. The user can execute commands or create programs containing the commands for his/her application. 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 Enter key 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 the calendar for January 2004. Here 01 and 2004 represents arguments and are not options.

25

General Unix Commands.


ls - List contents of a directory.
ls [option] [filenames]

cat - Concatenate and display files.


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

Copyright 2004, Infosys Technologies Ltd

26

ER/CORP/CRS/OS31/003 Version No: 2.00

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 already exists. 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.

26

General Unix Commands.


cp - Copy files.
cp [option...] [file1] [file2]

mv - Move or rename files.


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

Copyright 2004, Infosys Technologies Ltd

27

ER/CORP/CRS/OS31/003 Version No: 2.00

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 mentioned directory. 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 mentioned directory. 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 the file 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.

27

General Unix Commands.


pwd - Display Working directory.

cd - Change Working directory.


cd [Directory]

Copyright 2004, Infosys Technologies Ltd

28

ER/CORP/CRS/OS31/003 Version No: 2.00

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.

28

General Unix Commands


mkdir - Make Directories.
mkdir [options] DirectoryName

rmdir - Remove Directories.


rmdir [options] DirectoryName

Copyright 2004, Infosys Technologies Ltd

29

ER/CORP/CRS/OS31/003 Version No: 2.00

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 or directories. Example: $> rmdir javaprogs Output : Removes the empty directory javaprogs.

29

General Unix Commands


rm - Remove files or directories.
rm [option...] file ...

ln - Make link to files.


ln file1 [file2 ...] target

Copyright 2004, Infosys Technologies Ltd

30

ER/CORP/CRS/OS31/003 Version No: 2.00

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 the directories 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 is known. 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 not exist already. If cprog1 is removed, cprog1.bat is not deleted. cprog1 and cprog1.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 tmp directory should not exist. This creates a link to cprog2.bat under tmp directory. If cprog2 is removed cprog2.bat stays as an invalid link. cprog2 and cprog2.bat have different inode numbers ( that could be seen in the long listing). could be seen in the long list of the symbolically linked file entries.

30

General Unix Commands


cal Calendar.
cal [options] [arguments]

who Displays the users connected


who [options]

Copyright 2004, Infosys Technologies Ltd

31

ER/CORP/CRS/OS31/003 Version No: 2.00

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)

31

General Unix Commands


more Allows display page wise.
more [files]

whoami Display username.

Copyright 2004, Infosys Technologies Ltd

32

ER/CORP/CRS/OS31/003 Version No: 2.00

more Allows display page-wise Example /home/ajit>more grt1 grt4 Output : Displays each file pagewise. On pressing the Enter key, the 2nd file gets displayed. 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)

32

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.

Copyright 2004, Infosys Technologies Ltd

33

ER/CORP/CRS/OS31/003 Version No: 2.00

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 infy. Is same as $> ls > infy Standard Error (2) Is used for redirecting the errors. Example $> ls abcd11 2> def def $> cat def ls: abcd11: No such file or directory #Redirects the errors into a file called #Redirects the output to a file called

33

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.

Copyright 2004, Infosys Technologies Ltd

34

ER/CORP/CRS/OS31/003 Version No: 2.00

/dev/tty Allows display of output on ones 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.

34

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.

Copyright 2004, Infosys Technologies Ltd

35

ER/CORP/CRS/OS31/003 Version No: 2.00

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

35

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 ^abc Matches the pattern abc at the beginning of the line. abc$ - Matches the pattern abc at the end of the line. of a and c.

Copyright 2004, Infosys Technologies Ltd

36

ER/CORP/CRS/OS31/003 Version No: 2.00

* - 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 a single 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 a single 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 a single 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 a single letter which is in the range within a and c.

36

Unix.
The Vi Editor.

37

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)

Copyright 2004, Infosys Technologies Ltd

38

ER/CORP/CRS/OS31/003 Version No: 2.00

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 (GNUs)

38

The vi Editor.
The important characteristic features are:
Omnipresent. Fast. Powerful UNDO features.

Copyright 2004, Infosys Technologies Ltd

39

ER/CORP/CRS/OS31/003 Version No: 2.00

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 less effort.

39

The vi Editor.
The limitations are:
Less user-friendly. Highly Case-sensitive. Keystrokes could have more than one meaning.

Copyright 2004, Infosys Technologies Ltd

40

ER/CORP/CRS/OS31/003 Version No: 2.00

Less User-friendly : As vi Editor does not have a graphical user interface, hence the user needs to have a knowledge of the commands before the user works on the editor. 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 different meaning in different modes. E.g. In command mode, a stands for append operation, whereas in insert mode, a stands for data.

40

The vi Editor.
Modes of working:
Command Mode. Insert Mode. Escape Mode.

Copyright 2004, Infosys Technologies Ltd

41

ER/CORP/CRS/OS31/003 Version No: 2.00

vi works in 3 different modes Command mode Insert mode Keys echoed in edit buffer. The letter entered represents data. Keys interpreted as commands of ex mode. E.g. :wq is used for saving and exiting from vi editor. Escape mode Keys are interpreted as commands. E.g. a is used for appending, i is used for inserting, x is used for deleting.

41

vi Operating modes.

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

Command mode :
Enter esc

Insert mode

Escape mode

:q

Copyright 2004, Infosys Technologies Ltd

42

ER/CORP/CRS/OS31/003 Version No: 2.00

Whenever a particular file is opened using the vi editor, it opens up in the Command mode. In this mode, the keyboard-character represents command and not data. A user cannot enter the data in the Command mode. The user has to switch over to the 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 could switch back to the Command mode. For saving or exiting the application the user has to get into the Escape mode by pressing Esc key.

42

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]

Copyright 2004, Infosys Technologies Ltd

43

ER/CORP/CRS/OS31/003 Version No: 2.00

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

43

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.

Copyright 2004, Infosys Technologies Ltd

44

ER/CORP/CRS/OS31/003 Version No: 2.00

44

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.

Copyright 2004, Infosys Technologies Ltd

45

ER/CORP/CRS/OS31/003 Version No: 2.00

45

Customizing vi.
The set command.
:set all :set nu

The abbr command.


:abbr itl Infosys Technologies Ltd

The map command.


:map ^X :wq

Copyright 2004, Infosys Technologies Ltd

46

ER/CORP/CRS/OS31/003 Version No: 2.00

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

46

Vi Startup File.
.exrc
vi reads .exrc before loading. settings are permanent for a vi session.

Copyright 2004, Infosys Technologies Ltd

47

ER/CORP/CRS/OS31/003 Version No: 2.00

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

47

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.

Copyright 2004, Infosys Technologies Ltd

48

ER/CORP/CRS/OS31/003 Version No: 2.00

48

Unix.
Advanced Unix Commands

49

Advanced Unix Commands.


Text Processing Utilities. Process Oriented Commands.

Copyright 2004, Infosys Technologies Ltd

50

ER/CORP/CRS/OS31/003 Version No: 2.00

50

Text Processing Utilities.


tr - Translate characters.
Translates character/characters. tr [option...] string1 [string2]

cmp
Compare 2 files. cmp file1 file2

Copyright 2004, Infosys Technologies Ltd

51

ER/CORP/CRS/OS31/003 Version No: 2.00

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 in string1 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 text into 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 is inaccessible. Example: $> cmp prog1 prog2

51

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..

Copyright 2004, Infosys Technologies Ltd

52

ER/CORP/CRS/OS31/003 Version No: 2.00

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 the fields 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 8000 9000 $> paste file1 file2 1000 8000 2000 9000

52

Text Processing Utilities (Contd)


sort
Ordered arrangement. sort [option] file

comm
Compares 2 sorted files. comm file1 file2

Copyright 2004, Infosys Technologies Ltd

53

ER/CORP/CRS/OS31/003 Version No: 2.00

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

53

Text Processing Utilities (Contd)


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

Copyright 2004, Infosys Technologies Ltd

54

ER/CORP/CRS/OS31/003 Version No: 2.00

comm - Compares 2 sorted files & displays a three-columnar output. The first column contains lines unique to the first file, while the second column shows lines 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

54

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

Copyright 2004, Infosys Technologies Ltd

55

ER/CORP/CRS/OS31/003 Version No: 2.00

head - Display the first count lines of file If 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 lines 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.

55

Text Processing Utilities (Contd).


wc
Counts and displays the number of lines, words and wc [option...] filename... characters in a file.

grep
Pattern searching in a file. grep [option...] pattern [file...]

Copyright 2004, Infosys Technologies Ltd

56

ER/CORP/CRS/OS31/003 Version No: 2.00

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.

56

Text Processing Utilities (Contd).


diff
Reports the differences between files. diff [option...] file1 file2

uniq
To remove adjacent repeated lines. uniq [option] [file]

Copyright 2004, Infosys Technologies Ltd

57

ER/CORP/CRS/OS31/003 Version No: 2.00

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

57

Process Oriented Commands.


nohup
Continue execution even after logout. nohup command [arguments] &

nice
Execute commands with lower priority. nice [option] command [arguments]

Copyright 2004, Infosys Technologies Ltd

58

ER/CORP/CRS/OS31/003 Version No: 2.00

NOHUP Continue to execute the named command and optional command arguments after you log 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 lower priority. A privileged user can raise the priority by specifying a negative n ( e.g., -4)

58

Process Oriented Commands (Contd).


kill
Send signal to processes. kill -[option] pid

Copyright 2004, Infosys Technologies Ltd

59

ER/CORP/CRS/OS31/003 Version No: 2.00

kill - send signal to processes Kill sends signal to the process with process ID as pid. If no option is specified ,it sends 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] $ kill -9 280 [1] + killed sleep 20 & 280 #280 is the PID

59

Summary
Background Features of Unix Unix System Architecture Unix File System Processes General Unix commands and utilities. Regular Expressions.

Copyright 2004, Infosys Technologies Ltd

60

ER/CORP/CRS/OS31/003 Version No: 2.00

60

Summary (Contd...)
Vi Editor Modes of operation Pattern Searching Text Processing Commands Process oriented commands.

Copyright 2004, Infosys Technologies Ltd

61

ER/CORP/CRS/OS31/003 Version No: 2.00

61

Thank You!
Copyright 2004, Infosys Technologies Ltd 62 ER/CORP/CRS/OS31/003 Version No: 2.00

62

You might also like