Unix(introduction)

107
11/02/22 UNIT-I : UNIX Introduction 1

Transcript of Unix(introduction)

Page 1: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

1

Page 2: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

2

Page 3: Unix(introduction)

3

Page 4: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

4

History of UNIX

In 1965 , Bell laboratories joined with MIT and General Electric to develop a new operating system called Multics (Multiplexed Information Computing Service) , which would provide multi-user , multi-processor and multi-level (hierarchical) file system.

In 1969 , AT&T developed the first UNIX file system with a few utilities and gave the name UNIX to it. In 1970 , January 1 time zero for UNIX started. In 1973 , UNIX was re-written in C by Dennis Ritchie . Being written in a high level language decreased the effort to port it to new machines.

In 1980 , BSD 4.1 (Berkelely Software Development ) was developed.

In 1983, SunOs , BSD 4.2 , System were released.

In 1983 , AT&T and Sun Microsystems jointly developed System V Release 4 (SVR 4).This was later developed in UNIX ware and Solaris 2.

Page 5: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

5

Page 6: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

6

Page 7: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

7

Page 8: Unix(introduction)
Page 9: Unix(introduction)
Page 10: Unix(introduction)
Page 11: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

11

Page 12: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

12

Page 13: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

13

Boot Block :- This block contain a small boot program and partition table.

Super Block:-It contains globle information about the file system.It also maintains a free list of inodes and data blacks that can be immediately allocated by the kernel when creating a file.

This is mainly what it contains:- The size of the file system. The block size used by the file system. The number of free data blocks available and a partial list of

immediately allocable free data blocks. Number of free inodes available and a partial list of

immediately usable inodes. Last time of updating. The state of the file system (Whether “clean “ or “dirty”).

Inode Block :-This region contains the inode for every file of the file system. Data Block:-All data and programs created by users reside in this area.

Page 14: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

14

Page 15: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

15

Unix is a operating system, used by most large, powerful computers. A commands is an instruction given th the shell ; kernel will obey

that instruction. UNIX provides several commands for its users to easily work with it.

The general format of a command is :- command -options command_arguments

Page 16: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

16

Page 17: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

17

1.ls(list)-To shows the list of files and direrctories.Syntax: ls [-option] [name of file name/ directory name]

Where

Option Description-x Multicolumnar Output-F Marks executables with *,directories with /, and symbolic links with @-a Shows all filenames beginning with a dot including . And ..-R Recursive list (Directory., Subdirectory., files)-r Sorts filename in reverse order-1 One filename in each line-l long listing (Seven attributes of a file)-d dirname Lists only dirname if dirname is a directory-t Sorts filenames by last modification time-lt Sorts listing by last modification time-u Sorts filename by last access time-lu Sorts by ASCII collating sequence but listing shows last access

time-lut As above but sorted by last access time-i Displays inode number

Page 18: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

18

$lsxyz, pqr, abc , MCA4

$ls –lTotal 72-rw-r—r– 1 kumar metal 19514 May 10 13:54 MCA4-rw-xr-r- 1 kumar metal 4174 May 10 15:01 xyzdrwxr-xr-x 2 kumar meatl 84 Feb 12 12:30 pqr

$ls –liTotal 729045 -rw-r—r– 1 kumar metal 19514 May 10 13:54 MCA49056 -rw-xr-r- 1 kumar metal 4174 May 10 15:01 xyz9045 drwxr-xr-x 2 kumar meatl 84 Feb 12 12:30 pqr

Page 19: Unix(introduction)

04/11/23

UNIT-I : UNIX Introduction

19

‘*’ Represents any number of characters. ‘?’Represents a single character. ‘[RANGE]’Represents character list. ‘[Character_List]’ Matches any single character, Which is specified in

Character List. ‘[!Character_list]’ Matches any single character , Which is not specified in

Character List.

Example:-$ls pgm*This command will list out all the filenames of the current directory, which are

starting with “pgm”.the sufffix to pgm may be any number of characters.

$ls *sThis command will display all the filenames of the current directory , which are

ending with “s”.$ls ?gmsThis command will display four character filenames, which are ending with

“gms” starting with any of the allowed character. $ls gms?This command will display four character filenames, which are starting with

“gms” ending with any of the single character.

Page 20: Unix(introduction)

“[]” Represents a subset of related filenames. This can be used with range operator “-” to access a set of files . Multiple ranges must be separated by commas.

$ls pgm[1-5] This command will list only the files named , pgm1 , pgm2 , pgm3 , pgm4 ,

pgm5 if they exist in the current directory.

$ls pgm[1-5],[11-15]This command will list only the files named , pgm1 , pgm2 , pgm3 , pgm4 ,

pgm5, pgm11 , pgm12 , pgm13 , pgm14 , pgm15 if they exist in the current directory.

$ls a[bc]dThis command will list only the files named abd and acd

$ls a[!bc]dThis commands will list only the files named agd , ahd , akd , amd except abd

and acd.

04/11/23UNIT-I : UNIX Introduction 20

Page 21: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

21

2.mkdir[Make Directory]This command is used to create new directory.General format is :-

mkdir [-p] <directory-name1> <directory-name2>The option –p is used to create consequences of directories using a single

mkdir command.Example:-$mkdir ibrThis command will create ‘ibr’ a subdirectory of the current directory.$mkdir x x/y This command will create x as a subdirectory of current working directory , y

as subdirectory of x.$mkdir ibr/ib/iThis command will make a directory i as a subdirectory of ibr/ib, but the

directory structure –ibr/ib must exist.$mkdir –p ibr/ib/iThen for the current directory , a subdirectory named ibr is created . Then for

the directory ibr , a subdirectory named ib is created . After that the subdirectory i as created as a subdirectory of the directory ib.

Page 22: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

22

3.rmdir[Remove Directory]This command is used to remove (delete) the specified directory. A directory

should be empty before removing it.

Syntax:- rmdir [-p] <directory_name1> <directory_name2>

The option –p is used to remove consequence of directories using a single rmdir command.

Example:-

$rmdir ibrThis command will remove the directory ibr , which is the subdirectory of the

current directory.

$rmdir ibr/ib/iThis command will remove the directory i only.

$rmdir –p ibr/ib/iThis commad will remove the directory i , ib and ibr consequently.

Page 23: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

23

4.cd[Change Directory]This command is used to change working directory to a specified directory.Syntax:- cd <existing_directory_name>Example:- cd /home/ibr

Then the directory /home/ibr becomes as the current working directory.

5.pwd[Present Working Directory / Print Working Directory]This command displays the full pathname for the current working directory.Example :-

$pwdOutput:- /home/bmi

Your present working directory is /home/bmi.

Page 24: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

24

5. Find find is one of the power tools of the UNIX system. It recursively examines a

directory tree to look for files matching some criteria and then takes some action on the selected files.

Syntax:-find <path_list> <selection _criteria> action

This is how find operators: First , it recursively examines all files in the directories specified in

path_list. It matches each file for one or more selection_criteria. Finally , it takes some action on those selected files.

Example:-$find / -name a.out –print

Output:- /home/kumar/scripts/a.out/home/tiwari/scripts/reports/a.out/home/sharma/a.out

Page 25: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

25

<Selection_Criteria>

-name

<filename> Selects the file specified in <filename> If wild cards are used then double quote <filename>

-user <username>

Selects files owned by <username>

-type d Selects directories.

-size +n

-n

Selects files that are regular than /less than “n” blocks (1 blocks= 512 bytes)

-mtime

n

+n

-n

Selects files that have been modified on exactly n days / more than n days / less than n days.

-mmin n

+n

-n

Selects files that have been modified on exaclty n minutes / more than n minutes / less than n minutes.

-atime n

+n

-n

Seelcts files that have been accessed on exactly n days / more than n days /less than n days

-amin n

+n

-n

Selects files that have been accessed exactly n minutes / more than n minutes /less than n minutes.

Page 26: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

26

Example:-

$find /home/ibrahim –name “*.java” –print

$find /home/ibrahim –mtime 5 –print

$find /home/ibrahim –mtime +5 –print

$find /home/ibrahim –mtime -5 -print

Page 27: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

27

6. du [Disk Usage]This command reports the disk spaces that are consumed by the files ina specified

directory . Including all its subdirectories.

Syntax:- du [-option] [<directory_name /file_name>]Options Description

-a Displays counts for all files . Not just directories.-b Displays size in bytes.-c Displays output along with grand total of all arguments.-k Displays the size in Kilobytes.-m Displays the size in Megabytes.

Example:- $du o/p :- ?$du /home/bmi/text

Output:- 12 /home/bmi/text$du –a /home/bmi/text

Output:-4 /home/bmi/text/x.txt4 /home/bmi/text/y.txt

12 /home/bmi/text

Page 28: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

28

7.df (Disk Free)This df command reports the available free space on the mounted file system or disks.Syntax:- df [-option]

Options Description -l Shows local file system only -k Displays the size in kilobytes -m Displays the size in Megabytes -i Reports free used percentage of used inode.

This command reports the free space in blocks. Generally one block in 512 bytes. The i-nodes entry indicates that up to the specified number of files can be created on the file.

Example:- $df

File System 1-Blocks Used Available Used Mounted

On

/dev/hda8 1612808 63632 1467248 5% /

/dev/hda8 23302 3489 18610 16% /boot

/dev/hda9 1035660 7100 975952 1% /home

/dev/hda10 1778840 742916 945560 44% /usr

/dev/hda11 1517920 1517920 1423784 2% /var

Page 29: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

29

1.cat :- Means Concatenate i.e. To add more than one string.

It is one of the most important command of UNIX Operating System. It is useful for creating a file and displaying a contents of file in a small terminal .

cat command provides the various facility in UNIX Operating

System. This various facility is also called various flavors of cat command.

Page 30: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

30

Page 31: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

31

Page 32: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

32

Page 33: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

33

Page 34: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

34

Page 35: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

35

Page 36: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

36

Page 37: Unix(introduction)

2.cp [Copy]:- This command is used to copy the content of one file into another. If the destination is an existing file , the file is overwritten . If the destination is an existing directory the file is copied into that directory.

Syntax:-cp [-option] <source_file> < destination_file>where options can be:-

i – prompt before overwritten destination files.p - Preserve all information ,including owner , group , permission and timestamps.R - Recursively copies files in all subdirectories.

Example:- $cp myfile myfile1

Output:-$cat myfileI am file one.

$cat myfile1I am file one.

04/11/23UNIT-I : UNIX Introduction 37

Page 38: Unix(introduction)

3. rm [Remove]:-This am command is used to remove a file from the specified directory. To remove a file , you must have write permission for the directory that contains the file , but you need not have permission on the file itself . If you do not have write permission on the file , the system will prompt before removing.

Syntax:-rm [-option] <filename>where options are as follows:-r - deletes all directories including the lower order directories. Recursively deletes entire contents

of the specified directory and the directory itself .i - Prompts before deletingf - Removes write – protected files also, without prompting.

Example:-$rm myfile1This command deletes the file myfile form the current directory.

$rm –f /usr/ibrahim

This command deletes all the files and subdirectories of the specified directory /usr/ibrahim.Note that the directory ‘ibrahim’ also will be delted.

04/11/23UNIT-I : UNIX Introduction 38

Page 39: Unix(introduction)

4.mv [Move]This command is used to remove the specified files / directories.

Syntax:-$mv <source> <destination>

Example:-mv file1.txt file2.txt

Output:-$cat file1.txtFile Not Found

$cat file2.txt

I am file1.

04/11/23UNIT-I : UNIX Introduction 39

Page 40: Unix(introduction)

5. wc [Word Count]This command is used to display the number of lines,words and characters of information stored on the specified file.

Syntax:-wc [-option] <File_Name>

Wherel - Displays the number of lines in the filew – Displays the number of words in the filec – Display the number of characters in the

file Examples:- Command Output

$cat file1 I am file1$wc file1 1 3 10 file1$wc –l file1 1 file1$wc –w file1 3 file1$wc –c file1 10 file1

04/11/23UNIT-I : UNIX Introduction 40

Page 41: Unix(introduction)

6.ln(Link)This command is used to establish on additional filename to a specified file.It doesn’t mean that creating more copies of the specified file.

Syntax:-ln <filename> <additional_filename>Where <Filename> is the file name for which <additional_filename> is to located on any directory. Thus UNIX allows a file to have more than one name and yet maintain a single copy in the disk. But changes to one of these files are also reflected in the disk .But changes to one of these files are also reflected to the others. If you delete one filename using rm command , then the other link names will still exist.

Example:-$ls –l test1.txt

-rw-rw-r-- 1 bmi bmi 75 jan 13 14:35 test1.txt

$ln test1.txt test2.txt$ls –l test*.txt

-rw-rw-r-- 2 bmi bmi 75 jan 13 14:35 test1.txt-rw-rw-r-- 2 bmi bmi 75 jan 13 14:35 test2.txt

$rm test1.txt$ls –l test2.txt

-rw-rw-r-- 1 bmi bmi 75 jan 13 14:35 test2.txt

04/11/23UNIT-I : UNIX Introduction 41

Page 42: Unix(introduction)

7.fileThis command lists the general classification of a specified file. It lets you to know if the contents of the specified file is ASCII text , C program text , directory and others.

Syntax:- file <filename>

Example:-$file test1.txt

Outputtest1.txt : ASCII text.

$file *Output

test1.test: ASCII textxyz: Directory

04/11/23UNIT-I : UNIX Introduction 42

Page 43: Unix(introduction)

8.cmp [Compare]This command is used compare two files.

Syntax:- cmp <filename1> <filename2>

This command reports the first instance of differences between the specified files. That is the two files are compared byte by byte and the location of the first mismatch is echoed to the screen.

Example:-$cat file1.txtI am RamWhat is your name?

$cat file2.txtI am RamWhat are you doing?

$cmp file1.txt file2.txtOutput

file1.txt file2.txt differ : byte 10 char 1, line2

04/11/23UNIT-I : UNIX Introduction 43

Page 44: Unix(introduction)

9. comm [Common]

This command uses two sorted files as argumnets and reports what is common.It compares each line of the first with its corresponding line in the second file. The output of this command is in three column as follows:Column1 : Contains lines common for both filename 1 and filename2 Column2 : Contains lines unique to filename2.Column3 : Contains lines unique to filename1.

Syntax:- comm [-option] <filename1> <filename2>

Option:-1 - Suppresses listing of column12 - Suppresses listing of column23 - Suppresses listing

Example:-$cat file1.txt $cat file2.txtI am Ram I am RamWhat is your name? What are you doing?

$comm file1.txt file2.txtOutput:-

I am Ram What are you doing?

What is your name?

04/11/23UNIT-I : UNIX Introduction 44

Page 45: Unix(introduction)

10. nl [ Non-Blank Line]This command numbers all non-blank line in the specified text file and display the same on the screen.

Syntax:- nl <filename>

Example:-$cat file1.txtI am RamWhat is your name?

$nl file1.txt

Output:-1 I am Ram2 What is your name?

04/11/23UNIT-I : UNIX Introduction 45

Page 46: Unix(introduction)

11. tac This command reverses a file contents , so that the last line becomes the first line.

Syntax:- tac <filename>

Example:-$cat file1.txtI am RamWhat is your name?

$tac file1.txt

Output:-What is your name? I am Ram

04/11/23UNIT-I : UNIX Introduction 46

Page 47: Unix(introduction)

12. tailThis command displays the end of the specified file.

Syntax:- tail -n <filename>

Example:-$cat student.txtMohan always keeps himself to himself.We were in a hurry. The road being zigzag ,We had to cut off a corner to reach in time.In his youth , he was practically rolling in money.

$tail -1 file1.txtOutput:-

In his youth , he was practically rolling in money.

$tail -2 file1.txtOutput:-

We had to cut off a corner to reach in time.In his youth , he was practically rolling in money.

04/11/23UNIT-I : UNIX Introduction 47

Page 48: Unix(introduction)

12. headThis command displays the top of the specified file.

Syntax:- head -n <filename>

Example:-$cat student.txtMohan always keeps himself to himself.We were in a hurry. The road being zigzag ,We had to cut off a corner to reach in time.In his youth , he was practically rolling in money.

$head -1 file1.txtOutput:-

Mohan always keeps himself to himself.

$tail -2 file1.txtOutput:-

Mohan always keeps himself to himself.We were in a hurry. The road being zigzag ,

04/11/23UNIT-I : UNIX Introduction 48

Page 49: Unix(introduction)

UNIX treats everything as files.There are 3 types of files in UNIX as follows:-

1.Ordinary File2.Directory File3.Special File

There are 3 types of modes for accessing these files as follows:-

1. Read Mode(r)2.Write Mode(w)3.Execute Mode(x)

Unix separates its users into 3 groups for securty and convenience as follows :-

1. user (u)2. user group(g)3. others(o)

04/11/23UNIT-I : UNIX Introduction 49

Page 50: Unix(introduction)

13. chmod [ Change Mode] This command is used to change the file permissions for an existing

file. We can use any one of the symbolic and octal notations to change file permissions.Syntax:-

chmod <user_symbols set/deny_symbol access_symbol > <filename>

14 chgrp[Change Group]This command is used to change the group ownership of specified file.Syntax:-

chgrp <new_group name> <filename>

15.chown [Change Owner]This command is used to change the owner of a specified file.Syntax:-

chown <new_owner> <filename>

04/11/23UNIT-I : UNIX Introduction 50

Page 51: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

51

Page 52: Unix(introduction)

%H Hour 00 to 23

%I Hour 00 to 12

%M Minute 00 to 59

%S Second 00 to 59

%D Date MM/DD/YY

%T Time HH:MM:SS

%w(SMALL) Day of the Week

%r Time in AM & PM

%y Last two digits of the year

04/11/23UNIT-I : UNIX Introduction 52

1.date :-

This command displays the current system date.

Syntax:-date + <format>

Page 53: Unix(introduction)

Command Output

$date MON JAN 07 15:10:10 IST 2008

$date +%H 15

$date +%I 03

$date +%M 10

$date +%w 2

(0-Sunday , 1-Monday , 2-Tuesday , 3-Wednesday , 4-Thursday , 5-Friday,

6-Saturday)

$date +%r 03:10:10 PM

04/11/23UNIT-I : UNIX Introduction 53

Page 54: Unix(introduction)

2. WhoSince UNIX is a multi-user operating system , several users may work on this system. This command is used to display the users who are logged on the system currently.

Syntax & Example :-$who

Output:-Aman tty1 jan 07 10:17

Shwetabh tty2 jan 07 10:20Neha tty4 jan 07 10:30

The first column of the output represents the user names. The second column represents the corresponding terminal names and the remaining columns represents the time at which the users are logged on.

04/11/23UNIT-I : UNIX Introduction 54

Page 55: Unix(introduction)

3. Who am I This commands tells you who you are. (Working on the current terminal).

Syntax & Example :-$who am i

Output:-Aman tty1 jan 07 10:17

04/11/23UNIT-I : UNIX Introduction 55

Page 56: Unix(introduction)

4.man (Manual)This command displays the syntax and detailed usage of the UNIX command , which is supplied as argument.

Syntax :-$man <UNIX_COMMAND>

Example:-$man wcThis will display the help details for “wc” command.Almost all of the command offer – help option that displays a short listing of all the options.

$wc --help

04/11/23UNIT-I : UNIX Introduction 56

Page 57: Unix(introduction)

5. cal (calculator)This command will display the calender for the specified month and year.

Syntax :-$cal [month] <year]

where month can be ranged form 1 to 12Example:-

$cal 2008This command will display calender for thr year 2008 (from 1 to 12 months).$cal 1 2008This command will display the calender for the month january of the year 2008.

04/11/23UNIT-I : UNIX Introduction 57

Page 58: Unix(introduction)

6. lpr (Line Printer)This command is used to print one or more files on printer.

Syntax :-$lpr [-option] <filename1>………………..<filenamen>

where options can be :-r – Removes file(s) form directory after printing.m – Mail inform you when printing is over.

Example:-$lpr file1.txt

04/11/23UNIT-I : UNIX Introduction 58

Page 59: Unix(introduction)

7. expr This command is used to perform arithmetic operations on integer.The arithmetic operator and their corresponding functions are given below.

Syntax :-+ Addition- Subtraction\* Multiplication/ Division [It gives my quotient of the division]% Remainder of division [ Modulas operator ]

04/11/23UNIT-I : UNIX Introduction 59

Page 60: Unix(introduction)

8. bc [Basic Calculator]This command is used to perform arithmetic operation on integer as well as on floats (Decimal Number].

Type the arithmetic expression in a line and press [ENTER] key. Then the answer will be displayed on the next line. After you have finished your work press [ctrl+d] keys to endup.

Example :- Add 10 and 20 $bc10 + 2030^d

04/11/23UNIT-I : UNIX Introduction 60

Page 61: Unix(introduction)

$bc10 / 33 [Decimal portion is truncated]^d$bcscale = 18 / 32.6

scale = 28 / 32.66^d

04/11/23UNIT-I : UNIX Introduction 61

Page 62: Unix(introduction)

$bc1. ibase = 2 ----set ibase to binary (2).

101 -----Type the input binary number.5 -----Result in Decimal

2. obase = 2 ----set obase to binary(2).5 ----Type the input decimal number101 ----Result in Binary form^d

Here similarly for Octal and hexadecimal numbers.

04/11/23UNIT-I : UNIX Introduction 62

Page 63: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

63

Page 64: Unix(introduction)

PIPE :-Pipe is a mechanism in which the output of one command can be redirected as input to another command.

Syntax:-command | command2

The output of command1 is sent to command2 as input.Example:-

$ls | moreThe output of the command “ls” is sent to the “more” command as input. So the directory listing of the current directory is displayed page by page .

$cat file1.txt | sort file1.txt |wc file1.txt

04/11/23UNIT-I : UNIX Introduction 64

Page 65: Unix(introduction)

REDIRECTION :-

UNIX treats the keyboard as the standard input (value 0) and terminal screen as standard output (value 1) as well as standard error (value 2). However , input can be taken from sources other than the keyboard and output can be passed to any source other than the terminal screen. such a process is called “redirection”.

1.Redirecting inputs2.Redirecting outputs3.Redirecting Error messages

1.Redirecting inputs :-The ‘<‘ symbol is used to redirect inputs.

Example:- $cat < file1.txtthen the file1.txt is taken as input for the command – cat

2.Redirecting outputs:-The “>” symbol is used to redirect outputs.

Example:- $ls > list.docThen the output of the command ‘ls’ is stored on the file “list.doc”. We can also use ‘1>’ instead of ‘>’.

3.Redirecting Error Messages:-The ‘2>’ symbol is used to redirect error messages.

Example :- $cat list1.docIf there is no file named “list1.doc” in the current directory , then the error message is sent to the standard error device. We can redirect this error messages using ‘2>’ symbol.

Example:- $cat list1.doc 2> error.txtThen the error messages , if generated , will be stored on the disk – error.txt.

04/11/23UNIT-I : UNIX Introduction 65

Page 66: Unix(introduction)

FILTERS:-There are some UNIX commands that accept input from standard input or files , perform some manipulation on it , and produces some output to the standard output. Since these commands perform some filtering operations on data , they are appropriately called as “Filters”. These filters are used to display the contents of a file in stored order , extract the lines of a specified file that contains a specific pattern etc.

1. sort :-This command sorts the contents of a given file based on ASCII values of characters.

Syntax:- sort [-option] <filename>

Options-m <filelist> =Merge sorted files specified in <filelist>-o <filename> =stores output in the specified <filename>-r = Sorts the contents in reverse order.-u = Removes duplicate lines and display sorted content.-c= Checks if the file is sorted or not.

Example:-$cat file.txt $sort file.txt E A

S EA SW W

04/11/23UNIT-I : UNIX Introduction 66

Page 67: Unix(introduction)

2. grep [Global Regular Expression Printing] :-This command is used to search for a specified pattern form a specified file and display those lines containing the patter.

Syntax:-grep [-option] pattern <filename>

Where options -b ignores spaces , tab.-i Ignore case -v Displays only the lines that do not match the specified

pattern.-e Displays the total number of occurrences of the pattern in

the file.-n Displays the resultant lines along with their line number.Example:-

$cat emp.ext1001 Ram Computer CS1002 Merry Electronics ET1003 John Computer CS

04/11/23UNIT-I : UNIX Introduction 67

Page 68: Unix(introduction)

$grep “CS” emp.txto/p:- 1001 Ram Computer CS

1003 John Computer CS

Regular Expression Character Set*: Represents any number of characters?: Represents any single character.[r1-r2]: Range[^abcd] : Matches a single character which is not a,b,c or d.^[character]: Matches the lines that are beginning with the character

specified in <Character>[character]$ :Matches the lines that are ending with the character

specified in <character>Example:-

$grep “Com*” emp.txto/p:- 1001 Ram Computer CS

1003 John Computer CS

04/11/23UNIT-I : UNIX Introduction 68

Page 69: Unix(introduction)

Related commands with grep:- 1.egrep [ Extended grep]2.fgrep [ Fixed grep]

egrep :- This command offers additional features than grep. Multiple patterns can be searched by using pipe symbol.

04/11/23UNIT-I : UNIX Introduction 69

Page 70: Unix(introduction)

3.uniq {Unique}This command is used to handle duplicate line in a file . If this command is used without any option , It displays the lines by eliminating duplicate lines.

Syntax:-uniq [-option] <filename>

Where options are as follows:--u ==Displays only the no –repeated lines.-d ==Displays only the duplicate lines.-c ==Displays each line by eliminating duplicate lines and prefixing the number of times it occurs.

Example:-$cat abc.txtabcxyzabcpqr

$uniq abc.txtabcxyzabcpqr

04/11/23UNIT-I : UNIX Introduction 70

Page 71: Unix(introduction)

$uniq –u abc.txtO/P:-

xyzpqr

$uniq –d abc.txtO/P:-

abc

$uniq –c abc.txtO/P:-

2 abc1 xyz1 pqr

04/11/23UNIT-I : UNIX Introduction 71

Page 72: Unix(introduction)

cut :-This command is used to cut the columns/ fields of a specified file [Like the head and tail commands cut the line-rows].

Syntax:-cut [-option] <filename>

Where options

c <Columns> -Cuts the column specified in <columns>. You must separate the column numbers by using commas.

f <Fields> - Cuts the fields specified in <fields> . You must separate field number by using commas.

Example:-

$cat emp.txt1001 John Computer Cs1002 Merry Electronics ET1003 Ram Computer CS

04/11/23UNIT-I : UNIX Introduction 72

Page 73: Unix(introduction)

$cut –c 2-5, 11-15 emp.txtOutput:-

001 Compu 002 Elec003 Comp

$cut -f 2,3 emp.txtOutput:-

John ComputerMerry ElectronicsRam Computer

$cut –d “|” –f 1,3- emp.txt1001 Computer CS1002 Electronics ET1003 Computer CS

$cut –f 1-3 emp.txtOutput:-

1001 John Computer1002 Merry Electronics1003 Ram Computer

04/11/23UNIT-I : UNIX Introduction 73

Page 74: Unix(introduction)

paste:-This command concatenate the contents of the specified files into to a single file vertically. [Cut command separates the columns , this paste command merges the columns].

Syntax:-paste <Filename1> <filename2>………<filename

n>

Example:-$cat e1.txt $cat e2.txt1001 10041002 10051003 1006

$paste e1.txt e2.txtOutput:-

100110021003100410051006

04/11/23UNIT-I : UNIX Introduction 74

Page 75: Unix(introduction)

tr [Translating Character]

This tr command filter manipulates individual characters in a line. More specifically , it translates characters using one or two compact expressions:

tr options expression1 expression2 standard input

tr takes input only from the standard input , it doesn’t take a filename asargument. By default it translates each character in expression1 to its mapped counterpart in expression2. The first character in the first expression is replaced with the first character in the second expression and similarly for the other characters.

1. Replace a character:-

$cat emp.txtOutput:-

1001 | John | Computer | CS1002 | Merry | Electronics | ET1003 | Ram | Computer | CS

04/11/23UNIT-I : UNIX Introduction 75

Page 76: Unix(introduction)

$tr ‘|’ ‘-’ emp.txt Output:-

1001 - John - Computer - CS1002 - Merry - Electronics - ET1003 - Ram - Computer - CS

2. Changing Case a Text

$cat emp.txt | tr ‘[a-z]’ ‘[A-Z]’

Output:-1001 | JOHN | COMPUTER | CS1002 | MERRY |ELECTRONICS | ET1003 | RAM | COMPUTER |CS

$cat emp.txt | tr ‘C,p’ ‘c,P’ Output:-

1001 | John | comPuter | cS1002 | Merry | Electronics | ET1003 | Ram | comPuter | CS

04/11/23UNIT-I : UNIX Introduction 76

Page 77: Unix(introduction)

3. Deleting Characters (-d):-$cat emp.txt

Output:-1001| John | Computer | CS1002| Merry | Electronics | ET1003| Ram | Computer | CS

$tr –d ‘|’ emp.txtOutput:-

1001 John Computer CS1002 Merry Electronics ET1003 Ram Computer CS

04/11/23UNIT-I : UNIX Introduction 77

Page 78: Unix(introduction)

sed [Stream Editor]:-This is a multipurpose tool which combines the work of several filters. It isderived from ed , the original editor. sed performs non-interactive operations on a data stream – hence its name .

sed uses instructions to act on text. An instruction combines an address for selecting lines , with an action to be taken on them as shown by the syntax:-

sed [options]/[Edit Commands] ‘address action’ files

The address and action are enclosed within single quotes. Addressing in sed is done in two ways:-

By one or two line numbers. By specifying a /- enclosed pattern which occurs in a line.

Where edit commands can be:-

i – Inserts after linea – Appends after linec – Changes linesd – Deletes linesp – Prints linesq – Quits

04/11/23UNIT-I : UNIX Introduction 78

Page 79: Unix(introduction)

Example1:-$sed ‘2q’ emp.txtOutput:-

1001 JohnComputer CS1002 Merry Electronics ET

This command displays the 1st two lines of the emp.txt file.

Example2:- $sed ‘2d’ emp.txtOutput:-

1001 JohnComputer CS1003 RamComputer CS

This command displays the contents of emp.txt by deleting the second line.

Example3:- $sed –n ‘2p’ emp.txtOutput:-

1002 Merry Electronics ETThis command displays the second line of the emp.txt.

Example4:- $sed –n ‘2,4p’ emp.txtThis command displays the lines 2 through 4 of emp.txt.

Example5:- $sed –n ‘$p’ e mp.txtThis command displays the last line of emp.txt.

04/11/23UNIT-I : UNIX Introduction 79

Page 80: Unix(introduction)

Example6:- $sed -1 ‘/cs/p’ emp.txtThis command displays the lines that are containing the pattern “cs”.

Example7: - $sed ‘/cs/d’ emp.txtThis command displays the lines that are not containing the specified

pattern“cs”.Example8:- $sed ‘i\ ---------------------------------’ emp.txtThis command inserts the specified dash line before each line of the fileemp.txt.

Example9:- $sed ‘a\ --------------------------------’ emp.txtThis command inserts the specified dash line after each line of the fileemp.txt.

Example10:- $sed ‘3i\ ------------------------------- ‘ emp.txtThis command inserts the specified dash line before the third line of the file emp.txt.

Example10:- $sed ‘3a\ ------------------------------- ‘ emp.txtThis command inserts the specified dash line after the third line of the file emp.txt.

Example 11:- $sed ‘$a\ -----------------------------------’ emp.txtThis command inserts the specified dash line after the last line of the file emp.txt

04/11/23UNIT-I : UNIX Introduction 80

Page 81: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 81

yacc - yet another compiler-compiler

The yacc command converts a context-free grammar into a set of tables .The lex command is useful for creating lexical analyzers usable by yacc.

Page 82: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

82

Page 83: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

83

This editor can be invoked by typing vi at the $ prompt. If you specify a filename as an argument to vi , then the vi will edit the specified files , if it exits.

vi [<filename>]

A status line at the bottom of the screen (25th line) shows the filenames, current line and character position in the edited file.

vi + <line_number> <filename>Edits the file specified in <filename> and places the cursor on

the <line_number>th line.

Page 84: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

84

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

cccccccccccccccccccccccccccccccccccccccc

ddddddddddddddddddddddddddd

eeeeeeeeeeeeeeeeeeeeeeeee

~~

~~

~

~

~

:wq

Page 85: Unix(introduction)

vi Modes

Insert/Input Mode Command Mode Ex-Mode

04/11/23UNIT-I : UNIX Introduction 85

Page 86: Unix(introduction)
Page 87: Unix(introduction)
Page 88: Unix(introduction)
Page 89: Unix(introduction)
Page 90: Unix(introduction)
Page 91: Unix(introduction)
Page 92: Unix(introduction)
Page 93: Unix(introduction)
Page 94: Unix(introduction)
Page 95: Unix(introduction)
Page 96: Unix(introduction)
Page 97: Unix(introduction)
Page 98: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 98

Input Mode

Ex-Mode

Command

Mode

Shell

vi foo :x, :q

:wq & ZZ

[Enter]:

[Esc]

i,I,a,A,o,O,r,R,s, and S

T

H

E

T

H

R

E

E

M

O

D

E

S

Page 99: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 99

Insert Mode :-

1) The text should be entered in this mode and any key press in this mode is treated as text.

2) We can enter into this mode from command mode by pressing any of the keys:

i Ia Ao Or Rs S

Page 100: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 100

Command Mode:-

It is the default mode when we start up vi-editor.

All the commands on vi-editor (Cursor movement text manipulation etc. ) should be used in this mode.

We can enter into this mode from insert mode by pressing the [Esc] key and from Ex Mode by pressing [Enter Key].

Ex Command Mode:-

1. The Ex command mode (Saving files , find , replace etc ..) can be entered at the last line of the screen in this mode.

2)We can enter into this mode from command mode by pressing [:] key.

Page 101: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 101

Insert Commands

i Inserts before cursor.I Inserts at the beginning of the current line.a Appends at the end of the current line.A Appends at the start of the current line.o Inserts a blank line below the current line.O Inserts a blank line above the current line.

Page 102: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 102

Delete Commands

x -Deletes a character at the cursor position.

<n>x-Deletes specified number (n) of character.

X -Deletes a character before the cursor position.

<n>X -Deletes specified number (n) of characters before the cursor position.

dw -Deletes from current position to end of the current word.

db -Deletes from cursor position to beginning of current word.

dd -Deletes current line.<n>dd -Deletes specified number of lines (n) from the

current line.

Page 103: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 103

Replace Commands

r – Replaces single character at the cursor position.R –Replaces characters until[ESC] key is pressed from

current cursor position.s – Replaces single character at the cursor position with

any number of character.S – Replaces entire line.

Search Commands

/string [Enter] – Searches the specified string forward in the file.

?string [Enter]- Searches the specified string backward in the file.

n - Finds the next string in the same direction.

N - Finds the next string in the opposite direction.

Page 104: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 104

Yanking (Copy & Paste) Commands

yy or (y) : Yanks the current line into the buffer (Copy).

nyy or ny : Copies the ‘n’ lines from the current line to the buffer.

p : Paste the yanked text below the current line.

P : Paste the yanked text above the current line.

Ex Mode Commands

:w – Saves file without quitting:w <filename> - Saves the content into a file

specified in <filename>:x or :wq – Saves file and quits from vi.:q! – Quits from vi without saving.

Page 105: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 105

SET Commands

The two set command i.e. set nu and set ic.

The first is used to associate line numbers with the file . You are currently working with .

The second is used to ignore the case while searching a pattern in the file.

In addition to those two there are several other options that the user can set to customize the environment of vi.

To get a list of all the options available with set , use the command:

(ESC): set allThought the list is long there are only a few which are most commonly used .

Page 106: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction 106

A list of these along with the affect of each is shown in figure:-

:set nu Set display of line numbers on.:set nonu Set display of line numbers off (Default).:set eb Beep the speaker when an error occurs (Default).:set noeb Do not Beep the speaker when an error occurs. :set ai Set auto indent on.:set noai Set auto indent off (default).:set ic Ignores case while searching a pattern.:set noic Do not Ignores case while searching a pattern (default).

Page 107: Unix(introduction)

04/11/23UNIT-I : UNIX Introduction

107

.exrc Profile and Miscellaneous Commands

Type :ToCtrl-gs how line number of current line.Ctrl-l redraw the entire display.:!sh fork a shell.:type Ctrl-d to get back to vi.. repeat last text change command at current cursor position.:set showmodes how when you are in insert mode.:set ic ignore case when searching.:set noic turn ignore case off.:set nu turn on line numbering.:set nonu turn line numbering off.

NOTE:-Place the :set commands in an executable file in your home directory named ~/.exrc to automatically turn on these features for every vi edit session.