<<

IV SEM B Sc CS4T1: AND syllabus unit 1

Introduction: Definition, Types of Operating Systems, Functions of Operating System, services, system components System call. Process Management: Process Concept, Process Scheduling, Inter process communication, CPU Scheduling Criteria, Scheduling algorithm, Multiple Processor Scheduling, Real Scheduling, Algorithm evolution. Unit – II Process Synchronization and deadlocks: The Critical Section Problem, Synchronization hardware, Semaphores, Classical problems of synchronization, Critical regions, monitors, Dead locks – system model, Characterization, Dead lock prevention, avoidance and detection, Recovery from dead lock, Combined approach to deadlock handling. Unit - III Memory management: Functions, single contiguous, Partitioned memory management: multiple relocatable partitioned memory management, paging segmentation, demand paging virtual memory management. Management: Concept, access methods, directory structures, allocation methods, free space management, secondary storage structure. Disk Management: Disk Structure & Scheduling methods, Disk management, Swap – Space management. Unit-IV of Unix, salient features, Unix Components, types of shell, Internal and External commands, Files and File Organization- Categories of files, Unix file system, directories, file related commands, Directory related commands, wild cards, Printing and Comparing files. Ownership of files, File attributes File permissions and Manipulations, Standard I/O, Redirection, pipe, filter.

Unit-V Introduction to editor, The three modes of the vi editor, Invoking vi editor, Configuring the vi environment, Regular expressions, the , The process - parent and child process, process creation, process related commands, Shell Programming - shell script features, shell variables, writing and executing a shell script, positional parameters, Branching control structures- if, case etc., Loop control structures– while, until, for, etc., Jumping control structures – break, continue, , etc., Integer and Real arithmetic in shell programs, Debugging scripts.

1 CM_aps 1. A Brief History of UNIX

In the late 1960s, researchers from General Electric, MIT and launched a joint project to develop an ambitious multi-user, multi-tasking OS for mainframe known as MULTICS (Multiplexed Information and System). MULTICS failed, but it did inspire Ken Thompson, was a researcher Bell Labs, to have a go at writing a simpler operating system himself. He wrote a simpler version of MULTICS on a PDP7 in assembler and called his attempt UNICS (Uniplexed Information and Computing System). Because memory and CPU power were at a premium in those days, UNICS (eventually shortened to UNIX) used short commands to minimize the space needed to store them and the time needed to decode them - hence the tradition of short UNIX commands we use today.

Ken Thompson then teamed up with , the author of the first C compiler in 1973. They rewrote the UNIX kernel in C - this was a big step forwards in terms of the system's portability - and released the Fifth Edition of UNIX to universities in 1974. The Seventh Edition, released in 1978, marked a in UNIX development into two main branches: SYSV (System 5) and BSD (Berkeley Software Distribution). BSD arose from the University of California at Berkeley where Ken Thompson spent a sabbatical year. Its development was continued by students at Berkeley and other research institutions. SYSV was developed by AT&T and other commercial companies. UNIX flavors based on SYSV have traditionally been conservative, but better supported than BSD-based flavors. Until recently, UNIX standards were nearly as numerous as its variants. In early days, AT&T published a document called System V Interface Definition (SVID). X/OPEN (now The Open Group), a consortium of vendors and users, had one too, in the X/Open Portability Guide (XPG). In the US, yet another set of standards, named Portable Operating System Interface for Environments (POSIX), were developed at the behest of the Institution of Electrical and Electronics Engineers (IEEE). In 1998, X/OPEN and IEEE undertook an ambitious program of unifying the two standards. In 2001, this joint initiative resulted in a single specification called the Single UNIX Specification, Version 3 (SUSV3), that is also known as IEEE 1003.1:2001 (POSIX.1). In 2002, the International Organization for Standardization (ISO) approved SUSV3 and IEEE 1003.1:2001.

Some of the commercial UNIX based on system V are:

IBM's AIX Hewlett-Packard's HPUX SCO's Open Server Release 5 Silicon Graphics' IRIS DEC's Digital UNIX Sun Microsystems' Solaris 2

Some of the commercial UNIX based on BSD are:

2 CM_aps SunOS 4.1.X (now Solaris) DEC's Ultris BSD/OS, 4.4BSD

Some Free UNIX are:

2 Features of Unix 1) Multiuser • UNIX is a multiprogramming system. • Multiple users can access the system by connecting to points known as terminals. • Several users can run multiple programs simultaneously on one system. 2) Multitasking • UNIX is a multitasking system. • A single user can also run multiple tasks concurrently. • For example: At the same time, a user can → edit a file → print another file one on the printer → send email to a friend and → browse www • The kernel is designed to handle a user's multiple needs. • In a multitasking environment, a user sees one job running in the foreground; the rest run in the background. • User can switch jobs between background and foreground, suspend, or even terminate them. 3) Pattern Matching • UNIX has very sophisticated pattern matching features. • Regular Expressions are a feature of UNIX. • Regular Expressions describe a pattern to match, a sequence of characters, not words, within a line of text. 4) Portable • UNIX can be installed on many hardware platforms. • Unix operating system is written in C language, hence it is more portable than other operating systems. 5) UNIX Toolkit • UNIX offers facility to add and remove many applications as and when required. • Tools include → general purpose tools → text manipulation tools → compilers/interpreters

3 CM_aps → networked applications and → system administration tools 6) Programming Facility • The UNIX shell is also a programming language; it was designed for programmer, not for end user. • It has all the necessary ingredients, like control structures, loops and variables, that establish powerful programming language. • This features are used to design shell scripts – programs that can also invoke UNIX commands. • Many of the system's functions can be controlled and automated by using these shell scripts. 7) Documentation • The principal on-line facility available is the man command, remains the important references for commands and their configuration files. • Apart from the man documentation, there's a vast ocean of UNIX resources available on the Internet. 3 Unix Architecture (Components) • The UNIX operating system (OS) consists of a kernel layer, a shell layer , an application layer & files.

Figure 1.2 : Unix Architecture

Kernel • The kernel is the heart of the operating system. • It interacts with the machine’s hardware. • It is a collection of routines written in C. • It is loaded into memory when the system is booted. • Main responsibilities: 1) Memory management 2) Process management (using commands: , , ) 3) File management (using commands: , , , , ) • To access the hardware, user programs use the services of the kernel via system calls. System Call ➢ A system call is a request for the operating system to do something on behalf of the user's program. ➢ The system calls are functions used in the kernel itself. ➢ To the programmer, the system call appears as a normal C function call.

4 CM_aps ➢ UNIX system calls are used to manage the file system and control processes. ➢ Example: read(), open(), close(), fork(), exec(), exit() • There can be only one kernel running on the system. Shell • The shell interacts with the user. • The shell is a command line interpreter (CLI). • Main responsibilities: 1) interprets the commands the user types in and 2) dispatches the command to the kernel for execution • There can be several shells in action, one for each user who’s logged in. • There are multiple shells that are used by the UNIX OS. • For example: Bourne shell (sh), the C shell (csh), the Korn shell (ksh) and Bourne Again shell (bash). Application • This layer includes the commands, word processors, graphic programs and database management programs. File • A file is an array of bytes that stores information. • All the data of Unix is organized into files. • All files are then organized into directories. • These directories are further organized into a -like structure called the filesystem.

4 TYPES OF SHELL The shell provides you with an interface to the UNIX system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output. A shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of shells, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

Shell Prompt: The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can a command. The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words. Following is a simple example of date command which displays current date and time: $date Thu Jun 25 08:30:19 MST 2009 You can customize your command prompt using PS1 explained in Environment tutorial.

Shell Types: In UNIX there are two major types of shells: 1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character. 2. The C shell. If you are using a C-type shell, the default prompt is the % character. There are again various subcategories for Bourne Shell which are listed as follows:

5 CM_aps • Bourne shell ( sh) • Korn shell ( ksh) • Bourne Again shell ( bash) • POSIX shell ( sh) The different C-type shells follow: • C shell ( csh) • TENEX/TOPS C shell ( tcsh) The original UNIX shell was written in the mid-1970s by Stephen R. Bourne while he was at AT&T Bell Labs in New Jersey. The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as "the shell". The Bourne shell is usually installed as /bin/sh on most versions of UNIX. For this reason, it is the shell of choice for writing scripts to use on several different versions of UNIX.

5 Internal and External Commands • Some commands are implemented as part of the shell itself rather than separate executable files. Such commands that are built-in are called internal commands. • If the command (file) has an independence existence in the /bin directory, it is called external command. • Examples: $ type // echo is an internal command echo is shell built-in $ type ls // ls is an external command ls is /bin/ls • If the command exists both as an internal and external one, shell execute internal command only. Internal commands will have priority compare to external command of same name. type • The UNIX is command based system i.e.,- things happens because the user enters commands in. • Usually, UNIX commands are than 5 characters long. • All UNIX commands are single words like – cat, ls, , date, mkdir, rmdir, , grep etc. • The command names are all in lowercase. • Example: $ LS bash: LS: command not found • All UNIX commands are files containing programs, mainly written in C. • All UNIX commands(files) are stored in directories(folders). • If you want to know the location of executable program (or command), use type command. • Example: $ type ls ls is /bin/ls • When you execute ls command, the shell locates this file in the /bin directory and makes arrangements to execute it. Command Structure • UNIX commands take the following general < form: verb [options] [arguments] > where verb is the command name that can take a set of optional options and one or more optional arguments. • Commands, options and arguments have to be separated by spaces or tabs to enable the shell to interpret them as words.

6 CM_aps • A contiguous string of spaces and tabs together is called a whitespace. • The shell compresses multiple occurrences of whitespace into a single whitespace.

Options • An option is preceded by a minus sign (-) to distinguish it from filenames. • Example: $ ls –l // -l option list all the attributes of the file note • There must not be any whitespaces between – and l. • Options are also arguments, but given a special name because they are predetermined. • Options can be normally combined with only one – sign. thus $ ls –l –a –t is same as $ ls –lat • Because UNIX was developed by people who had their own ideas as to what options should look like, there will be variations in the options. • Some commands use + as an option prefix instead of -.

Filename Arguments • Many UNIX commands use a filename as argument so that the command can take input from the file. • If a command uses a filename as argument, it will usually be the last argument, after all options. • Example: ls -lat chap01 chap02 chap03 # Multiple filenames as arguments file1 file2 file3 dest_dir rm file1 file2 file3 • The command with its arguments and options is known as the command line. • This line can be considered complete only after the user has hit [Enter]. • The complete line is then fed to the shell as its input for interpretation and execution.

Exceptions • There are some commands that don't accept any arguments. • There are also some commands that may or may not be specified with arguments. • For Example: The ls command can run → without arguments (ls) → with only options (ls –l) → with only filenames (ls f1 f2), or → using a combination of both (ls –l f1 f2). • There are some commands compulsorily take options (). • There are some commands which can take an expression as an argument, or a set of instructions as argument. : grep, Understanding of Some Basic Commands echo • This command can be used to display a message on the terminal. • Example: $ echo "Welcome to UNIX \n" Welcome to UNIX • This command can be used with following escape characters: "\a" Audible Alert (Bell) "\b‟ Back Space "\f‟ Form Feed

7 CM_aps "\n‟ New Line "\r‟ Carriage Return "\t‟ Horizontal Tab "\v‟ Vertical Tab "\\‟ Backslash

• This command can be used to display a message on the terminal. • This command can be used with following escape characters: "\a" Audible Alert (Bell) "\b‟ Back Space "\f‟ Form Feed "\n‟ New Line "\r‟ Carriage Return "\t‟ Horizontal Tab "\v‟ Vertical Tab "\\‟ Backslash • Example: $ printf "Welcome to UNIX \n" Welcome to UNIX • Similar to C language, this command can be used with following format specifiers: %d Decimal integer %f Floating point number %s String %o Octal integer (base 8) %x Hexadecimal integer (base 16) • Syntax: printf("format-string", variable-list); where format-string contains one or more format-specifiers variable-list contains names of variables • Example: $ printf "My current shell is %s\n" $SHELL My current shell is /bin/ksh

ls • ls command can be used to obtain a list of all filenames in the current directory. • -l option can be used to obtain a detailed list of attributes of all files in the current directory. • Example: $ ls –l Type & Perm Owner Group Size Date & Time File Name -rwxr-xr-- 1 kumar metal 195 may 10 13:45 chap01 drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir who • This command can be used to display the details of all the users logged-in into the unix system at the

8 CM_aps same time. • Example: $ who kumar tty0 Oct 8 14:10 rama tty2 Oct 4 09:08 • –H option can be used to display the header information. • Example: $ who NAME LINE TIME COMMENT kumar tty0 Oct 8 14:10 rama tty2 Oct 4 09:08 • -u option can be used to display detailed information of users. • Example: $ who -Hu NAME LINE TIME IDLE PID COMMENT kumar tty0 Oct 8 14:10 00:18 185 rama tty2 Oct 4 09:08 00:23 123

date • This command can be used to display the current date and time. • Example: $ date Mon Sep 4 16:40:02 IST 2017 • This command can also be used with suitable format specifiers as arguments. • Following are some format specifiers: d – day of month (1 - 31) m - Month (01-12) y – last two digits of the year. H– hour (00-24) M – minute (00-59) S – second (00-59) D – date in the format mm//yy T – time in the format hh:mm:ss • Syntax: $ date +%format_specifier • Example: $ date +"%d-%m-%y" 04-09-17 $ date +%m // displays month number 09

• This command can be used to change user password. • All Unix systems require passwords to help ensure that your files and data remain secure from hackers. • Following are the steps to change your password – 1) To start, type password at the command prompt as shown below. 2) Enter your old password, the one you're currently using. 3) Type in your new password.

9 CM_aps 4) You must verify the password by typing it again. $ passwd Changing password for kumar (current) Unix password: ***** New Unix password: ***** Retype new Unix password: ***** passwd: all authentication tokens updated successfully

• This command can be used to display the calendar of the current month. • Syntax: cal [ [ month] year ] • Example: $ cal September 2017 Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 • This command can also be used to display the calendar of any specific month or a complete year. • Example: $ cal 9 2017 September 2017 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 • You can't hold the calendar of a year in a single screen page; it scrolls off rapidly to end of the year. • To pause at each screen page, a pipe "|" can be connected to more pager: • Example: $cal 2017 | more

6 UNIX Files • A file is the container for storing information. • The file doesn't store 1) file-size and 2) file-name. • Some attributes of file are file-type, permissions, links, owner, group-owner etc. • These file attributes are stored in inode table which is accessible only to kernel. • A UNIX system makes no difference between a file and a directory, since a directory is just a file containing names of other files. • Programs, services, texts, images etc are considered to be files. • Generally, all devices including I/O devices are also considered to be files.

Naming Files • A filename can consist up to 255 characters. • Files may or may not have extensions. • Files consist of any ASCII character expect the "/" and NULL character.

10 CM_aps • Following is recommended for filenames: → Alphabetic characters and numerals → period(.), hyphen(-) and underscore(_) • However, users are permitted to use control characters or other unprintable characters in a filename. Example: .last_time list. @ # $ % * abcd a.b.c.d.e • UNIX is case sensitive; chap01, Chap01 and CHAP01 are three different filenames. Basic File Types • Three categories of files are: 1) Ordinary file (or regular file) – It contains only data as a stream of characters. 2) Directory file – it contains files and other sub-directories. 3) Device file – all devices and peripherals are represented by files.

Ordinary (Regular) File • An ordinary file is a file on the system that contains data, text, or program instructions. • An ordinary file can be either a text file or a binary file. 1) A text file contains only printable characters and you can view and edit them. ➢ Examples: All C and Java program sources, shell scripts are text files. ➢ Every line of a text file is terminated with the newline character. 2) A binary file contains both printable and non printable characters that cover the entire ASCII range. ➢ Examples: Most Unix commands, executable files, pictures, sound and video files are binary files. Hidden Files ➢ An invisible file is one, the first character of which is the or the period character (.). ➢ Unix programs (including the shell) use most of these files to store configuration information. ➢ Some common examples of the hidden files include the files: .profile − The Bourne shell ( sh) initialization script. .kshrc − The Korn shell ( ksh) initialization script. .cshrc − The C shell ( csh) initialization script. .rhosts − The remote shell configuration file.

Directory File • A directory contains no data, but keeps details of the files and subdirectories that it contains. • A directory file contains one entry for every file and subdirectory that it houses. • Each entry has two components namely, 1) filename and 2) unique identification number of the file or directory (called the inode number). • When you create or remove a file, the kernel automatically updates its corresponding directory by adding or removing the entry (filename and inode number) associated with the file. • Unix directories are equivalent to windows folders.

Device File • All the operations on the devices are performed by reading or writing the file representing the device. • Advantage of device file is that some of the commands used to access an ordinary file also work with device file. • Device filenames are generally found in a single directory structure, /dev. • A device file is not really a stream of characters. • It is the attributes of the file that entirely govern the operation of the device. • The kernel identifies a de ice from its attributes and uses them to operate the device.

11 CM_aps Parent Child Relationship • All data in Unix is organized into files. • All files are organized into directories. • These directories are organized into a tree-like structure called the filesystem.

Figure 2.4: Parent Child Relationship • The feature of UNIX file system is that there is a top, which serves as the reference point for all files. • This top is called root (represented by a "/"). • The root is actually a directory. • The root directory (/) has a number of subdirectories under it. • The subdirectories in turn have more subdirectories and other files under them. • Every file apart from root, must have a parent, and it should be possible to trace the ultimate parentage of a file to root. • In parent-child relationship, the parent is always a directory.

Standard Directories (UNIX Filesystem) • Following are the directories that exist on the major versions of Unix 1) / (root directory) • This contains only the directories needed at the top level of the file structure. 2) /bin • This contains common programs, shared by the system, the system administrator and the users. 3) /dev • This contains references to all the CPU peripheral hardware, which are represented as files with special properties. These are device drivers. 4) /etc • This contains system configuration files. 5) /home • This contains home directories of the common users. 6) /lib • This contains library files, including files for all kinds of programs needed by system and users. 7) /sbin • This contains programs for use by the system and the system administrator. 8) /tmp • This contains temporary space for use by the system. This space is cleaned upon reboot. So, don't use this for saving any work. 9) /usr • This contains programs, libraries, documentation etc. for all user-related programs.

12 CM_aps 10) /var • This contains storage for all variable files and temporary files created by users, such as → log files → queue or → print spooler area HOME Variable • When a user logs into the system, the user will be placed in a directory called home directory. • Home directory is created by the system when the user account is created. • The shell-variable HOME indicates the home directory of the current user. • This variable is set for a user by the system admin in /etc/passwd. • Example: $ echo $HOME /home/kumar • Here, absolute pathname is shown. • Absolute pathname is a sequence of directory names starting from root (/). • The subsequent slashes are used to separate the directories.

PATH Variable • This variable specifies the locations in which the shell should look for commands. • Example: $ echo $PATH /bin: /usr/bin: • When you specify a command like date, the system will the associated file from a list of directories specified in the PATH variable and then executes it. • The PATH variable also includes the current directory. • Whenever you enter any UNIX command, you are actually specifying the name of an executable file located somewhere on the system. • The system goes through the following steps in order to determine which program to execute: 1) Built in commands (such as cd and history) are executed within the shell. 2) If an absolute path name (such as /bin/ls) or a relative path name (such as ./myprog), the system executes the program from the specified directory. 3) Otherwise, the PATH variable is used. Relative and Absolute Pathname • A pathname is a text string made up of one or more names separated by a "/". • A pathname specifies how to traverse (navigate) the hierarchical directory names in the file system to reach some destination object. Absolute Pathname • An absolute pathname begins with a slash (/). • The Absolute path defines the location of a Directory or a file from the root file system (/). • The absolute path contains the full path to the directory or file. Relative Pathname • The relative pathname do not begin with "/". • It specify the location relative to your current working directory. 1) . (a single dot) - This represents the current directory. 2) .. (two dots) - This represents the parent directory. • Example: • 'date' command can executed in two ways as follows: Using Absolute Pathname Using Relative Pathname $ /bin/date $ date Thu Sep 7 10:20:29 IST 2017 Thu Sep 7 10:20:29 IST 2017

13 CM_aps • Example: • P1.java can be copied to kumar under home directory in two ways as follows: Using Absolute Pathname Using Relative Pathname $ pwd $ pwd /home/kumar /home/kumar $ cp /home/sharma/P1.java /home/kumar $ cp /home/sharma/progs .

• Example: • cd & mkdir can be used in two ways as follows: Using Absolute Pathname Using Relative Pathname $ pwd $ pwd /home/kumar /home/kumar $ mkdir /home/kumar/progs $ mkdir progs $ cd /home/kumar/progs $ cd progs $ pwd $ pwd /home/kumar/progs /home/kumar/progs $ cd /home/kumar $ cd .. $ pwd $ pwd /home/kumar /home/kumar 8 Directory Commands pwd (print working directory) • This command can be used to display the current working directory. • Example: $ pwd /home/kumar • Like HOME, it displays the absolute path.

cd • This command can be used to change the current working directory. • Syntax cd PATHNAME Case 1: • This command can be used with the argument. • This command can work with both absolute and relative path names. • Example: Using relative pathname Using absolute pathname $ pwd $ pwd /home/kumar /home/kumar $ mkdir progs $ mkdir /home/kumar/progs $ cd progs $ cd /home/kumar/progs $ pwd $ pwd /home/kumar/progs /home/kumar/progs Case 2: • This command can also be used without the argument. • When used without argument, this command changes the working directory to home directory. • Example: $ pwd /home/kumar/progs $ cd $ pwd /home/kumar

14 CM_aps

Case 3: • This command can also be used with short hand notations. • Example: $ cd / // changes the working directory to root directory (/) $ cd .. // changes the working directory to the one level up parent directory (..) $ cd ../.. // changes the working directory to the two level up parent directory (../..) mkdir • This command can be used to create a new directory. • Syntax: mkdir DIRECTORY_NAME • This command can work with both absolute and relative path names. • Example: Using relative pathname Using absolute pathname $ pwd $ pwd /home/kumar /home/kumar $ mkdir progs $ mkdir /home/kumar/progs $ cd progs $ cd /home/kumar/progs $ pwd $ pwd /home/kumar/progs /home/kumar/progs Case 2: • This command can also accept more than one directory name as arguments. • Example: $mkdir usp ade dms // creates 3 directories under current directory $mkdir sem3 sem3/usp sem3/ade // creates 3 subdirectories – sem3, usp, ade • The order of specifying arguments is important. You cannot create subdirectories before creation of parent directory. • System refuses to create a directory due to following reasons: 1) User doesn‘t have permission to create directory. (i.e. directory protected). 2) The directory already exists. 3) There may be ordinary file by that name in the current directory.

rmdir • This command can be used to delete a directory. • Syntax: rmdir DIRECTORY_NAME Case 1: • Example: $ rmdir usp // delete usp directory under current directory Case 2: • This command can also accept more than one directory name as arguments. • Example: $ mkdir sem3 sem3/usp sem3/ade // creates 3 subdirectories – sem3, usp, ade $ rmdir sem3/ade sem3/usp sem3 // deletes 3 subdirectories – sem3, usp, ade

15 CM_aps • Here, rmdir expects the arguments to be reverse of mkdir's arguments. • The order of specifying arguments is important. You cannot delete parent directories before deletion of subdirectories. • System refuses to delete a directory due to following reasons: 1) User doesn‘t have permission to delete directory. (i.e. write protected directory). 2) The directory doesn‘t exist in system. 3) The directory is your present working directory.

8 File Related Commands cat • This command can be used to display the content of a file on the terminal. • Syntax: cat FILENAME Case 1: • Example: $ cat P1.c WELCOME TO UNIX // contents of P1.c $ cat P2.c WELCOME TO PERL // contents of P2.c Case 2: • This command can also accept more than one filename as arguments. • Example: $ cat P1.c P2.c WELCOME TO UNIX // contents of P1.c WELCOME TO PERL // contents of P2.c • Here, the content of the second file is shown immediately after the first file. • So, this command concatenates two files- hence its name (cat). Case 3: • This command can also be used to create a new file. • Syntax: cat > FILENAME • Example: $ cat > P3.c WELCOME TO SHELL // contents of P3.c [ctrl-d] // Terminates P3.c $ cat P3.c WELCOME TO SHELL // contents of P3.c

cat Options 1) Displaying Non-printing Characters (-v) • By default, without any option, this command displays only printing ASCII characters of the file. • -v option can be used to display even non-printing ASCII characters of the file. 2) Numbering Lines (-n) • -n option can be used to number the lines of the file.

16 CM_aps • This option helps the programmer in debugging programs. • This command can be used to get a count of the total number of lines, words, and characters contained in a file. • Syntax: wc FILENAME Case 1: • Example: $ cat P1.c WELCOME // contents of P1.c TO UNIX $ wc P1.c LINE WORD CHARATCTER FILENAME 3 3 15 P1.c

• The header includes the following attributes: 1) LINE • This represents the total number of lines in the file. 2) WORD • This represents the total number of words in the file (excluding space, tab and newline). 3) CHARATCTER • This represents the total number of characters in the file (including space, tab and newline). 4) FILENAME • This represents the name of the file. Case 2: • This command can also accept more than one filename as arguments. • Example: $ wc P1.c P 3 3 2.c 15 P1.c 3 3 15 P2.c

wc Options –l option can be used to count only number of lines – option can be used to count only number of words –c option can be used to count only number of characters • Example: $ wc -l P1.c 3 P1.c $ wc -c P1.c 15 P1.c cp • This command is used to copy a file(s) from one location to another location. • It creates an exact image of the file on the disk with a different name. • Syntax:

17 CM_aps cp SOURCE_FILE DESTINATION_FILE Case 1: • This command can be used to copy a file within current working directory. • Example: $ cp FILE1 FILE2 // copies contents of FILE1 to FILE2 in current working directory • Here, 1) If the destination file doesn‘t exist, it will first be created before copying takes place. 2) If the destination file exists, it will be overwritten without any warning from the system. Case 2: • This command can also be used to copy a file to the another directory. • Example: $ cp FILE1 part2/FILE2 • Here, this copies the file FILE1 from your current working directory to the file FILE2 in the subdirectory "part2". Case 3: • This command can also be used with .(dot) to signify the current directory as the destination. • Example: $ cp part2/FILE2 . same as $ cp part2/FILE2 FILE2 • Here, this copies the file FILE2 in the subdirectory "part2" to your current working directory. Case 4: • This command can also accept more than 2 filenames as arguments. • In this case, the last filename must be a directory. • Example: $ cp FILE1 FILE2 FILE3 module // copies 3 files to "module" directory • Here, "module" directory should already exist because cp cannot create a directory. Case 5: • This command can also be used with metacharcters (* or ?). • Example: cp *.pdf DIR1 //copies all the files with extensions .pdf to directory DIR1

cp Options 1) Interactive Copying (-i) • –i option can be used to warn the user before overwriting the destination file. • Example: $ cp –i FILE1 FILE2 // copies contents of FILE1 to FILE2 if "Y" is entered $ cp: overwrite FILE2 (/no)? Y 2) Recursive Copying (-R) • –R option can be used to recursively copy an entire directory structure from one location to another location. • Entire directory including all files in its subdirectories will be copied. • Example: cp –R DIR1 DIR2

18 CM_aps • If directory DIR2 doesn‘t exist, cp creates it along with the associated subdirectories. • This command renames or moves files. Case 1: • This command can be used to rename a file in the current directory. • Syntax: mv OLDFILENAME NEWFILENAME • It doesn't create a copy of the file; it merely renames it. • No additional space is consumed on disk during renaming. • Example: $ mv OLDFILE NEWFILE // renames the OLDFILE by NEWFILE Case 2: • This command can also be used to move a group of files to a directory. • In this case, the last filename must be a directory. • Example: $ mv FILE1 FILE2 FILE3 module // moves 3 files to "module" directory

rm • This command can be used to delete a file. • Syntax: rm FILENAME • Example: $ rm FILE1 // deletes FILE1 $ rm FILE1 FILE2 FILE3 // deletes three files $ rm *.pdf // deletes all files with extensions .pdf in the currant directory

rm Options 1) Interactive Deletion (-i) • –i option can be used to warn the user before deleting the file. • Example: $ rm –i FILE1 FILE2 // delete FILE1 & FILE2 if "Y" is entered

rm: remove FILE1 (yes/no)? ? Y rm: 2) Recursiveremove Deletion FILE2 (yes/no)? (-r or - ?R) Y • –R option can be used to recursively delete an entire directory structure. • Entire directory including all files in its subdirectories will be deleted. • Example: $ rm –r DIR1 // delete DIR1 and all its subdirectories & files $ rm -r * // deletes all files in the current directory and all its subdirectories. 3) Forceful Deletion (-f) • By default, this command cannot delete a file which is write-protected. • –f option can be used to delete even the write-protected file.

19 CM_aps • Example: rm -rf * // deletes all files in the current directory and all its subdirectories. 9 File Attributes and Permissions and Knowing them • ls command can be used to obtain a list of all filenames in the current directory. • -l option can be used to obtain a detailed list of attributes of all files in the current directory. • For example: $ ls -l Type & Perm Link Owner Group Size Date & Time File Name -rwxr-xr-- 1 kumar metal 195 may 10 13:45 chap01 drwxr-xr-x 2 kumar metal 512 may 09 12:55 Helpdir • The header includes the following attributes: 1) Type & Perm • This represents the file-type and the permission given on the file. File-Type ➢ The first character indicates type of the file as shown below: i) hyphen (-) for regular file ii) d for Directory file iii) l for Symbolic link file iv) b for block special file v) c for character special file File Permission ➢ The remaining 9 characters indicates permission of the file. ➢ There are 3 permissions: read (r), write (w), execute (x). i) Read: Grants the capability to read, i.e., view the contents of the file. ii) Write: Grants the capability to modify, or remove the content of the file. iii) Execute: User with execute permissions can run a file as a program. ➢ There are 3 types of users: owner, groups and others. ➢ The permission is broken into group of 3 characters: i) The first 3 characters (2-4) represent the permissions for the file's owner. ii) The middle 3 characters (5-7) represent the permissions for the group to which the file belongs. iii) The last 3 characters (8-10) represents the permissions for everyone else. ➢ Consider an example -rwxr-xr-- i) Owner has read (r), write (w) and execute (x) permission. ii) Group members have read (r) and execute (x) permission, but no write permission. iii) Others have read (r) only permission. 2) Link • This indicates the number of file names maintained by the system. • This does not mean that there are so many copies of the file. (Link similar to pointer). 3) Owner • This represents the owner of the file. This is the user who created this file. 4) Group • This represents the group of the owner. • Each group member can access the file depending on the permission assigned. • The privileges of the group are set by the owner of the file and not by the group members. • When the system admin creates a user account, he has to assign these parameters to the user: i) The user-id (UID) and ii) The group-id (GID) 5) Size • This represents the file size in bytes. • It is the number of character in the file rather than the actual size occupied on disk. 6) Date & Time • This represents the last modification date and the time of the file.

20 CM_aps • If you change only the permissions /ownership of the file, the modification time remains unchanged. • If at least one character is added/removed from the file then this field will be updated. 7) File name • This represents the file or the directory name. ls • This command can be used to list the files and directories stored in the current directory. • Syntax: ls [options] [argument] • For example: $ ls bin lib users work • With options it can provide information about the size, type of file, permissions, dates of file creation, change and access.

ls Options • -l option can be used to get more information about the listed files. • When no argument is used, the listing will be of the current directory. • There are many very useful options for the ls command. • A listing of many of them follows. • When using the command, string the desired options together preceded by "-". a Lists all files, including those beginning with a dot (.). d Lists only names of directories, not the files in the directory F Indicates type of entry with a trailing symbol: i) executables with * ii) directories with / and iii) symbolic links ith @ R Recursive list u Sorts filenames by last access time t Sorts filenames by last modification time i Displays inode number l Long listing: lists the mode, link information, owner, size, last modification (time). If the file is a symbolic link, an arrow (→) precedes the pathname of the linked to file. • The mode field is given by the -l option and consists of 10 characters. • The first character is one of the following: Character If entry is a d directory - plain file b block-type special file c character-type special file l symbolic link

21 CM_aps s socket • The next 9 characters are in 3 sets of 3 characters each. • They indicate the file access permissions: 1) the first 3 characters refer to the permissions for the user 2) the next three for the users in the Unix group assigned to the file, and 3) the last 3 to the permissions for other users on the system. • Designations are as follows: r read permission w write permission x execute permission - no permission • Example: $ ls -al // To get a long listing of all files in a directory total 24 drwxr-sr-x 5 workshop acs 512 J n 7 11:12 . drwxr-xr-x 6 root sys 512 May 29 09:59 .. -rwxr-xr-x 1 workshop acs 532 May 20 15:31 .cshrc -rwxr-xr-x 1 workshop acs 238 May 14 09:44 .login -rw-r--r-- 1 workshop acs 273 May 22 23:53 .plan

Listing Directory Attributes • $ls -d : can be used to list only names of directories, not the files in the directory. • However, this command will not list all subdirectories in the current directory. • For example: $ls –ld helpdir progs drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs • Directories are easily identified in the listing by the first character(d) of the first column.

9 Changing File Permissions • A file is created with a default set of permission. • command can be used to change permission of a file. • This command can be used in two ways: 1) Relative mode and 2) Absolute mode.

Relative Permissions • This command can be used to add/delete permission for specific type of user (owner, group or others). • This command can be used to → change only those permissions specified in the command line and → leave the other permissions unchanged. • Syntax: chmod category operation permission filename • This command takes 4 arguments: 1) category can be u → user (owner)

22 CM_aps g → group o → others a → all (ugo) 2) operation can be + → assign - → remove = → absolute 3) permission can be r → read w → write x → execute 4) Filename whose permission has to changed • Example: $ ls -l xstart -rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart $ chmod u+x xstart // user (u) is added(+) an execute(x) permission $ ls -l xstart -rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart $ chmod ugo+x xstart OR chmod a+x xstart // all(a) are added(+) an execute(x) permission $ ls –l xstart -rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart $ chmod go-r xstart // group(g) & others(o) are removed(-) a read(r) permission $ls –l xstart -rwx—x--x 1 kumar metal 1906 sep 23:38 xstart • This command can also accept multiple file names. • Example: $ chmod u+x note1 note2 note3

Recursively Changing File Permissions • chmod command can be used to change recursively permission of all the files and subdirectories found in the current directory. • Example: chmod -R a+x c_progs // current directory c_progs Here, all files and subdirectories are made executable for all users in current directory c_progs. Absolute Permissions • This command can be used to add/delete permission for all type of users (owner, group or others). • This command can be used to change all permissions specified in the command line. • Syntax: chmod octal_value filename • This command takes 2 arguments: 1) octal_value contains 3 octal digits to represent 3 type of users (owner, group or others). 1) First digit is for user 2) Second digit is for group and 3) Third digit is for others Each digit represents a permission as shown below: 4 (100) – read only

23 CM_aps 2 (010) – write only 1 (001) - execute only 6 (110) – read & write only For ex: octal_value of 644(110 100 100) means → user can read & write only → group can read only → others can read only 2) Filename whose permission has to changed. • Example: $ ls -l xstart // current permission 644 $-rw chmod-r—r-- 744 1 kumar xstart metal 1906 sep 23:38same xstartas $ chmod u+x xstart

// user(u) is added(+) an execute(x) permission

$ ls –l xstart

-rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart

$ chmod 755 xstart same as $ chmod a+x xstart

// all(a) are added(+) an execute(x) permission

$ ls –l xstart • It is the directory permissions that determine whether a file can be deleted or not. -rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart i) 777 signify all permissions for all categories, but still we can prevent a file from being deleted. ii) $000 chmod signifies 711 xstart absence of all permissionssame as for all categories,$ chmod go bu-rt xstart still we can delete a file. • The system administrator can change the file permissions of every user. // group(g) & others(o) are removed(-) a read(r) permission i) Only owner can change the file permissions. $lsii) User–l xstart cannot change other user‘s file‘s permissions.

-rwx—x--x 1 kumar metal 1906 sep 23:38 xstart Relative Permissions Absolute Permissions $ ls -l xstart $ ls -l xstart -rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart -rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart $ chmod u+x xstart $ chmod 744 xstart $ ls -l xstart $ ls -l xstart -rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart -rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart $ chmod ugo+x xstart OR chmod a+x xstart $ chmod 755 xstart OR chmod a+x xstart $ ls –l xstart $ ls –l xstart -rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart -rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart $ chmod go-r xstart $ chmod 711 xstart $ls –l xstart $ls –l xstart -rwx—x--x 1 kumar metal 1906 sep 23:38 xstart -rwx—x--x 1 kumar metal 1906 sep 23:38 xstart 10 Changing File Ownership • While creating an account on Unix, it assigns a owner ID and a group ID to each user.

24 CM_aps • The permissions are assigned to each user based on the Owner and the Groups. • Two commands are available to change the owner and the group of files: 1) (change owner) is used to change the owner of a file. 2) (change group) is used to change the group of a file.

chown • This command can be used to change the ownership of a file. • Syntax: chown USERNAME FILENAME • The value of the user can be the name or uid(user id) of a user on the system. • Example: $ ls -l note -rwxr----x 1 kumar ISE 347 may 10 20:30 note $ chown sharma note //now sharma becomes new owner of file "note" ls -l note -rwxr----x 1 sharma ISE 347 may 10 20:30 note • Now, new owner will have same file permissions as that of old owner. • Old owner cannot edit "note" since there is no write privilege for group and others. • Old owner cannot get back the ownership. • Only super user can change the ownership of any file. • But normal users can change the ownership of only those files that they own.

chgrp • This command can be used to change the group-ownership of a file. • Syntax: chgrp GROUPNAME FILENAME • The value of the group can be the name or GID(group ID) of a group on the system. • Example: $ ls -l note

-rwxr----x 1 kumar ISE 347 may 10 20:30 note//now CSE becomes new group-owner of file "note"

$ chgrp CSE note ls -l -rwxr----x 1 kumar CSE 347 may 10 20:30 note • No supernote user permission is required.

11 Directory Permissions • The default permissions of a directory are: rwxr-xr-x (755). 1) Read directory permission grants the ability to view a file. 2) Write directory permission grants the ability to add, change or remove files from the directory. 3) Execute directory permission grants the ability to list (ls) the directory content or search () for files in the directory. • Example:

25 CM_aps $mkdir c_progs $ls –ld c_progs drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs • A directory must never be writable by group and others .

12 I/O REDIRECTION

• Most Unix system commands take input from your terminal and send the resulting output back to your terminal. A command normally reads its input from the standard input, which happens to be your terminal by default. Similarly, a command normally writes its output to standard output, which is again your terminal by default.

• Output Redirection • The output from a command normally intended for standard output can be easily diverted to a file instead. This capability is known as output redirection.

• If the notation > file is appended to any command that normally writes its output to standard output, the output of that command will be written to file instead of your terminal.

• Check the following who command which redirects the complete output of the command in the users file.

• $ who > users

• Notice that no output appears at the terminal. This is because the output has been redirected from the default standard output device (the terminal) into the specified file. You can check the users file for the complete content −

• $ cat users • oko tty01 Sep 12 07:30 • ai tty15 Sep 12 13:32 • ruth tty21 Sep 12 10:10 • pat tty24 Sep 12 13:07 • steve tty25 Sep 12 13:03 • $

• If a command has its output redirected to a file and the file already contains some data, that data will be lost. Consider the following example −

• $ echo line 1 > users • $ cat users • line 1 • $

26 CM_aps • You can use >> operator to append the output in an existing file as follows −

• $ echo line 2 >> users • $ cat users • line 1 • line 2 • $

• Input Redirection • Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character > is used for output redirection, the less-than character < is used to redirect the input of a command.

• The commands that normally take their input from the standard input can have their input redirected from a file in this manner. For example, to count the number of lines in the file usersgenerated above, you can execute the command as follows −

• $ wc -l users • 2 users • $

• Upon execution, you will receive the following output. You can count the number of lines in the file by redirecting the standard input of the wc command from the file users −

• $ wc -l < users • 2 • $

• Note that there is a difference in the output produced by the two forms of the wc command. In the first case, the name of the file users is listed with the line count; in the second case, it is not.

• In the first case, wc knows that it is reading its input from the file users. In the second case, it only knows that it is reading its input from standard input so it does not display file name. 13 Unix - Pipes and Filters You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe.

To a pipe, put a vertical bar (|) on the command line between two commands.

27 CM_aps When a program takes its input from another program, it performs some operation on that input, and writes the result to the standard output. It is referred to as a filter. The grep Command The grep command searches a file or files for lines that have a certain pattern. The syntax is −

$grep pattern file(s) The name "grep" comes from the (a Unix line editor) command g/re/p which means “globally search for a regular expression and print all lines containing it”.

A regular expression is either some plain text (a word, for example) and/or special characters used for pattern matching.

The simplest use of grep is to look for a pattern consisting of a single word. It can be used in a pipe so that only those lines of the input files containing a given string are sent to the standard output. If you don't give grep a filename to read, it reads its standard input; that's the way all filter programs work −

$ls -l | grep "Aug" -rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02 -rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07 -rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros $ There are various options which you can use along with the grepcommand −

Sr.No. Option & Description

1 -v

Prints all lines that do not match pattern.

2 -n

28 CM_aps Prints the matched line and its line number.

3 -l

Prints only the names of files with matching lines (letter "l")

4 -c

Prints only the count of matching lines.

5 -i

Matches either upper or lowercase.

Let us now use a regular expression that tells grep to find lines with "carol", followed by zero or other characters abbreviated in a regular expression as ".*"), then followed by "Aug".−

Here, we are using the -i option to have case insensitive search −

$ls -l | grep -i "carol.*aug" -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros $ The Command The sort command arranges lines of text alphabetically or numerically. The following example sorts the lines in the food file −

$sort food Afghani Cuisine Bangkok Wok Big Apple Deli Isle of Java

Mandalay Sushi and Sashimi Sweet Tooth Tio Pepe's Peppers $ The sort command arranges lines of text alphabetically by default. There are many options that control the sorting −

29 CM_aps Sr.No. Description

1 -n

Sorts numerically (example: 10 will sort after 2), ignores blanks and tabs.

2 -r

Reverses the order of sort.

3 -f

Sorts upper and lowercase together.

4 +x

Ignores first x fields when sorting.

More than two commands may be linked up into a pipe. Taking a previous pipe example using grep, we can further sort the files modified in August by the order of size.

The following pipe consists of the commands ls, grep, and sort−

$ls -l | grep "Aug" | sort +4n -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros -rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro -rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07 -rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02 $ This pipe sorts all files in your directory modified in August by the order of size, and prints them on the terminal screen. The sort option +4n skips four fields (fields are separated by blanks) then sorts the lines in numeric order. The and more Commands A long output can normally be zipped by you on the screen, but if you run text through more or use the pg command as a filter; the display stops once the screen is full of text.

30 CM_aps Let's assume that you have a long directory listing. To make it easier to read the sorted listing, pipe the output through moreas follows −

$ls -l | grep "Aug" | sort +4n | more -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros -rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro -rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07 -rw-rw-r-- 1 john doc 14827 Aug 9 12:40 ch03 . . . -rw-rw-rw- 1 john doc 16867 Aug 6 15:56 ch05 --More--(74%) The screen will fill up once the screen is full of text consisting of lines sorted by the order of the file size. At the bottom of the screen is the more prompt, where you can type a command to move through the sorted text.

Once you're done with this screen, you can use any of the commands listed in the discussion of the more program.

13 file comparision NAME cmp - Compare two files, and if they differ, tells the first byte and line number where they differ. SYNOPSIS cmp options... FromFile [ToFile] DESCRIPTION 'cmp' reports the differences between two files character by character, instead of line by line. As a result, it is more useful than '' for comparing binary files. For text files, 'cmp' is useful mainly when you want to know only whether two files are identical. For files that are identical, 'cmp' produces no output. When the files differ, by default, 'cmp' outputs the byte offset and line number where the first difference occurs. You can use the '-s' option to suppress that information, so that 'cmp' produces no output and reports whether the files differ using only its exit status. Unlike 'diff', 'cmp' cannot compare directories; it can only compare two files. OPTIONS

Print the differing characters. Display control characters as a '^' -c followed by a letter of the alphabet and precede characters that have the high bit set with 'M-' (which stands for "meta").

31 CM_aps --ignore- Ignore any differences in the the first BYTES bytes of the input initial=BYTES files. Treat files with fewer than BYTES bytes as if they are empty.

-l Print the (decimal) offsets and (octal) values of all differing bytes.

Print the differing characters. Display control characters as a '^' --print-chars followed by a letter of the alphabet and precede characters that have the high bit set with 'M-' (which stands for "meta").

--quiet Do not print anything; only return an exit status indicating whether -s the files differ. --silent

--verbose Print the (decimal) offsets and (octal) values of all differing bytes.

Output the version number of 'cmp'. The file name '-' is always the -v standard input. 'cmp' also uses the standard input if one file name is --version omitted. An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble.

EXAMPLES Compare two files $ cat sample.txt This is a sample text file $ cat sample1.txt This is another sample file $ cmp sample.txt sample1.txt sample.txt sample1.txt differ: byte 10, line 1 - compare two sorted files line by line and write to standard output: the lines that are common, plus the lines that are unique. SYNOPSIS comm [options]... File1 File2 DESCRIPTION Before 'comm' can be used, the input files must be sorted using the collating sequence specified by the 'LC_COLLATE' locale, with trailing newlines significant. If an input file ends in a non-newline character, a newline is silently appended. The 'sort' command with no options always outputs a file that is suitable

32 CM_aps input to 'comm'. With no options, 'comm' produces three column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files. Columns are separated by a single TAB character. The options -1, -2, and -3 suppress printing of the corresponding columns. Unlike some other comparison utilities, 'comm' has an exit status that does not depend on the result of the comparison. Upon normal completion 'comm' produces an exit code of zero. If there is an error it exits with nonzero status. OPTIONS

-1 suppress lines unique to file1.

-2 suppress lines unique to file2.

-3 suppress lines that appear in both files.

EXAMPLES Show the lines unique to words.txt $ cat words.txt Apple Banana Orange India US Canada $ cat countries.txt India US Canada $ comm -23 < (sort words.txt | uniq) < (sort countries.txt | uniq) Apple Banana Orange diff diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.

33 CM_aps The important thing to remember is that diff uses certain special symbols and instructions that are required to make two files identical. It tells you the instructions on how to change the first file to make it match the second file.

Special symbols are: a : add c : change d : delete Syntax : diff [options] File1 File2 Lets say we have two files with names a.txt and b.txt containing 5 Indian states. $ ls a.txt b.txt

$ cat a.txt Gujarat Uttar Pradesh Kolkata Bihar Jammu and Kashmir

$ cat b.txt Tamil Nadu Gujarat Andhra Pradesh Bihar Uttar pradesh Now, applying diff command without any option we get the following output: $ diff a.txt b.txt 0a1 > Tamil Nadu 2,3c3 < Uttar Pradesh Andhra Pradesh 5c5 Uttar pradesh

14 Wildcards in Filenames UNIX allows wildcards in almost all commands -- it is actually a feature of the shell. Caution: UNIX also uses the wildcard characters in pattern matching, but the meaning is only similar, not identical. MPE allows wildcards in the Listf, Store, Restore, and Purge (new feature) commands. UNIX Wildcards Text version. ? any single character, except a leading dot * zero or more characters, except a leading dot [ ] defines a class of characters ( - for range, ! to exclude) UNIX Examples: Text version.

34 CM_aps [abc]?? 3 character filename beginning with "a", "b", or "c". [1-9][A-Z] 2 character filename starting with a number, and ending with an uppercase letter. [!A-Z]?? 3 character filename that does not begin with an uppercase letter. *e[0-9]f any file ending with "e", a single number, and "f" 15 lp printing command The lp command is used to print files on Unix and Linux systems. The name "lp" stands for "line printer". As with most Unix commands there are a fairly large number of options available to enable flexible printing capabilities. Let's look at some lp printing commands examples.

lp printing command examples lp /etc/passwd This command prints the "/etc/passwd" file to the default printer. If you do not use the "-d" option, the file is printed to the default printer destination. lp -dSales .profile This command prints the ".profile" file to the printer named "Sales". The -d option specifies the destination. lp -dSales file1 file2 file3 This command prints the three files "file1", "file2", and "file3" to the printer named "Sales". lp -i Sales-101 -H hold This command places the print request Sales-101 on hold. lp -i Sales-101 -H resume This command resumes the print request Sales-101. The print request starts printing from page one unless you instruct it otherwise. lp -i Sales-101 -H resume -P 4- This command resumes the print request Sales-101, starting with page 4 of the print job. Note that the final hyphen (following the "4") is required. The "lp" command can also be used as part of a pipeline. For instance, the following command will print the output of the "ps -ef" command to the default printer: ps -ef | lp Print the output of the "ps -ef" command to the default printer.

35 CM_aps