UNIT II Linux Notes
UNIT II Linux Notes
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)
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
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
[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
[Type here]
5
Syntax
Vi filename <Enter>
Example
# Vi [Link]
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
[Type here]
7
[Type here]
8
[Type here]
9
[Type here]
10
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
Example
$ emacs +3 sample
The above command will open the sample file in the emacs editor and place the cursor on line
number three.
[Type here]
12
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
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
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
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.
[Type here]
15
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 { } \;
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.
[Type here]
17
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.
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
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.
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.
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
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
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
[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
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.
[Type here]
25
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
[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
The above command displays the first five characters from the /etc/passwd file.
[Type here]
27
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
[Type here]
31
[Type here]
32
[Type here]