0% found this document useful (0 votes)
10 views58 pages

Kali Linux

The document provides an overview of text editors in Linux, focusing on the vi editor and its functionalities, including creating and managing text files. It covers various commands for file management, redirection, and filtering, as well as the standard input and output mechanisms in Linux. Additionally, it explains the use of pipes and filters to manipulate data in the command line environment.

Uploaded by

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

Kali Linux

The document provides an overview of text editors in Linux, focusing on the vi editor and its functionalities, including creating and managing text files. It covers various commands for file management, redirection, and filtering, as well as the standard input and output mechanisms in Linux. Additionally, it explains the use of pipes and filters to manipulate data in the command line environment.

Uploaded by

supravat.p2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Department of Computer Science

Subject Name : Linux & Shell Programming

Subject Code : 43A

Class : II B.Sc. CS

Semester : IV

Prepared by : Dr. P. Selvi


Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit - III
TEXT EDITORS
• UNIT III: Creating files using the vi editor: Text editors – The vi editor.
Managing Documents: Locating files in LINUX – Standard files –
Redirection – Filters – Pipes.


TEXT EDITORS

• A text editor is a program that enables to create and modify text


files. Typically, a text editor provides a screen with a fixed line length
and line numbers.
Functions of a text editor

• Create files: A text editor can be used to create, edit, and save files.
• Open files: A text editor can be used to open, modify and save an
existing file. It is also used to open multiple documents at a time.
• Copy text: text editors facilitate document creation because they enable
to copy and paste text.
• Search for text: A text editor can be used to search a particular
expression or a word. The search and replace feature enables to
change multiple instances of a particular word or a string.
Editors available with linux

• The vi Editor
• The visual editor, popularly known as the vi editor, is one of the
most widely used UNIX based text editors. It offers a compact interface
and enables to control the system by using the keyboard.
The vim Editor

• The visual editor improved or vim editor is an enhanced version of


the vi editor. The vim editor includes various enhancements over the vi
editor, such as syntax highlighting, command-line editing, and online
help
The emacs editor

• Another widely used text editor is the edit macros or emacs editor.
The emacs editor provides a much larger set of commands than the vi
editor
The ed Editor

• The line editor or ed is used to create, display, modify and save


text files. When we start the ed editor with a file name as an argument,
a copy of the file is created in the editor’s buffer.
The red Editor

• The restricted editor or red is used to edit files in the current


directory. This editor cannot execute shell commands.
The joe Editor

• Another popular editor available is the Joe’s own editor or joe. The
joe editor is a full – screen editor that lets to edit both programs and
text.
The pico Editor

• The pico editor is based on the Pine messaging system. The pine
messaging system is a character-based interface in Linux. It is used to
send and receive e-mail messages. The pico editor offers various
features, including paragraph justification and search features
Unit - III
THE vi
EDITOR
THE vi EDITOR

 The vi editor is a visual editor used to create and edit text files,
documents, and programs. It displays the contents of files on the
screen.
 The vi editor is one of the oldest editors available for the UNIX platform.
The vi editor is available for all of the variants of UNIX and Linux.
Getting started with the vi Editor

• Give the following command at the Linux prompt to invoke the vi editor:
• vi filename <Enter>
The following table lists out the basic
commands to use the vi editor −
• Following is an example to create a new file testfile if it already does
not exist in the current working directory −
• $vi testfile
• The above command will generate the following output −
• |

• ~

• ~

• ~

• ~

• ~

• ~

• ~

• ~

• ~

• ~

• ~

• ~

• "testfile" [New File]


• A tilde (~) represents an unused line. If a line does not begin with a tilde and
appears to be blank, there is a space, tab, newline, or some other non-
viewable character present.
• The vi editor works in two modes, edit and command. The edit mode is used to
add text to a file. The vi editor has different commands to add, change, and
delete text.
• The command mode is used to move the cursor, save the file, and give various
commands to manage files.
• To switch from edit mode to the command mode, press the <Esc> key. To
switch from the command mode to the edit mode, press a, A, o, O, i, or I keys.
Commands used in the vi Editor
Unit - III
Managing Documents
Locating Files in Linux

• The find Command


• The find command is used to locate a file in a specific directory and
all in its subdirectories.
• Syntax
• find [path] [expression]
• The –name Option
• The –name option to specify the name of the files that to be
located.
• Example
• $ find / home / Steve -name “jan”
• / home / Steve / 2000 / jan
• / home / Steve / 2001 / jan
A Sample Directory Structure
• When the find command is used without the –name option, the command displays
the path names of all the files in the specified directory and its subdirectories.
• Example: $ find / home / Steve
• / home / Steve / 2000
• / home / Steve / 2000 / df
• / home / Steve / 2000 / jan
• / home / Steve / 2000 / feb
• / home / Steve / 2000 / temp
• .
• .
• / home / Steve / progs / report
The –type Option

• The –type option is used to locate a specific type of file.


• Example : $ find / home / Steve –type f
• The option f with –type searches for all the ordinary files
The –exec Option


• The –exec option is used to specify the operations that need to be
performed on the files that have been located by the find command.
• $ find . –name temp –type f –exec rm {} \;
The –ok Option

• The –exec option is to execute a command on each file located by


the find command.
• The –ok option is used to operate on files that are located. When the –
ok option is used, the command prompts the user for a confirmation
before executing the command for each file.
• The syntax of the –ok option is similar to that of the –exec option.
• Example:
• $ find . –name temp –type f –ok rm {} \;
• To locate the file dmesg, the following command is used:
• $ find / –name dmesg
• To view the contents of the file, the following command is used:
• $ find / –name dmesg –exec cat {} \;
Unit - III

Standard Files
Standard Files

• A computer system consists of mainly three parts, input, processing, and output devices. The
input device accepts data from a user, and the output device displays or prints information or
any error messages that are generated.
• The keyboard and mouse are examples of input devices, and the
screen and the printer are the examples of output devices. The CPU is
the processing device.
• In the Linux operating system, all input and output devices are
considered special files.
• Standard Input File
• If we specify the cat command with a file name, all the lines in the file
are displayed. However, without a file name, the cat command takes
input from the standard input file or the keyboard as given below:
• $ cat <Enter>
• The cat command waits for input from the keyboard. The screen will
display the following output:
• $ cat <Enter>
Standard Error File

• When a 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.
• The screen is referred to as the standard error file. This is assigned to
the file descriptor 2.
• A user logs on to the Linux server and types the following command:
• $ cp file1 dir1
• The command is correct; it might generate error if the user does not
have the write permission on dir1 or the read permission on file1.
Unit - III
REDIRECTION
» The visual editor on the Linux.
» The vi editor comes with every version of Linux or Unix.
» Using vi is similar to using other editors in that you can see your file on the
screen ..
» The vi editor is the most popular editor in linux. The current version is
» really "vim", but to invoke it simply type "vi".
» Before vi the primary editor used on Unix was the line editor
⋄ - User was able to see/edit only one line of the text at a time
» The
⋄ vi- you
editor is not a text formatter (like MS Word, Word Perfect, etc.)
cannot set margins
⋄ - center heading
⋄ - Etc…”
» Although other stories exist, the true one tells that vi was originally
» bywritten
Bill Joy in 1976.

» Who is Bill Joy you ask?


- He co-founded Sun Microsystems in 1982

.
and served as chief scientist until 2003
The vi editor is:

» - A very powerful
» - It is hard to learn, specially for
windows
users
» - Move from point to point in the file, and
make changes.
» - Available on all UNIX systems
There are three modes in vi
» - Command mode
⋄ - Accessed by typing Esc
⋄ - In this mode all the keys pressed by the user are interpret to the editor command.
» - Input mode
⋄ - Accessed by typing “i”
⋄ - This mode permits insertion of new text, editing of existing text or replacement of existing text.
» Ex mode( Last Line Mode)
⋄ -The bottom line of the vi screen is called ex mode.
⋄ - When you start vi by default it is in command mode
» -You exit the input mode by pressing the Esc key to get back to the command mode.
I/P & O/P
REDIRECTION
Your keyboard is your standard
input (stdin) device, and the
screen or a particular
terminal
• Most Linux commands read input, such as a file or another attribute for
the command, and write output. By default, input is being given with the
keyboard, and output is displayed on your screen.
window is the standard output (stdout) device.
However, since Linux is a flexible system, these default settings
don't necessarily have to be applied. The standard output, for
example, on a heavily monitored
server in a large environment may be a printer.
Output redirection with > and Input
redirection with <
The> operator tells unix, “Don’t display this output on screen instead , put it
somewhere else”. Likewise the < operator says “ the input for this command is
not coming from the keyboard this time look for somewhere else”.

The somewhere else can be a file or a printer for output redirection and a
file or a keyboard for input redirection.
Example
$cat file1 > file2

$cat test1
some words
$ cat test2
some other words
$ cat test1 test2 > test3
$cat test3
some words some other
words
Example
$cat file1 < file2

$cat < test3


some words some other words
Example
Sometime we may use both the redirection operators at once as
shown below
$ cat < currentfile > newfile
input is taken fron currentfile and output is stored in
newfile
$ cat < currentfile > newfile
is also same
Example
Another redirection operator is
>>
Example
$who >> logfile
it is similar to > except if the target
file already existing the new output is append to its end
This command append current users who have logged in to the
end of logfile
Unit - III
Pipes & Filter commands in Linux
Pipe is a mechanism in which the output of one command can be
redirected as input to another command.

General format is, Command1|command2

The output of command1 is sent to command2


as input.

Example:$ls |more

The output of the command ls is sent to the more command as


input.
The directory listing of the current directory is displayed page by
page.

The following two command group displays the total number of


users who are currently logged ion the system.

$who > userlist.txt Wc –l userlist.txt


Redirection

Linux treats the keyboard as the standard input (value 0) and terminal screen
standard output (value 1) as well as standard error(value2).
Redirection inputs:
The < symbol is used to redirect inputs. Example
$cat < file.txt

File.txt takes input command cat. Redirecting outptuts:


The > symbol is used to redirect outputs.
$ ls >list.doc
Redirecting error messages:

The ‘2>’ symbol is used to redirect error messages.

Example :

$cat list1.doc

If there is no file named list1.doc in the current directory then the


error messages is sent to the standard error device.
Filters

There are some linux commands that accept input from standard input
or files,perform some manipulation on it and produce some
output to the standard output.

Since these commands perform filtering operations on data ,they are


called as filters.
Sort
This command sorts the contents of a given file based on ASCII values of
characters.

General format is,


Sort [options] <filename>

Where options can be,


-m <filelist> merges sorted files specified in <filelist>. in the
-o <filename> stores output specified
<filename>.
-r sorts the content in reverse order.
-u removes duplicate lines and displays sorted content.
-n numeric sort
Ggrep (global search for regular expression)command is used to search
specified pattern from a specified file and displays those lines
containing the pattern.
General format is,
Grep [options] pattern <filename> Where options
can be,
b ignores spaces ,tabs
I ignores distinction matching
V displays only the lines that match the
do not specified pattern.
C displays the total number of occurrence of the pattern in the
file.
N displays the resultant lines along with their
line numbers.
Match zero or more characters. Matches a single character.

[r1-r2] matches a single character within the ascii


range represented by the characters.

[^abcd] matches a single character which is not a,b,c or d.

^<character>matches the lines that are beginning with character


specified in <character>.
More command
The more command is used to display output page by page.
More <filename>
Example:
$more a.c
This command will display the content of the file –
a.c page by page.
$ls | more
This command will display the directory listing page by page.
Pr
This command displays the contents of the
specified file adding with suitable header and footers.
The header part consist of last modified date and time
along with file name and page number.
General format is,
Pr [-options] <filename>
Where options can be,
-l it changes the page size to specified<number> Of lines.
-<number> prepares the output in <number>columns.
-n numbers lines
-t turns off the heading at the to of the page.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy