0% found this document useful (0 votes)
3 views32 pages

UNIT II Linux Notes

Uploaded by

fowmila j
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

UNIT II Linux Notes

Uploaded by

fowmila j
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1

JCT COLLEGE OF ARTS AND SCIENCE ,PICHANUR,COIMBATORE


IV SEMESTER
LINUX AND SHELL PROGRAMMING
UNIT-II
Staff name:Fowmila J
-----------------------------------------------------------------------------------------------------------------------------
UNIT II: Creating files using the vi editor: Text editors – The vi editor. Managing
Documents: Locating files in LINUX – Standard files – Redirection – Filters – Pipes.
------------------------------------------------------------------------------------------------------------------------------
3.1 – Creating Files Using vi Editor-Text Editor

Synopsis
1. Introduction
2. Ed Editors
3. Text editors
4. Functions of Text Editors
5. Editors Available with Linux

Introduction
 Editors are mainly used for creating, deleting, and editing the files
 The VI editor is the most popular and classic text editor in the Linux family.
 There are several editors in Linux and some of them are highly sophisticated but all
systems have two standard editors, they are
 Ed and Vi editors.

Ed Editors
 Ed allows the user for one line editing only, so it is not of much use.

Text Editors
 It is a program that enables you to create and modify text files.
 Text editor provides a screen with a fixed line length and line numbers
 Its helps to type text line by line, navigate through the documents by using different
commands, and save the document.
 Text editor can be used to
 Create and edit documents
 Create programs and utilities
 Write E-mail messages
 A text editor is just like a word processor with a lot of features.
 All operating systems come with a basic text editor.
 Linux comes with several text editor.
Functions of Text Editor
1. Create files (create, edit & save files)
2. Open files (open, modify, save an existing file in a text editor)
3. Copy text (copy and paste text)

[Type here]
2

4. Search for text (Search and replace a word or string or particular expression, incase you
forget to remember expression we can use wildcard character to look expression)

Editors available with Linux


 There are various editors available with Linux.
 They are
1. The vi Editor
2. The vim Editor
3. The emacs Editor
4. The ed Editor
5. The red Editor
6. The joe Editor
7. The pico Editor

3.2 – Editors Available in Linux

Synopsis
1. Introduction
 The vi Editor
 The vim Editor
 The emacs Editor
 The ed Editor
 The red Editor
 The joe Editor
 The pico Editor

Introduction
 There are various editors available with Linux.
 They are
1. The vi Editor
2. The vim Editor
3. The emacs Editor
4. The ed Editor
5. The red Editor
6. The joe Editor
7. The pico Editor

The vi Editor
 The Visual Editor
 It is otherwise known as vi editor
 Mostly used in UNIX based text editors
 It offers a compact interface
 It enables you to control the system by using the keyboard
 This editor is used to enter and edit source code and write short notes such as email messages
 Vi is Called Visual – it immediately because it displays the changes that make to text on screen

[Type here]
3

 It is screen oriented text editor


 It is used to edit lines in the file

The vim Editor


 The improved version of the VI editor is called the VIM.
 VIM stands for Vi Improved.
 It is an enhanced version of the vi editor.
 It includes various enhancement over the vi editor, such as syntax highlighting, command line
editing and online help
 It can be used to edit ASCII text
 It is useful for editing programs

The emacs Editor


 It is an another text editor which is otherwise known as edit macros or emacs editor
 It provides a much larger set of commands than the vi editor
 This editor helps to format source code for programming languages such as C and C++
 To enable the users to access information about commonly used commands
 This editor displays an online help screen

The ed Editor
 We can use line editor or ed to create, display, modify and save text files
 Ed allows the user for one line editing only, so it is not of much use

The red Editor


 Otherwise known as restricted editor
 It is used to edit files in the current directory
 We cannot execute shell commands by using the red editor

The joe Editor


 Otherwise known as joe’s own editor or joe
 We can invoke by typing joe at the command prompt
 We can also give a file name along with joe command to open the file in the editor
 Joe editor is a full screen editor that help you to edit both programs, and text

The pico Editor


 This editor is based on the Pine Messaging System
 It is a character based interface in Linux
 It is used to send and receive email messages
 It offers various features, including paragraph justification and search features
 It provides spelling checkers and file browser
 Pico editor and commands are displayed at the bottom of the screen
 It also offers context sensitive help
3.3 - Vi Editors Available in Linux
Synopsis

[Type here]
4

 Introduction
 Getting Started with the Vi Editor
 Syntax
 Example

Introduction
 The Vi editor is a visual editor used to create and edit text files, documents and program.
 It displays the contents of files on the screen.
 The Vi editor is one of the oldest editors available for the UNIX platform.
 In addition, the vi editor is available for all the variants of UNIX and LINUX.
 In Red Hat Linux, Vi is a symbolic link to the vim editor
 Therefore, you can start the vim editor by typing the vi command.
 Additionally, view command is another symbolic link to the vim editor and is used to open a
file in the read only mode.

Symlink
 A symlink is a symbolic Linux/ UNIX link that points to another file or folder on your
computer, or a connected file system.
 This is similar to a Windows shortcut.
 Symlinks can take two forms:
 Soft links are similar to shortcuts, and can point to another file or directory in any file system.
 Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder
or file in a different file system.

The vi Editor
 The Visual Editor
 It is otherwise known as vi editor
 Mostly used in UNIX based text editors
 It offers a compact interface
 It enables you to control the system by using the keyboard
 This editor is used to enter and edit source code and write short notes such as email messages
 Vi is Called Visual – it immediately because it displays the changes that make to text on screen
 It is screen oriented text editor
 It is used to edit lines in the file

The vim Editor


 The improved version of the VI editor is called the VIM.
 VIM stands for Vi Improved.
 It is an enhanced version of the vi editor.
 It includes various enhancement over the vi editor, such as syntax highlighting, command line
editing and online help
 It can be used to edit ASCII text
 It is useful for editing programs
Starting the Vi editor

[Type here]
5

Getting Started with the Vi Editor


 You can give the following command at the Linux prompt to invoke the vi editor

Syntax
Vi filename <Enter>

Example
# Vi [Link]

Figure: A file created with vi Editor

Getting Started with the Vi Editor


 In the above example, if the [Link] (or [Link]) file exists, the vi editor displays the
contents of the file on the screen.
 In contrast, if the [Link] [Link] (or [Link]) file does not exist, the vi editor
creates the file and displays the following screen.

3.4 – Commands Used in the Vi Editor


[Type here]
6

Synopsis
1. Introduction
2. Commonly used commands in the Vi Editors
3. Insert and Replace Commands
4. Cursor Movement Commands in the Vi Editor
5. Word Movement Commands
6. Deletion and Modification Commands
7. Commands to copy lines
8. Pattern Finding Commands

Introduction
 The Vi editor offers commands that can be used for creating and editing files.
 The following tables describes the functions of various commands used in the vi editor

Commonly used commands in the Vi Editor

Insert and Replace Commands

[Type here]
7

Insert and Replace Commands


 The following commands used to insert and replace text in the Vi editor

Cursor Movement Commands in the Vi Editor


 The following table describes commands used for the basic cursor movement commands

[Type here]
8

Word Movement Commands


 The following table describes commands used for the basic word movement commands

Deletion and Modification commands


 The following table describes commands used for the basic word movement commands

[Type here]
9

Commands to Copy Lines


 The following tables describes the commands used to copy lines

Commands to Copy Lines


Note:
 While working on a file, you might need to copy sections of text.
 For example, to copy 20 lines of text from a file, give the 20 yy command.
 The 20yy command will copy 20 lines of text starting from the current line

Pattern (model) Finding Commands


 The following table describes the pattern finding commands in the Vi editor
Example-Pattern (model) Finding Commands

Example-Pattern (model) Finding Commands


Note:
 The vi editor offers commands that enable you to search for a specified pattern in a file
 For eg, the customer files stores the details of customers who have registered with ABC
telecommunications.
Example-Pattern (model) Finding Commands

[Type here]
10

“000001”,”Angela”, “Smith”, “16223 Radiance court”, ”Kansas City”, “Kansas”


“000002”,”Barbara”, “Johnson”, “227 Beach Ave”, ”Alexandria”, “Virginia”
“000003”,”Betty”, “Williams”, “1 Treat Road”, ”Dublin”, “Georgia”
 In the above file, to find the details about the customers at Georgia, give the /Georgia command.
 So that /Georgia command will find the line containing the word Georgia, as shown in below
“000003”,”Betty”, “Williams”, “1 Treat Road”, ”Dublin”, “Georgia”

3.5 – The emacs Editor


Synopsis
1. Introduction
2. Use of emacs editor
3. Getting started with emacs editor
4. Example
5. Commands in the emacs editor

Introduction
 The emacs Editor is another widely used text editor that offers more commands than the vi
editor

[Type here]
11

 Like other UNIX text editors, the emacs editor offers commands and key combinations that
enable you to create and modify text files
 It is used to type source code for programs

Use of emacs Editor


 The emacs editor also allows you to work with multiple files at a time
 In the emacs editor, you can switch between files that are open and save or close individual
files
 In addition, the emacs editor offers the ispell utility, which enables you to check the spelling in
a document
 The emacs editor offers online help
 The emacs editor also offers a tutorial to help you know more about the various commands
 To access the tutorial, press the <Ctrl>+h+t key combinations

Getting started with the emacs Editor


 The emacs editor is invoked by the following command from the shell prompt
Syntax
emacs [command line switches] [file/s]
 The above command will start the emacs editor and create a new file

Example
$ emacs +3 sample
 The above command will open the sample file in the emacs editor and place the cursor on line
number three.

Commands in the emacs Editor


 The following table describes some of the commonly used commands in the emacs Editor

[Type here]
12

3.6 – The joe Editor


Synopsis
1. Introduction
2. What is joe editor
3. Getting started with joe editor
4. Example
5. Commands in the joe editor

Introduction
 Another popular editor available in Linux is the joe editor
 It is the full screen editor
 The joe editor offers many features that enable you to create and edit text files
 It allows you to work with multiple file at a time
 In addition, the joe editor offers the search and replace feature
 We can also check spelling in a document by using the ispell utility
 The joe editor emulates (follows or imitates) several other editors

What is joe Editor


 JOE is a full featured terminal-based screen editor which is distributed under the GNU
General Public License (GPL).
 JOE has been around since 1988 and comes standard with many Linux distributions.

Example
 We can invoke the joe editor by typing the joe command at the command prompt
 To open a file in the joe editor, specify the file name along with joe command

[Type here]
13

Getting started with the joe Editor


Syntax
Joe filename
Example
Joe [Link]

Commands in the joe Editor


 The following table describes some of the commonly used commands in the joe Editor

3.7 – Managing Documents –Locating Files in Linux

Synopsis
1. Introduction
2. The find command
3. Some options of the file commands
 The –name option
 The –type option
 The –exec option
 The –ok option

Introduction
 For managing documents and locating files in Linux the following command are used.
 The find command
The find command
 The find command helps us to find a particular file within a directory.
 It is used to find the list of files for the various conditions like permission, user ownership,
modification, date/time, size, and more.
 It is used to find (locate) a file in a specific directory and in all its sub directories

[Type here]
14

Syntax of find command


Syntax
Find [path] [expression] //File name to be searched
Where
Find – is a command
Path – specify the path where the files are located
Expression-consists of options to perform the advanced search among files

Some options of the file commands


 Some of the options to perform the advanced search among file command are given below
 The –name option
 The –type option
 The –exec option
 The –ok option

The –name option


 It is used to specify the name of the files that you need to locate or searched
Example
 Consider the following command:
 $ find/ home/ Steve -name “jan”
 /home/Steve/2000/Jan
 /home/Steve/2001/Jan

 In the above command /home/Steve is the name of the directory in which specific files need
to be located
 The –name “jan” option specifies that all the files with the name jan are to be located.

File hierarchy of the /home directory

[Type here]
15

3.8. The –name option


Introduction
 Note: when you use the find command without the –name option, the command displays the
path names of all the files in the specified directory and its subdirectories
 This is shown in example below
 $ find /home/Steve
 /home/Steve/2000
 /home/Steve/2000/df
 /home/Steve/2000/jan
 /home/Steve/2000/feb
 /home/Steve/2000/temp

The –name option


 home/Steve/2001
 /home/Steve/2001/df
 /home/Steve/2001/jan
 /home/Steve/2001/temp
 /home/Steve/Desktop
 /home/Steve/progs
 /home/Steve/2001/progs/ [Link]
 /home/Steve/2001/ progs/report

The –type option


 It is used to identify or determine a specific type of file and its data
 Consider the command given below
$ find /home/Steve –type f
 Where option
 –type f search for all the ordinary files
 To search the directory files, you need to type the following command
 $ find /home/Steve –type d search for all the directory files

The –exec option


 Used to specify the operation that needs to be performed on the files
 For eg, you can remove all the files that are named temp from all the directories under a
specific directory
 For example
$ find. –name temp –type f –exec rm { }\ ;
 In the above command, each ordinary files “temp” is to be removed from the Steve current
directory and all its sub directories

The – ok option
 It is used to operate on the files that are located
 When we use the –ok option, the command prompts the user for a confirmation before
executing the command for each file

[Type here]
16

Syntax
 It is similar to that of a –exec option

Example
$ find. –name temp –type f –ok rm { } \;

 To locate the file dmesg,


 $ find/ -name dmesg
 To view the contents of the file
 $ find/ -name dmesg –exec cat {}\;

3.9 - Standard files

Synopsis
1. Introduction
2. Input devices
3. Processing devices
4. Output devices
5. Standard Input file
6. Standard output file
7. Standard Error file
Introduction
 A computer system consists of mainly three parts.
 They are
1. Input devices
2. Processing devices
3. Output devices

Input devices
 It accepts data from a user through the input devices.
 The input devices are keyboard and mouse.

Processing devices
 It processes the user input according to the specified instructions to generate the desired
output.
 The processing devices are Processor in CPU.

Output devices
 It displays or prints information or any error messages that are generated in processor.
 The output devices are screen or monitor, printer.

Linux Operating System


 In Linux OS, all input and output devices are consist of following special files
 Standard input file
 Standard output file

[Type here]
17

 Standard Error file

Fig: Standard files

File descriptor
 A file descriptor is an integer used by a process to identify an open file.
 A file descriptor is a number that uniquely identifies an open file in a computer's operating
system.
 It describes a data resource, and how that resource may be accessed.

 In Linux, all the open files, including standard files, are assigned a number called file
descriptor.
 The standard input file is assigned the file descriptor 0.

Standard input file


 In Linux, when you execute a command that requires input, the shell interpret (read) the
command and assigns the keyboard as the default source of input.
 The keyboard is referred to as the standard input file

Example
$ cat <Enter>
 When you specify the cat command with a file name, all the lines in the file are displayed

Example
 When you specify cat command without file name, it takes input from the standard input file
or the keyboard, the following characters are displayed on the screen as shown below
$ cat <Enter>
 This is a test to depict <Enter>
 This is a test to depict

[Type here]
18

 [The cursor waits at the next]

Example
 While using the cat command, you can enter multiple lines.
 To indicate the end of input, you need to press the <Ctrl> and d keys.
 The $ prompt is then displayed on the screen.

Standard Output file


 In Linux, the shell assigns the screen as the default destination for the output of any command
that is executed.
 The screen is referred to as standard output file

Example
$ ls
 Here shell executes the command and sends the output, the directory list, to the standard
output file or the screen.
 The standard output file is assigned the file descriptor 1.

Standard Error file


 When the user types invalid or unauthorized commands, such as commands for which a user
does not have permission, the shell utilities display error messages on the screen
 This screen also referred to as standard error file
 The standard error file is assigned to the file descriptor 2

Example
 Error messages are also displayed on the standard error file if you do not have appropriate
permissions on a file.
 For example, a user logs on to the Linux server and types the following command
$ cp file1 dir1
 Although the command is correct, it might generate an error if the user does not have the
write permission on dir1 or the read permission on file1.

3.10 – Redirection

Synopsis
1. Introduction
2. Types of redirection
3. Input redirection
4. Output redirection
5. Error redirection
6. File descriptor
Introduction

 Inorder to prevent error messages from displayed on the screen, we need to change the
default assignment (task) of standard files.

[Type here]
19

 The process of changing the assignment for the standard input, standard output, and the
standard error file is known as Redirection
 Using redirection, input to a command can be taken from a file instead of keyboard
 Similarly, the output of a command or error can be written to a file or printed instead of being
displayed on the screen

What is Redirection?
 It is the ability of the Linux operating system that allows us to change the standard input
(stdin) and standard output (stdout) when executing a command on the terminal
 By default, the standard input device is your keyboard and the standard output device is your
screen

Types of redirection
1. Input
2. Output
3. Error

Input redirection
 We can redirect (forward or sent) the input from a file instead of typing the commands by
using the keyboard
 Symbol used for redirecting the input is <.

Example
$ cat <test1 <enter>
 In the above example < symbol implies input redirection from the file test1 on the hard disk.
 In the above example, the cat commands read each line of the file test1 as input and displays it
on the screen.
 We can also type the above command by using the file descriptor as follows

Example with file descriptor


$ cat 0 <test1 <enter>
 In the above example, 0 indicates input redirection

Output Redirection
 If you need to store the output of the ls command in another file instead of displaying the
output on the screen, you need to redirect the output to a file.
 Symbol used for redirecting the output is >.

Example
$ cat test1 > test2
 In the above example > symbol redirect the output to the file test2.
 The output of the cat command is written to a disk file test2.

Example
 You can also use the cat command with the file descriptor are as follows
$ cat 1 test1 > test2

[Type here]
20

$ cat 1 test1 >> test2


 In the above example, 1> indicates output redirection.
File descriptor
 In Linux, all the open files, including standard files, are assigned a number called file
descriptor.
 The standard input file is assigned the file descriptor 0.

Error Redirection
 The following example explains error redirection
$ cat datafile 2>error-mesg <enter>
 Assume that the file datafile does not exist in the current directory

Error Redirection
 When a user tries to execute the above command, Linux generates an error messages because
execution is unsuccessful .
 In this case, the error message will not be displayed on the screen (standard error file)
 Instead the message will be written to the file error-mesg

3.11. Example Program for Redirection

[Type here]
21

[Type here]
22

[Type here]
23

3.12– Filter
Synopsis
1. Introduction
2. Various filter
3. Cat
4. Grep
5. Wc
6. Cut
7. Tr

Introduction
 It is a program that takes input from the standard input file, processes it, and sends the output to the
standard output file.

Various Filters
 Linux provides various filters such as
 cat
 grep
 wc
 tr
 cut
 It enable you to work effectively with data

Fig: Filter

The grep Filter


 It searches a file for a specified pattern of characters and displays all the lines that contain the pattern.
 The pattern that is searched is referred to as regular expression

Syntax
grep regular_expression [filename]
where
 filename optional in grep command (if ur do not specify a file name, the filter expects
input from standard input file)
 while typing text, grep searches the line for the regular expression and displays the line
 To stop the execution of grep filter, press <ctrl> and d keys

[Type here]
24

Example
$ grep job <enter>
 Work
 Job
 Job
 Task
 <ctrl>d

 In the above example, the grep filter waits for input from the standard input file
 The grep filter searches for the word job when you enter the words work, job, and task
 Whether grep filter finds the word job, its displays the word job again on the standard output file
 Notice that the word job appears two times in the output.

Characters Used in Grep Pattern

[Type here]
25

Options of the Grep Filter

The wc Filter
 It is used to count the number of lines, words, and characters in a file
 It is also used to count the number of lines, words and characters typed by using the standard input
file

The syntax of wc Filter


Syntax
wc [-lwc] [filename/s]
Example
$ wc sample <Enter>
2 7 29 sample

 The file sample has 2 lines, 7 words and 29 characters
Options of the wc Filter

The syntax of wc Filter


 As like grep filter, in the wc filter, when you do not specify a file name with wc filter, it expects input
from the standard input file as shown below
$ wc sample <Enter>
wc is a filter
<press Enter>
<press ctrl and d>
1 4 15

[Type here]
26

 After you type the text, wc is a filter and press the enter key
 Wc filters counts the number lines, words, and characters that you entered.
 It displays
 No of lines
 No of words
 No of characters entered
 To stop the execution of wc filter, use
<Ctrl> and d
The cut Filter
 To extract specific columns from files
 You can use the cut filter to extract the output of certain commands, such as ls and who.
Syntax and Examples
Syntax
Cut [options] [filename/s]
Examples
$ cut –d ‘:’ –f1 /etc/passwd
Syntax and Examples

Syntax
 in the above example, the cut filter is used to extract only the names of the users in the /etc/passwd
file.
 The field separator is the : symbol

$ cut –c– 5 /etc/passwd

 The above command displays the first five characters from the /etc/passwd file.

[Type here]
27

Options of the cut Filter

The tr filter
The tr filter
 It is used to replace one set of characters with another
Examples
$ tr ‘:’ ‘ ‘ < / etc / passwd
 The above command replaces each occurrences of the :symbol in the /etc/passwd file with
a space and displays the output on the standard output file.
Example - tr filter
 We can also use the tr filter to change the casing of letter
Example
$ tr ”[a-z]” ”[A-Z]”
 The above command converts all lowercase letters to uppercase, when the user types the
following sentence
The road was a ribbon of moonlight
THE ROAD WAS A RIBBON OF MOONLIGHT
3.13-Example Program for Filter

[Type here]
28

[Type here]
29

Pipes
Synopsis
1. Introduction
2. What is Pipes
3. Functioning of a pipe
4. The tee command
5. Example1
6. Example2
Introduction
 Standard output of one program used as standard input to next program
 Used with filter commands to further refine (process) data
 Not limited to two programs
What is Pipes?
 In Linux, the pipe command sends the output of one command to another.
 Piping, as the term suggests, can redirect the standard output, input, or error of one process
to another for further processing.
Example
$ ls > tempfile
$ more tempfile
 ls command stores a list of the directories in the file tempfile.
 The tempfile is used as input by the more command
 By using Linux pipe feature, the above two steps can be combined and executed as a single
command without creating a temporary file as shown in below
$ ls|more
 In the above example the vertical (|) is the pipe character, which indicates the output of the
command typed before the | is to be sent as input to the command typed after the |
Functioning of a pipe
 You can use the pipe feature to perform complex tasks by combining the commands
available in Linux.
 When you use the pipe features, you can specify the multiple commands or filters
 To understand how the pipe command works, consider the following example in the figure
below.

[Type here]
30

Fig: Functioning of a pipe

The tee command


 It is used to redirect the standard input and write it to the standard output or to file(S)
Example 1
$ cat temp | tee temp1 temp2
 In the above command displays the contents of the file temp on the screen and also writes
the files temp1 and tmep2
Example 2
$ sort numbers | tee sort-numbers | more
 In the above command, the file numbers is sorted, and the sorted data is redirected to the
tee command.
 It creates the file sort-numbers and stores the data in the file (sort numbers)
 It also redirects data as input to more utility
 For example.
Figure: the tee Command

[Type here]
31

[Link] Program for Redirection

[Type here]
32

All the Best

[Type here]

You might also like