OS_LAB_04
OS_LAB_04
OS_LAB_04
Operating Systems
Experiment 4
Introduction to VI Editor and Shell Programming –I
1) find
The find command is used to locate files in a directory.
The –name option: This lists out the specific files in directory. Wild cards can be used.
Examples
$find
2) history
List all commands typed so far.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
3) more FILE
Display the contents of FILE, pausing after each screen.
There are several keys which control the output once a screen has been printed.
Examples
more +3 myfile.txt
Display the contents of file myfile.txt, beginning at the first line containing the string
"hope".
4) less FILE
"less" is a program like "more".
Generally, if a command is given it will affect only the current working directory.
For example, the following command will create a directory named curr in the current
working directory.
$ mkdir curr
The directory can also be created elsewhere in the file system using the absolute and relative
path.
1. Absolute Path
If the path is given with respect to the root directory, then it is called full path or
absolute path.
$ mkdir /home/it2006/it2k601/newdir
The full path always starts with the /, which represents the root directory.
Relative Path
If the path is given with respect to the current working directory or parent directory,
$ mkdir ../newdir
The above command will create a directory named newdir in the parent directory.
$ mkdir ./first/newdir
The above command will create a directory named “newdir” inside “first directory”,
Note:
PIPES
The Pipe is a command in Linux that lets you use two or more commands such that
In short, the output of each process directly as input to the next one like a pipeline.
Pipes help you mash-up two or more commands at the same time and run them
consecutively.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Syntax:
grep search_string
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Option Function
-v Shows all the lines that do not match the searched string
Let us try the first option '-i' on the same file use above -
Using the 'i' option grep has filtered the string 'a' (case-insensitive) from the all the lines.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
sort Filename
There are extensions to this command as well, and they are listed below.
Option Function
-r Reverses sorting
-n Sorts numerically
The example below shows reverse sorting of the contents in file 'abc'.
FILTERS
Linux has a lot of filter commands like awk, grep, sed, spell, and wc.
A filter takes input from one command, does some processing, and gives output.
When you pipe two commands then the "filtered " output of the first command is given to
the next.
1. Sort filter
The sort filter arranges the input taken from the standard input in alphabetical order.
The sort command when used with “-r” option will display the input taken from the
When used with “-n” option arranges the numbers, alphabets and special characters per their
ASCII value.
2. Grep filter
Used to search for a particular pattern from a file or from standard input
Where the given pattern is searched inside the lines contained in the named files.
When the options and input file are not specified, then the grep command searches for
standard input (which is by default the standard text typed on the keyboard)
The VI editor is the most popular and classic text editor in the Linux family.
It is user-friendly.
Hence, millions of Linux users love it and use it for their editing needs
Nowadays, there are advanced versions of the vi editor available, and the most popular one
1. Command mode:
In this mode, you can, move the cursor and cut, copy, paste the text.
This mode also saves the changes you have made to the file.
2. Insert mode:
You can switch to the Insert mode from the command mode by pressing 'i' on the
keyboard
Once you are in Insert mode, any key would be taken as an input for the file on which
To return to the command mode and save the changes you have made you need to
vi <filename_NEW> or <filename_EXISTING>
VI Editing commands
dd - Delete line
C - Delete contents of a line after the cursor and insert new text. Press ESC key to end
insertion.
dw - Delete word
cw - Change word
r - Replace character
S - Substitute entire line and begin to insert at the beginning of the line
Note:
VI editor is case-sensitive so make sure you type the commands in the right letter-case.
Make sure you press the right command otherwise you will end up making undesirable
k - Move cursor up
You should be in the command mode to exit the editor and save changes to the file.
Running Commands
The vi has the capability to run commands from within the editor.
To run a command, you only need to go to the command mode and type
:! command
For example, if you want to check whether a file exists before you try to save your file with
that filename
:! ls
You can press any key (or the command's escape sequence) to return to your vi session.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
or
Task 1:
Get into the vi editor and type some text then save the file with the name
my_first_linux_file.
Task 2:
4. Lastly remove the lines “It is a bore subject. It is not helpful for us.” from
Task 3:
Make a Shell Script that make use of pipe command.
2. Next make use of pipe command for the word count of already created
Task 4:
Make a shell script that highlights only the lines in a text file that do not