0% found this document useful (0 votes)
4 views42 pages

Final Unix Project

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)
4 views42 pages

Final Unix Project

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/ 42

UNIX PRACTICAL FILE

INDEX

SR.NO TOPIC PG NO. SIGN


1 INTRODUCTION 2-7

2 Execute the following list of basic commands in UNIX: 8-10


(i) pwd (ii) mkdir (iii) cd

3 11-14
Execute the following list of basic commands in UNIX:
(i) who (ii) echo (iii) cat
4 15-22
Execute the following list of basic commands in UNIX:
(i) rm (ii) mv (iii) wc (iv) cp
5 23-29
Execute the basic file attributes with all possible options:
(i) ls (ii) chmod
6 20-33
Execute basic commands using vi editor: a.
input mode commands
b. saving text and quitting
7 34-38
Execute basic commands using vi a.
navigation
b. editing text
c. searching pattern
8 Execute the following filters using regular expressions 39-41
with all possible options: (i) grep (ii) sed

9
Write a shell script to display current date and calendar 42

HARSH KUMAR SINGH 190720040011 1


UNIX PRACTICAL FILE

INTRODUCTION

What is UNIX?

UNIX is an operating system which was first developed


in the 1960s, and has been under constant development
ever since. By operating system, we mean the suite of
programs which make the computer work.
It is a stable, multi-user, multitasking system for servers, desktops and laptops.

UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows
which provides an easy to use environment. However, knowledge of UNIX is required for
operations which aren't covered by a graphical program, or for when there is no windows
interface available, for example, in a telnet session.

Types of UNIX

There are many different versions of UNIX, although they share


common similarities. The most popular varieties of UNIX are Sun
Solaris, GNU/UNIX, and MacOS X.

Here in the School, we use Solaris on our servers and workstations, and Fedora UNIX
on the servers and desktop PCs.

The UNIX Operating System

The UNIX operating system is made up of three parts; the kernel, the shell and the
programs.
The Kernel

The kernel of UNIX is the hub of the operating system: it allocates time and memory to
programs and handles the file store and communications in response to system calls.

HARSH KUMAR SINGH 190720040011 2


UNIX PRACTICAL FILE

As an illustration of the way that the shell and the kernel work together, suppose a user
types rm myfile (which has the effect of removing the file myfile). The shell searches the
filestore for the file containing the program rm, and then requests the kernel, through
system calls, to execute the program rm on myfile. When the process rm myfile has
finished running, the shell then returns the UNIX prompt % to the user, indicating that it is
waiting for further commands.

The Shell

The shell acts as an interface between the user and the kernel. When a user logs in, the
login program checks the username and password, and then starts another program called
the shell. The shell is a command line interpreter (CLI). It interprets the commands the
user types in and arranges for them to be carried out. The commands are themselves
programs: when they terminate, the shell gives the user another prompt (% on our
systems).

The adept user can customise his/her own shell, and users can use different shells on the
same machine. Staff and students in the school have the tcsh shell by default.

The tcsh shell has certain features to help the user inputting commands.

Filename Completion - By typing part of the name of a command, filename or directory


and pressing the [Tab] key, the tcsh shell will complete the rest of the name
automatically. If the shell finds more than one name beginning with those letters you have
typed, it will beep, prompting you to type a few more letters before pressing the tab key
again.
History - The shell keeps a list of the commands you have typed in. If you need to repeat a
command, use the cursor keys to scroll up and down the list or type history for a list of
previous commands.

Files & Processes

Everything in UNIX is either a file or a process.

A process is an executing program identified by a unique PID (process identifier).

HARSH KUMAR SINGH 190720040011 3


UNIX PRACTICAL FILE

A file is a collection of data. They are created by users using text editors, running
compilers etc.

Examples of files:

• a document (report, essay etc.)


• the text of a program written in some high-level programming language
• instructions comprehensible directly to the machine and incomprehensible to a
casual user, for example, a collection of binary digits (an executable or binary file);
• a directory, containing information about its contents, which may be a mixture of
other directories (subdirectories) and ordinary files.

The Directory Structure

All the files are grouped together in the directory structure. The file-system is arranged in
a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally
called root (written as a slash / )

In the diagram above, we see that the home directory of the undergraduate student
"ee51vn" contains two sub-directories (docs and pics) and a file called report.doc.

The full path to the file report.doc is "/home/its/ug1/ee51vn/report.doc"

HARSH KUMAR SINGH 190720040011 4


UNIX PRACTICAL FILE

Starting an UNIX Terminal

To open an UNIX terminal window, click on the "Terminal" icon from


Applications/Accessories menus.

An UNIX Terminal window will then appear with a % prompt, waiting for you to start
entering commands.

HARSH KUMAR SINGH 190720040011 5


UNIX PRACTICAL FILE

UNIX Commands

The UNIX system is command-based i.e. things happen because of the commands that you
key in. All UNIX commands are seldom more than four characters long.

Internal Commands External Commands

HARSH KUMAR SINGH 190720040011 6


UNIX PRACTICAL FILE

Commands are grouped into two categories:

• Internal Commands: Commands which are built into the shell. For all the shell built-in
commands, execution of the same is fast in the sense that the shell doesn’t have to
search the given path for them in the PATH variable, and also no process needs to be
spawned for executing it.
Examples: source, cd, fg etc.

• External Commands: Commands which aren’t built into the shell. When an external
command has to be executed, the shell looks for its path given in the PATH variable,
and also a new process has to be spawned and the command gets executed. They are
usually located in /bin or /usr/bin. For example, when you execute the “cat” command,
which usually is at /usr/bin, the executable /usr/bin/cat gets executed. Examples: ls, cat
etc.

HARSH KUMAR SINGH 190720040011 7


UNIX PRACTICAL FILE

PROGRAM-1
OBJECTIVE: Execute the following list of basic commands in UNIX:
(i) pwd (ii) mkdir (iii) cd

(i) Pwd
• pwd stands for Print Working Directory.
• It prints the path of the working directory, starting from the root. pwd is shell built-in
command (pwd) or an actual binary(/bin/pwd).
$PWD is an environment variable which stores the path of the current directory. This
command has two flags.

• pwd -L: Prints the symbolic path.


pwd -P: Prints the actual path.

Here the working directory is /home/user.

HARSH KUMAR SINGH 190720040011 8


UNIX PRACTICAL FILE

(ii) mkdir
• mkdir command in Linux allows the user to create directories (also referred to as folders in some
operating systems).
• This command can create multiple directories at once as well as set the permissions for the
directories.
• It is important to note that the user executing this command must have enough permissions to
create a directory in the parent directory, or he/she may recieve a ‘permission denied’ error.

Options: -

I. --version: It displays the version number, some information regarding the license and exits.
II. --help: It displays the help related information and exits.
III. -v or --verbose: It displays a message for every directory created.
IV. -p: A flag which enables the command to create parent directories as necessary. If the
directories exist, no error is specified.
V. -m: This option is used to set the file modes, i.e., permissions, etc. for the created directories.
The syntax of the mode is the same as the chmod command.

We are in the /home/user directory where we have created a directory named “BCA”.

HARSH KUMAR SINGH 190720040011 9


UNIX PRACTICAL FILE

(iii) cd

• The cd (“change directory”) command is used to change the current working


directory.
• It is one of the most basic and frequently used commands when working on the
Linux terminal.
• The current working directory is the directory (folder) in which the user is
currently working in.
• Each time you interact with your command prompt, you are working within a
directory.

The command accepts only two options that are rarely used.

• −L: - Follow symbolic links . By default, cd behaves as if -L option is specified.


• −P: - Don’t follow symbolic links. In other words, when this option is specified,
and you try to navigate to a symlink that points to a directory, cd will change into
the directory.

In its simplest form, when used without any argument, cd will take you to your home
directory.

Here, the user has changed the directory to BCA which we can notice on executing the
pwd command, the shell displays the current working directory.

HARSH KUMAR SINGH 190720040011 10


UNIX PRACTICAL FILE

PROGRAM-2
OBJECTIVE: Execute the following list of basic commands in UNIX:

(i) who (ii) echo (iii) cat

(i) who

• The standard Unix command who displays a list of users who are currently
logged into the computer.
• The who command is related to the command w, which provides the same
information but also displays the following information:
1) Time of last system boot
2) Current run level of the system 3) List of logged in users and more.
• The who command displays the following information for each user currently
logged in to the system if no option is provided:
1) Login name of the users
2) Terminal line numbers
3) Login time of the users in to system
4) Remote host name of the user

HP user@Sony-Vaio MINGW64 ~

$ who

HP user tty7 2021-02-18 19:08 (:0)

HP user@Sony-Vaio MINGW64 ~

HARSH KUMAR SINGH 190720040011 11


UNIX PRACTICAL FILE

(ii) echo
• echo command in linux is used to display line of text/string that are passed as an
argument .
• This is a built in command that is mostly used in shell scripts and batch files to
output status text to the screen or a file.

Options: -

 \b : it removes all the spaces in between the text.


 \c : suppress trailing new line with backspace interpretor ‘-e‘ to continue without
emitting new line.
 \n : this option creates new line from where it is used.
 \t : this option is used to create horizontal tab spaces.
 \r : carriage return with backspace interpretor ‘-e‘ to have specified carriage
return in output.
 \v : this option is used to create vertical tab spaces.
 \a : alert return with backspace interpretor ‘-e‘ to have sound alert.
 -n : this option is used to omit echoing trailing newline .
 echo * : this command will print all files/folders, similar to ls command .

HARSH KUMAR SINGH 190720040011 12


UNIX PRACTICAL FILE

(iii) cat

• Cat(concatenate) command is very frequently used in Linux.


• It reads data from the file and gives their content as output.
• It helps us to create, view, concatenate files.
• To view a single file, $cat filename command is used.  To view multiple
files, $cat file1 file2.

Options: -
Tag Description

-A, --show-all equivalent to -vET

-b, --number-nonblank number nonblank output lines

-e equivalent to -vE

-E, --show-ends display $ at end of each line

-n, --number number all output lines

-s, --squeeze-blank never more than one single blank line

-t equivalent to -vT

-T, --show-tabs display TAB characters as ^I

-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB. display this help and
exit

--help display this help and exit

HARSH KUMAR SINGH 190720040011 13


UNIX PRACTICAL FILE

--version output version information and exit

HARSH KUMAR SINGH 190720040011 14


UNIX PRACTICAL FILE

PROGRAM-3
OBJECTIVE: Execute the following list of basic commands in UNIX:

(i) rm (ii) mv (iii) wc (iv) cp

(i) rm

• rm removes each specified file. By default, it does not remove directories.


• If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or
--recursive are given, then rm prompts the user for whether to proceed with the entire operation.
If the response is not affirmative, the entire command is aborted.
• Otherwise, if a file is unwritable, standard input is a terminal, and the -f or -force option is not
given, or the -i or --interactive=always option is given, rm prompts the user for whether to
remove the file. If the response is not affirmative, the file is skipped.

Options: -

TAG DESCRIPTION

-f, --force ignore nonexistent files, never prompt.


-i prompt before every removal.

prompt once before removing more than three files, or when removing
recursively. Less intrusive than -i, while still giving protection against most
-I mistakes.
-- prompt according to WHEN: never, once (-I), or always (-i). Without interactive[=WHEN] WHEN,
prompt always.

when removing a hierarchy recursively, skip any directory that is on a file


--one-file-system system different from that of the corresponding command line argument.
--no-preserve-root do not treat '/' specially.

HARSH KUMAR SINGH 190720040011 15


UNIX PRACTICAL FILE

--preserve-root do not remove '/' (default).

-r, -R, --recursive remove directories and their contents recursively.

-v, --verbose explain what is being done.

--help display this help and exit.

--version output version information and exit.

(ii) mv
 mv stands for move. mv is used to move one or more files or directories from one place
to another in file system like UNIX. It has two distinct functions:
a) It renames a file or folder.
b) It moves group of files to different directory.

HARSH KUMAR SINGH 190720040011 16


UNIX PRACTICAL FILE

• No additional space is consumed on a disk during renaming. This command normally


works silently means no prompt for confirmation.

• If the destination file doesn’t exist, it will be created.

Options: -

Tag Description

--backup[=CONTROL] make a backup of each existing destination file


-b like --backup but does not accept an argument

-f, --force do not prompt before overwriting

-i, --interactive prompt before overwrite

--strip-trailing-slashes remove any trailing slashes from each SOURCE

-S, --suffix=SUFFIX override the usual backup suffix

move all SOURCE arguments into DIRECTORY


-t, --target-
directory=DIRECTORY

-T, --no-target-directory treat DEST as a normal file

-u, --update
move only when the SOURCE file is newer than the destination file or
when the destination file is missing

HARSH KUMAR SINGH 190720040011 17


UNIX PRACTICAL FILE

-v, --verbose explain what is being done

--help display this help and exit

--version output version information and exit

none, off never make backups (even if --backup is given)

numbered, t make numbered backups

existing, nil numbered if numbered backups exist, simple otherwise


simple, never always make simple backups

(iii)
wc

HARSH KUMAR SINGH 190720040011 18


UNIX PRACTICAL FILE

• wc stands for word count. As the name implies, it is mainly used for counting purpose.
• It is used to find out number of lines, word count, byte and characters count in the files
specified in the file arguments.
• By default, it displays four-columnar output.
• First column shows number of lines present in a file specified, second column shows
number of words present in the file, third column shows number of characters present in
file and fourth column itself is the file name which are given as argument.

Options: -

Tag Description

-c, --bytes print the byte counts

-m, --chars print the character counts

-l, --lines print the newline counts

print the length of the longest line


-L, --max-linelength

-w, --words print the word counts

--help display this help and exit

--version output version information and exit

HARSH KUMAR SINGH 190720040011 19


UNIX PRACTICAL FILE

(iv) cp

• cp stands for copy. This command is used to copy files or group of files or
directory.
• It creates an exact image of a file on a disk with different file name.  cp
command requires at least two filenames in its arguments.

cp command works on three principal modes of operation and these operations depend
upon number and type of arguments passed in cp command:

1. Two file names: If the command contains two file names, then it copies the
contents of 1st file to the 2nd file. If the 2nd file doesn’t exist, then first it
creates one and content is copied to it. But if it existed then it is simply
overwritten without any warning. So be careful when you choose destination file
name.
cp Src_file Dest_file

2. One or more arguments: If the command has one or more arguments, specifying
file names and following those arguments, an argument specifying directory
name then this command copies each source file to the destination directory with
the same name, created if not existed but if already existed then it will be
overwritten, so be careful!

HARSH KUMAR SINGH 190720040011 20


UNIX PRACTICAL FILE

cp Src_file1 Src_file2 Src_file3 Dest_directory

3. Two directory names: If the command contains two directory names, cp copies
all files of the source directory to the destination directory, creating any files or
directories needed. This mode of operation requires an additional option,
typically R, to indicate the recursive copying of directories.
cp -R Src_directory Dest_directory

Options: -

Tag Description

-a, --archive same as -dpR

-b, --backup make backup before removal

-d, --no-dereference preserve links

-f, --force remove existing destinations, never prompt

-i, --interactive prompt before overwrite

-l, --link link files instead of copying

-p, --preserve preserve file attributes if possible

-P, --parents append source path to DIRECTORY

-r copy recursively, non-directories as files

--sparse=WHEN control creation of sparse files

-R, --recursive copy directories recursively

-s, --symbolic-link make symbolic links instead of copying

-S, --suffix=SUFFIX override the usual backup suffix

copy only when the SOURCE file is newer than the destination file or when
-u, --update
the destination file is missing

HARSH KUMAR SINGH 190720040011 21


UNIX PRACTICAL FILE

-v, --verbose explain what is being done

-V, --version-
override the usual version control
control=WORD

-x, --one-file-system stay on this file system

--help display this help and exit

--version output version information and exit.

HARSH KUMAR SINGH 190720040011 22


UNIX PRACTICAL FILE

PROGRAM-4
OBJECTIVE: Execute the basic file attributes with all possible options: (i) ls (ii) chmod

i. ls

ls is a Unix/Linux shell command that lists directory contents of files and


directories.

Description
Tag

-a, --all do not ignore entries starting with .

-A, --almost-all do not list implied . and ..

--author with -l, print the author of each file

-b, --escape print octal escapes for nongraphic characters

--block-size=SIZE use SIZE-byte blocks

-B, --ignore-backups do not list implied entries ending with ~

with -lt: sort by, and show, ctime (time of last modification of file status
-c information) with -l: show ctime and sort by name otherwise: sort by ctime

-C list entries by columns

HARSH KUMAR SINGH 190720040011 23


UNIX PRACTICAL FILE

control whether color is used to distinguish file types. WHEN may be


--color[=WHEN] `never', `always', or `auto'

list directory entries instead of contents, and do not dereference symbolic


-d, --directory links

-D, --dired generate output designed for Emacs' dired mode

-f do not sort, enable -aU, disable -ls --color

-F, --classify append indicator (one of */=>@|) to entries

--file-type likewise, except do not append `*'

across -x, commas -m, horizontal -x, long -l, single-column 1,verbose -l,
--format=WORD vertical -C

--full-time like -l --time-style=full-iso

-g like -l, but do not list owner

HARSH KUMAR SINGH 190720040011 24


UNIX PRACTICAL FILE

group directories before files.augment with a --sort option, but any


--group-directories-
use of -
first
-sort=none (-U) disables grouping

-G, --no-group in a long listing, don't print group names

-h, --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G)

--si likewise, but use powers of 1000 not 1024

-H, -- follow symbolic links listed on the command line


dereferencecommand-line

--
dereferencecommandlinesymlink- follow each command line symbolic link that points to a directory
to-dir

do not list implied entries matching shell PATTERN (overridden by a


--hide=PATTERN or -
A)

--indicator- append indicator with style WORD to entry names: none


style=WORD (default),slash (p), file-type (--file-type), classify (-F)

-i, --inode print the index number of each file

-I, --ignore=PATTERN do not list implied entries matching shell PATTERN

-k like --block-size=1K

-l use a long listing format

when showing file information for a symbolic link, show information


-L, --dereference
for the file the link references rather than for the link itself

-m fill width with a comma separated list of entries

-n, --numeric-uid-gid like -l, but list numeric user and group IDs

-N, --literal print raw entry names (don't treat e.g. control characters specially)

HARSH KUMAR SINGH 190720040011 25


UNIX PRACTICAL FILE

-o like -l, but do not list group information

-p, --indicatorstyle=slash append / indicator to directories

-q, --hide-controlchars print ? instead of non graphic characters

show non graphic characters as-is (default unless program is `ls' and output
--show-control-chars
is a terminal)

-Q, --quote-name enclose entry names in double quotes

use quoting style WORD for entry names: literal, locale, shell, shellalways,
--quotingstyle=WORD
c, escape

-r, --reverse reverse order while sorting

-R, --recursive list subdirectories recursively

-s, --size print the allocated size of each file, in blocks

-S sort by file size

sort by WORD instead of name: none -U, extension -X, size -S, time -t,
--sort=WORD
version -v
with -l, show time as WORD instead of modification time: atime-u, access
--time=WORD -u, use -u, ctime -c, or status -c; use specified time as sort key if -sort=time

with -l, show times using style STYLE: full-iso, long-iso, iso,locale,
+FORMAT. FORMAT is interpreted like `date'; if FORMAT is
-time-style=STYLE FORMAT1FORMAT2, FORMAT1 applies to non-recent files and
FORMAT2 to recent files; if STYLE is prefixed with `posix-', STYLE takes
effect only outside the POSIX locale

-t sort by modification time

-T, --tabsize=COLS assume tab stops at each COLS instead of 8

with -lt: sort by, and show, access time with -l: show access time and sort by
-u
name otherwise: sort by access time

-U do not sort; list entries in directory order

-v natural sort of (version) numbers within text

HARSH KUMAR SINGH 190720040011 26


UNIX PRACTICAL FILE

-w, --width=COLS assume screen width instead of current value

-x list entries by lines instead of by columns

-X sort alphabetically by entry extension

-Z, --context print any SELinux security context of each file

-l list one file per line

--help display this help and exit

--version output version information and exit

ii. chmod chmod command is used to change the access mode of a file.

Syntax: chmod [reference][operator][mode] file...

The operator is used to specify how the modes of a file should be adjusted. The
following operators are accepted:
Operator Description
+ Adds the specified modes to the

specified classes

- Removes the specified modes from


the specified classes

HARSH KUMAR SINGH 190720040011 27


UNIX PRACTICAL FILE

= The modes specified are to be made the


exact modes for the specified classes

Permission Character Meaning on File


Read - The file is not readable. You cannot view the file contents.

r The file is readable.

Write - The file cannot be changed or modified.

w The file can be changed or modified.

Exec ute - The file cannot be executed.

x The file can be executed.

s If found in the user triplet it sets the setuid bit. If found in the group
triplet, it sets the setgid bit. It also means that x flag is set. When
the setuid or setgid flags are set on an executable file, the file is
executed with the file’s owner and/or group privileges.

S Same as s but the x flag is not set. This flag is rarely used on
files.

t If found in the others triplet it sets the sticky bit.


It also means that x flag is set. This flag is useless on files.

T Same as t but the x flag is not set. This flag is useless on file

Numeric Method:
• no permissions = 0

• x (execute) = 1

HARSH KUMAR SINGH 190720040011 28


UNIX PRACTICAL FILE

• w (write) = 2

• wx (write & execute) = 3

• r (read) = 4

• rx (read & execute) = 5

• rw (read & write) = 6

• all (read, write & execute) = 7

HARSH KUMAR SINGH 190720040011 29


UNIX PRACTICAL FILE

PROGRAM-5
OBJECTIVE: Execute basic commands using vi editor:
a. input mode commands
b. saving text and quitting

• Vi Editor is the default editor that comes with the UNIX operating system.
• Using vi editor, we can edit an existing file or create a new file from scratch.
• We can also use this editor to just read a text file.

Modes of Operation in vi editor:

There are three modes of operation in vi:

• Command Mode: When vi starts up, it is in Command Mode. This mode is where vi
interprets any characters we type as commands and thus does not display them in the
window. This mode allows us to move through a file, and to delete, copy, or paste a
piece of text.
To enter into Command Mode from any other mode, it requires pressing the [Esc]
key. If we press [Esc] when we are already in Command Mode, then vi will beep
or flash the screen.

• 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.

• Last Line Mode (Escape Mode): Line Mode is invoked by typing a colon [:], while
vi is in Command Mode. The cursor will jump to the last line of the screen and vi
will wait for a command. This mode enables you to perform tasks such as saving
files, executing commands.

HARSH KUMAR SINGH 190720040011 30


UNIX PRACTICAL FILE

Starting the Vi Editor


The following table lists out the basic commands to use the vi editor −

Command & Description

S.No.

vi filename
Creates a new file if it already does not exist, otherwise opens an existing file.

vi -R filename
Opens an existing file in the read-only mode.

view filename
Opens an existing file in the read-only mode.

Getting out of of Vi Editor

• The command to quit out of vi is :q. Once in the 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.

HARSH KUMAR SINGH 190720040011 31


UNIX PRACTICAL FILE

• The command to save the contents of the editor is :w. You can combine the above
command with the quit command, or use :wq and return.
• The easiest way to save your changes and exit vi is with the ZZ command. When
you are in the command mode, type ZZ. The ZZ command works the same way as
the :wq command.
• If you want to specify/state any particular name for the file, you can do so by
specifying it after the :w. For example, if you wanted to save the file you were
working on as another filename called filename2, you would type :w filename2 and
return.

HARSH KUMAR SINGH 190720040011 32


UNIX PRACTICAL FILE

HARSH KUMAR SINGH 190720040011 33


UNIX PRACTICAL FILE

PROGRAM-6
OBJECTIVE: Execute basic commands using vi editor
a. navigation
b. editing text
c. searching pattern
a) Moving within a File

Command & Description


Sr.No.

1
k
Moves the cursor up one line

2
j
Moves the cursor down one line

3
h
Moves the cursor to the left one-character position

4
l
Moves the cursor to the right one-character position

Command & Description


Sr.No.

1
i
Inserts text before the current cursor location

HARSH KUMAR SINGH 190720040011 34


UNIX PRACTICAL FILE

2 b) Editing
I Files
Inserts text at the beginning of the current line

3
a
Inserts text after the current cursor location

4
A
Inserts text at the end of the current line

5 o

HARSH KUMAR SINGH 190720040011 35


UNIX PRACTICAL FILE

Creates a new line for text entry below the cursor location

6
O
Creates a new line for text entry above the cursor location

c) Searching Pattern

Character &Description
Sr.No.

^
Searches at the beginning of the line (Use at the beginning of a search expression).

2
.
Matches a single character.

3
*
Matches zero or more of the previous character.

4
$
End of the line (Use at the end of the search expression).

HARSH KUMAR SINGH 190720040011 36


UNIX PRACTICAL FILE

5
[
Starts a set of matching or non-matching expressions.

6
<
This is put in an expression escaped with the backslash to find the ending or the
beginning of a word.

7
>
This helps see the '<' character description above

HARSH KUMAR SINGH 190720040011 37


UNIX PRACTICAL FILE

HARSH KUMAR SINGH 190720040011 38


UNIX PRACTICAL FILE

PROGRAM-7
OBJECTIVE: Execute the following filters using regular expressions with all possible
options:

(i) grep (ii) sed

i. grep

• The grep filter searches a file for a particular pattern of characters, and displays
all lines that contain that pattern.
• The pattern that is searched in the file is referred to as the regular expression
(grep stands for globally search for regular expression and print out).

Options:

Options Description
This prints only a count of the lines that match a pattern
-c

Display the matched lines, but do not display the filenames.


-h

-i Ignores, case for matching

-l Displays list of a filenames only.

-n Display the matched lines and their line numbers.

This prints out all the lines that do not matches the pattern
-v

Specifies expression with this option. Can use multiple times.


-e exp

-f file Takes patterns from file, one per line.

-E Treats pattern as an extended regular expression (ERE)

HARSH KUMAR SINGH 190720040011 39


UNIX PRACTICAL FILE

-w Match whole word

Print only the matched parts of a matching line, with each such part on a
-o separate output line.

-A n Prints searched line and n lines after the result.

-B n Prints searched line and n line before the result.

Prints searched line and n lines after before the result.


-C n

ii. sed
• SED command in UNIX is stands for stream editor and it can perform lots of
function on file like, searching, find and replace, insertion or deletion. Though most
common use of SED command in UNIX is for substitution or for find and replace.
By using SED, you can edit files even without opening it, which is much quicker

HARSH KUMAR SINGH 190720040011 40


UNIX PRACTICAL FILE

way to find and replace something in file, than first opening that file in VI Editor
and then changing it.
• SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
• SED command in UNIX supports regular expression which allows it perform
complex pattern matching.

HARSH KUMAR SINGH 190720040011 41


UNIX PRACTICAL FILE

PROGRAM-8
OBJECTIVE: Write a shell script to display current date and calendar
SOURCE CODE:
#shell script to display date and calendar echo -e "\n\nToday is %s\n"

"$(date)""\nenjoy your day.\n" echo -e "Mark your days

here\n""$(cal)"

HARSH KUMAR SINGH 190720040011 42

You might also like