Laboratory Exercise 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8
At a glance
Powered by AI
The document talks about using vi editor to create and edit files in Linux. It also discusses various commands to view, search and manipulate file content.

The main steps are to open vi editor by typing 'vi filename', go to insert mode by pressing 'i' to add content, press Esc to go to command mode and then type ':wq' to save and quit.

The cat command is used to display full file content. Head/tail commands like head and tail can be used to view parts of a file. wc command counts characters, words and lines.

COLLEGE OF COMPUTER STUDIES

IT0035L
(APPLIED OPERATING SYSTEMS LABORATORY)

EXERCISE

5
LINUX FILE READERS AND vi EDITOR

Student Name / Group


Name:
Name Role
Members (if Group):

Section:
Professor:

I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE


 Ability to use and apply current technical concepts and practices in the core information
technologies; human computer interaction, information management, programming, networking and
web systems and technologies. [PO: J]

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE


 Perform file and directory creation and manipulation using DOS commands; LINUX installation in
virtual machine, file and directory creation and manipulation, and system administration using LINUX
commands. [CLO: 2]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Able to create files and edit file content using vi editor, and display file content using various LINUX
file readers

IV. BACKGROUND INFORMATION


vi Editor
You can use vi editor to edit an existing file or to create a new file from scratch. You can also use
this editor to just read a text file.

Starting the vi Editor:

There are following way you can start using vi editor:

Command Description
Creates a new file if it already does not exist, otherwise opens existing
vi filename
file.
vi -R filename Opens an existing file in read only mode.
view filename Opens an existing file in read only mode.
:f filename Renames current file to filename.

Following is the example to create a new file testfile if it already does not exist in the current
working directory:
$vi testfile
As a result you would see a screen something like as follows:

• ~
• ~
• ~
• ~

IT0035L-Applied Operating System Laboratory Page 2 of 8


• ~
• "testfile" [New File]
You will notice a tilde (~) on each line following the cursor. 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 nonviewable character present.

So now you have opened one file to start with. Before proceeding further let us understanding few
minor but important concepts explained below.

Operation Modes:
While working with vi editor you would come across following two modes:

 Command mode: This mode enables you to perform administrative tasks such as saving
files, executing commands, moving the cursor, cutting and pasting lines or words, and
finding and replacing. In this mode, whatever you type is interpreted as a command.

 Insert mode: This mode enables you to insert text into the file. Everything that's typed in
this mode is interpreted as input and finally it is put in the file.

The vi always starts in command mode. To enter text, you must be in insert mode. To come in
insert mode you simply type i. To get out of insert mode, press the Esc key, which will put you back
into command mode.

Hint: If you are not sure which mode you are in, press the Esc key twice, and then you'll be in
command mode. You open a file using vi editor and start type some characters and then come in
command mode to understand the difference.

Getting Out of vi:


The command to quit out of vi is :q. Once in command mode, type colon, and 'q', followed by
return. If your file has been modified in any way, the editor will warn you of this, and not let you
quit. To ignore this message, the command to quit out of vi without saving is :q!. This lets you exit
vi without saving any of the changes.

The command to save the contents of the editor is :w. You can combine the above command with
the quit command, or :wq and return.

The easiest way to save your changes and exit out of vi is the ZZ command. When you are in
command mode, type ZZ and it will do the equivalent of :wq.

You can specify a different file name to save to by specifying the name after the :w.
For example, if you wanted to save the file you were working as another filename called filename2,
you would type :w filename2 and return. Try it once.

IT0035L-Applied Operating System Laboratory Page 3 of 8


V. GRADING SYSTEM / RUBRIC (please see separate sheet)

VI. LABORATORY ACTIVITY

TASKS:
Part I. How to use vi editor in creating files.

To create a file using vi editor:


1. Type vi then followed by a filename
For example:
$vi file1
Press insert key to input text
Type “Hello World!”
To save and quit, press ALT+SHIFT: then type wq
Note:
w – save
q – quit

Paste your captured executed command and output below.

2. To display the content of file1, type cat file1.


Paste your captured executed command and output below.

3. Create a directory named Folder1 at your username directory. List the content of Folder1 directory.
Paste your captured executed command and output below.

IT0035L-Applied Operating System Laboratory Page 4 of 8


4. Create a new file named Psalm23.txt using the vi editor and write in verbatim of the ff. including the
numbering:

1. Psalm 23
2. The Lord is my shepherd, I lack nothing.
3. He makes me lie down in green pastures,
4. he leads me beside quiet waters,
5. he refreshes my soul.
6. He guides me along the right paths
7. for his name’s sake.
8. Even though I walk
9. through the darkest valley
10. I will fear no evil,
11. for you are with me;
12. your rod and your staff,
13. they comfort me.

Paste your captured executed command and output below.

5. Display the content of the file Psalm23.txt using cat command


Paste your captured executed command and output below.

IT0035L-Applied Operating System Laboratory Page 5 of 8


6. Display the first 7 lines of the Psalm23.txt
Paste your captured executed command and output below.

7. Display the last 4 lines of the Psalm23.txt


Paste your captured executed command and output below.

8. Count the number of characters in file Psalm23.txt


Paste your captured executed command and output below.

9. In just one line of command, count the number of lines and words are there in Psalm23.txt
Paste your captured executed command and output below.

IT0035L-Applied Operating System Laboratory Page 6 of 8


How many lines are there? _____
How many words are there? _____

10. Using vi editor, open your file Psalm23.txt and search for the word Lord in the file content.
Paste your captured executed command and output below.

Observation:

Conclusion:

VII. REFERENCES:
 Sobell, M., et al. (2017). A Practical Guide to Linux Commands, Editors, and Shell Programming,
4th Ed. Addison-Wesley Professional

IT0035L-Applied Operating System Laboratory Page 7 of 8


 Cobbaut, P. (2016). Mastering Linux- Networking
 Blum, R., (2015). Linux Command Line and Shell Scripting Bible
 Fox, R., (2015). Linux with operating system concepts
 Dulaney, E., (2014). Linux all in-one for dummies, 5th Ed.Wiley
 Rosen, R. (2014). Linux kernel networking: implementation and theory. Apress

IT0035L-Applied Operating System Laboratory Page 8 of 8

You might also like