1. Features of Linux Operating System
• MultiUser Capability
• Multitasking Capability
• Communication
• Security
– Login Security
– File Security
• Portability- Easily portable
2. Linux System Organization
• Functioning of Linux is manned in three levels
– On outer crust reside the application programs and
other utilities
– At the heart of Linux is the kernel which interacts
with the actual hardware in machine language
– The streaming of these two modes of
communication is done by middle layer . This
middle layer can be desktops, window managers
and command line shells
– The shell or command interpreter as it is called is
the mediator which interprets the commands that
we give and then conveys them to the kernel which
ultimately executes them.
3. • Three major types of shells are there in Linux
• Bourne shell
• Korn shell
• C shell
Current versions of linux incorporate all the three
shells enabling you to work in any one of them.
When you start the system , you are placed in
Bourne shell. C shell is used only for the C
programming. Its prompt is %. Bourne shell can
tackle all the tasks of korn shell but not of C shell.
The default prompt of Bourne shell is $ for the user
and # for the administrator.
As an alternative to a command line interface, Linux
provides both desktops and window managers.
4. • When using command line interface, you are given a
simple prompt at which you type in your command.
The Prompt for the user is $ whereas for the
administrator the prompt is #.
• File structure of Linux. The file system of linux starts at
a directory called the root (/). Under the root directory
are various other directories as
• proc- kernel resides in proc
• bin- contains executable files
• lib- contains all library functions
• usr- All users are in usr directory
• etc- Binary executable files required for system
administration
• tmp- temporary files created by linux or users
• dev- Device related files
5. • /
proc bin lib dev usr tmp etc
user1 user2 user3
(Root)
6. • Block distribution of Disk
In linux, the hard disk is divided into different
blocks each of the size of a multiple of 512KB.
The various blocks are:
• Boot block- containing all the files for booting
• Super block- contains temporary and empty
files. Contains information regarding number of
files that can be included in a file system
• Inode block- contains inode numbers. Each file
in linux has a unique inode number. Also
contains file permission entries. For storing all
this information, an inode table is maintained in
inode block
• Data Block – contains all the user created files
7. • Creating a file
• Two commands are there for creating files
• touch
• cat
• touch <filename>- This command creates empty files. The
arguments can be more than one file at a time
• cat- This command is used for two purposes
– For displaying the contents of the file
– For entering data into the already created file
For entering data
cat > <filename> On entering this command, the cursor will be placed at
the line next to command line. After text entry is finished, press ctrl+d to
finish
• Cat command can also be used for concatenating two files.
• cat filename1 filename2 > newsample
• This would create newsample file which contains contents of
filename1 and filename2. if newsample contains the contents,
they will be overwritten.
8. • In order to append to the previous contents
“append output redirection operator”, >>, can
be used as
• cat file1 file2 >> newfile
• Copying a file- the copying of files is done in
linux using cp command
• cp source target
• If target file does not exist it will be created and
if it exists, it will be overwritten
cp source target directory
• The files will be copied to the directory. The
addresses mentioned can be complete address
or relative address
9. • The various options of cp command are:
• -i – for invoking cp in interactive mode.
• -r – this copies the entire directory structure
• Examples
• cp -i file1 file2
• cp –r directory1 directory2
10. • Command to remove the files from linux
• rm <filename>
• Options with rm command are:
• -i option- interactive mode. Asks for
confirmation to delete
• -r option –this command deletes directories and
subdirectories.
• rm –r dir1
• -f option- removes files forcibly
11. • Renaming of the files- the renaming of the files is
done using mv command. This command is also used
for moving the files from one location to another
• mv test sample
• mv command also has the power to rename
directories
mv olddir newdir
Provided newdir is not already existing.
A group of files can be moved to a new directory with
mv command
mv file1 file2 file3 file4 directory
The various directories are separated from each other
with a forward slash (/)
12. • Directory related commands-
• pwd- This command is used to see the present
working directory
$ pwd
• Creating directory in linux- The directory can be
created in linux with the command
• mkdir <directory name>
• Changing the directory- The user can shift from one
directory to another using cd command. cd command
can be used with both absolute and relative
pathnames. For absolute pathnames, we can start the
path with a forward slash (/).
• There is a better way to of navigation available for
moving between two directories with same ancestor.
13. • Linux offers a shortcut---the relative pathname---that
uses either the current or parent directory as reference
and specifies the path relative to it. A relative
pathname uses one of these symbols
• .(a single dot)---This represents the current directory
• ..(two dots)---This represents the parent directory
• Examples:
• cd .. – Moves the user to the parent of the current
directory.
• cd ../.. - Moves the user to the parent of the parent
of the current directory. Any number of .. Can be
combined in a cd command but they should be
separated by a ‘/ ‘ .
14. • cp /home/kumar/.profile .
• This copies the .profile file to the current
directory
• Every user, when login to a linux system is
allotted a home directory by the system. This
pathname is stored in /etc/passwd file.This
pathname can be seen using the echo
command
echo $HOME
HOME is the shell variable that knows the
home directory.
15. • Removing directory- The directories in linux are
removed using the command rmdir
• rmdir <directory name>
• The directory to be removed must be empty.
We can also remove multiple directories in one
shot
• rmdir pis/data pis/prog pis
• You can’t remove a directory until you are
placed in the directory hierarchically above the
one to be removed.
16. • ls – Listing directory contents
• ls command lists all filenames to current directory
• ls <filename>--- This command is to check whether
the filename exits or not
• ls options-
• ls has a very large number of options.
• -x – When you have several files, its better to display
the filenames in multiple columns.
• Identifying the directories and executables (-F). This
option lists the files with identification marks for files
and directories. Combining this option with –x
produces a multicolumnar output as well.
• ls –Fx
• The * indicates that file contains executable code and /
refers to the directory.
17. • -a – Shows the hidden files. These are the files
that begin with a dot. These files don’t show up
normally but they can be viewed with –a option.
• ls –axF
• -R– This option lists all the files and
subdirectories in a directory tree
• ls –xR
• -r – sorts the files in reverse order
• -1 – One filename in one line
• -d – Lists only dirname if dirname is a directory
• -t – Sorts filenames by last modification time.
• -i – Displays the inode numbers
18. • -l – listing file attributes. The –l option displays
seven attributes of a file
• Number of blocks occupied by these files in
the disk each of 512 bytes
• File type and permissions
• Links
• Ownership
• Group ownership
• File size
• Last modification time
• filename
19. • ls –l – this option of ls command is used for
displaying the attributes of the file. –l stands for
long. This option displays seven attributes of
each file and subdirectory in a particular
directory. The list is preceded by words total
72, which indicates the total number of blocks
these files are accommodating. Each block
consisting of 1024 bytes. The seven attributes
displayed by the option are :
• File permissions and type- the first column
shows the type and permissions associated
with each file. The first character in this column
is mostly a ‘–’ which indicates that the file is an
ordinary file. For directories, this entry is
represented by a character ‘d’.
20. • After this first character, there are series of
characters in this column that can take the
value r,w , x or - . These character represent
the various types of permissions associated
with a file. There are three types of permissions
that a file can have
• r – read permission
• w – write permission
• x – Executable permission
• Linux follows a three tiered file protection
system that determines a file’s access rights.
The permission string is thus broken down into
three groups. Each group here represent the
21. category and contains three slots, representing the
read, write and execute permissions of the file in that
order. The read permission allows the group to read
the file, w indicates write permission, you can edit
such a file with an editor, x indicates execute
permission, the file can be executed as a program.
The presence of – indicates the absence of
corresponding permission. The three groups indicated
in the listing are:
• Owner- The one who creates the file is the owner of
the file
• Group- When opening a user account, the system
administrator assigns the user to some group. Groups
can be seen in the file /etc/group (both group id and
group name)
• Others- Other is the one who is neither the owner of
the file nor belongs to the group.
22. • Links – the second column of the ls listing
represent the number of links associated with
the file. This is actually the number of filenames
maintained by the system of that file. Linux
allows a file to have many names even though
there is a single file on the disk.
• Ownership- The third column represent the
owner of the file (the one who created the file).
The owner has full authority to tamper with the
file’s contents and permissions---a privilege not
available with others except the root user.
Similarly, you can create, modify or remove
files in a directory if you are the owner of the
directory.
23. • Group ownership- This column represents the
name of the group to which the file belongs
• File size- The fifth column represents the size
of file in bytes i.e amount of data it contains. It
is actually character count of the file and not a
measure of the disk space that it occupies. The
directories show smaller file sizes. This is to be
expected as a directory maintains a list of
filenames with an identification number (inode
number) for each file. The size of the directory
file depends on the size of this list---whatever
be the size of the files themselves.
24. • Last modification time-the sixth, seventh and eighth
columns indicate the last modification time of the file
which is stored to the nearest second. A file is said to
be modified only if its contents have changed in any
way. If we change the permissions or ownership of the
file, the modification time remains unchanged.
• Filename- The last column displays the filenames
arranged in ASCII collating sequence.
25. • ls –d –To force ls to list the attributes of a
directory, rather than its contents, -d option can
be used.
• ls –ld <directoryname> <directoryname>
• Directories are easily identified from the list by
the first character of the first column, which
here shows a d.
• For device files, this first character is identified
as either b or c.
26. • Changing the file permissions- A file or
directory is created with default set of
permissions and this default is determined by a
simple setting (called the umask). Generally,
the default setting write protects the file from all
except the owner, though all users may have
read access. To see the default permissions of
a file, create a new file and see its permissions.
• By default, For any file, the execute permission
is not given to any type of users (owner, group
or others) To do that, the permissions of the file
need to be changed. This is done with chmod
27. • The chmod command is used to set the
permissions of one or more files for all three
categories of users. It can be run only by the
owner and the super user. The command can
be used in two ways:
• In a relative manner by specifying the changes
to the current permissions
• In an absolute manner by specifying the final
permissions.
28. • Relative permissions- When changing
permissions in relative manner, chmod
changes only the permissions specified in the
command line and leaves the other
permissions unchanged.
chmod category operation permission
filename
• category includes owner, group or others
whose permissions are to be changed
• operation includes assign or remove a
permission
• permission specifies read, write or execute
permission which is to be changed.
29. • Suitable abbreviations are used for each of the
components to frame a compact expression to
be used as an argument to chmod
• Abbreviations used:
Category Operation permission
• u- owner +--Assign permission r- Read
• g- group - ---Remove permission w-Write
• o- others =--Assign absolute x-Execute
• a- all(ugo) permission
30. • To assign execute permission to the owner, the
command will be
• chmod u+x abc
To enable all catogories to have execute permissions we
can have
chmod ugo+x abc; ls –l abc
or
chmod a+x abc
If we do not specify the category, the permissions are
assigned or removed, as the case may be, to all the
categories
chmod +x abc
This will assign execute permissions to all the users for
the file abc
31. • chmod can accept more than one file name in
the command line
• chmod also accepts multiple expressions
delimited by commas
• chmod a-x,go+r abc
• More than one permission can also be set in
one chmod command
• chmod o+wx abc
32. • Absolute permissions- We can also change the
permissions of a file using a string of three octal
numbers. In this case we are not interested in
knowing the previous permissions of the file
and want to set all nine permission bits
explicitly.
• Each type of permission is assigned a number
as shown:
• Read permission---4
• Write permission----2
• Execute permission---1
33. • For each category we add the numbers that
represent the assigned permissions. For
instance, 6 represents read and write
permissions and 7 represents all permissions.
Thus if we want to assign all the permissions to
all the category of users, our each octal digit
will be 7 and the command will be
chmod 777 abc
Similarly, 000 indicates no permission to any of
the categories.
34. • Directory permissions- Directories also have their own
permissions and significance of these permissions differ from
those of ordinary files. Read and write permission of an
ordinary file are also influenced by permissions of directory
housing them.
• Read permission for a directory means that the list of filenames
stored in that directory is accessible. If a directory’s read
permission is removed, ls won’t work.
• Write permission of a directory implies that you are permitted to
create or delete files in the directory. Whether you can modify
the contents of the file in the directory lies with the
permissions of the file not of the directory
• Execute permission of a directory implies that you can traverse
it till its last subdirectory.
• By default any new file that we create has one link whereas any
new directory we create has two links. Because that directory
name appears in two directory files. For example, if dir1 is
created inside aa1 directory, one entry for the file exists in aa1
and one entry exists in dir1 itself as current directory (.) .
35. • umask: Default file and directory permissions
• When you create a file or directory , the permissions
assigned to it depend on the system’s default setting.
The linux has the following default setting
• rw-rw-rw- (666) for ordinary files
• rwxrwxrwx (777) for directories
• However these permissions are not visible. This is
because this default is transformed by subtracting the
user mask from it to remove one or more permissions.
To see the current value of umask, we can use
• umask
022
this shows an octal number which has to be subtracted
from system default to obtain the actual default. Thus
666 becomes 644 for ordinary file and 755 for
directories.
36. • umask is shell built-in command though it also
exists as an external command. A user can
also use it to set a new default.
umask 000
37. • Changing File ownership- chown command can
be used to change the owner of a file. By
default, the person who creates a file is the
default owner of a file. This can be changed as
chown options owner [:group] file(s)
chown sharma :dba dept
changes ownership to sharma and group to dba
changing ownership requires super user
permissions. The status is changed from user
to super user using su command
su
Password: ……….
38. • Changing group ownership- We can change
the group ownership using chgrp command. By
default, a group owner of a file is the one to
which the owner belongs. A user can change
the group owner but only to a group to which
he(owner) also belongs
chgrp dba dept
• The groups and their ids can be seen in file
/etc/group
root:x:0:users
Group
name Group
Password
Group ID
users
39. • Almost every UNIX system, including Linux, provides
a facility known as manual pages. These manual
pages contain online documentation for system
commands, resources, configuration files and so on.
• The command used to access manual pages is man.
If you're interested in learning about other options of
the ls command, you can type
man ls
and the manual page for ls will be displayed.
• Unfortunately, most manual pages are written for
those who already have some idea of what the
command or resource does. For this reason, manual
pages usually contain only the technical details of the
command, without much explanation. However,
manual pages can be an invaluable resource for
jogging your memory if you forget the syntax of a
command.
40. • Wildcards.
• A key feature of most Linux shells is the ability to refer to more than
one file by name using special characters. These wildcards let you refer to,
say, all file names that contain the character `` n''.
• The wildcard ``*'' specifies any character or string of characters in a file
name. When you use the character ``*'' in a file name, the shell replaces it
with all possible substitutions from file names in the directory you're
referencing.
• Here's a quick example. Suppose that Larry has the files frog, joe, and stuff
in his current directory.
• ls
• frog joe stff
• To specify all files containing the letter ``o'' in the filename, use the
command
• ls *o*
• frog joe
• As you can see, each instance of ``*'' is replaced with all substitutions that
match the wildcard from filenames in the current directory. The use of ``*'' by
itself simply matches all filenames, because all characters match the
wildcard.
41. •
The process of changing a ``*'' into a series of filenames is called
wildcard expansion and is done by the shell. This is important: an
individual command, such as ls, never sees the ``*'' in its list of
parameters. The shell expands the wildcard to include all filenames
that match. So, the command
ls *o*
is expanded by the shell to
ls frog joe
One important note about the ``*'' wildcard: it does not match file
names that begin with a single period (``.''). These files are treated as
hidden files--while they are not really hidden, they don't show up on
normal ls listings and aren't touched by the use of the ``*'' wildcard.
• We mentioned earlier that each directory contains two special entries:
``.'' refers to the current directory, and ``..'' refers to the parent
directory. However, when you use ls, these two entries don't show up.
If you use the -a switch with ls, however, you can display filenames
that begin with ``.''. The listing contains the two special entries, ``.''
and `` ..'', as well as two other ``hidden'' files--.bash_profile and
.bashrc. These two files are startup files used by bash when user logs
in.
42. • Note that when you use the ``*'' wildcard, none of the
filenames beginning with ``.'' are displayed.This is a safety
feature: if the ``*'' wildcard matched filenames beginning
with ``.'', it would also match the directory names ``.'' and
``..''. This can be dangerous when using certain commands.
• Another wildcard is ``?''. The ``?'' wildcard expands to
only a single character. Thus, ``ls ?'' displays all one-
character filenames. And ``ls termca?'' would display
``termcap'' but not ``termcap.backup''. As you can see,
wildcards lets you specify many files at one time.
• Examples
• ls j?e
• joe
• ls f??g
• frog frig
43. • Utility Commands-Linux supports various utility commands. Some of these
commands are:
• passwd- This command is used to change the login password.
• tty- this is called teletype command. This tells the filename of the terminal
• uname- Displays the name of the Operating system
uname –r -----displays the version of Operating system
uname –s----- displays the implementation name of Operating System
uname –n---- displays the machine name
• cal command- This is used to display the calendar of the month and year.
cal <monthnumber> <year>
• date command- This command displays the date with time
date--------Date and month with time is displayed
date +%m-------month number is displayed
date +%h------month name is displayed
date +%d------ Date is displayed
• echo- Used to display message
• who- used to find the persons logon to the system
who
• Username device name date time of login machine name
44. who –Hu------- This option prints the column headers and u provides additional
information
• wc command- This command is used to count lines, words and characters in a file. It
comes with the option –l, -w and –c which allow the user to count number of lines,
words and characters.
• who am i- the present settings of the terminal are displayed
• ps- In linux, every process that runs is assigned a unique process id . To know which
processes are running on your terminal, ps command is used.
• ps
• Options that can be used with ps are:
• -a option is used to see the processes of other users
• -u is used for seeing the processes of a particular user
• -f option is used to display additional information for the process
• -e option is used for seeing various other processes of the system
45. • Creating links of a file- The links to a file are established using
ln command
ln filename linkname
Created link does not occupy any new entry on hard disk but
point to the same file with a new name. Making of links to a file
avoid accidental deletion of the file. If the file with a link is
deleted, the link remains.
• More:Paging output
linux offers more pager that displays one page of the output at
a time.
ls –l | more
46. • Disk related commands- One of the major concerns
for the system administrator of a linux installation is
efficient hard disk management. Since the linux file
system is installed usually on a hard disk its
upkeeping is of primary importance
• Checking free disk space
• df command- This command reports used and free
memory on disk. The various fields that are displayed
are:
• File system name
• Number of blocks
• Used
• Available
• Used%
• Mounted on
47. • Other options used for the command are:
• -i – to display the number of inodes used and
free on the file system .df counts the blocks in
sizes of 512 bytes irrespective of actual block
size
• - T- to display the type of file system
48. • Disk usage- The du command
du reports the disk space used by directories. It
reports in terms of number of blocks occupied.
du reports details for the current directory. This
can be changed by specifying the directory
name with du
du /dev
To view only directories and not the
subdirectories, we can use
du –s /dev
49. • The ulimit command- This command is used to
avoid creation of very large sized files on a file
system that can damage a file system. It stands
for user limit and contains a value that signifies
the largest file that can be created by the user
ulimit
The limit is shown in terms of bytes. If a file
greater than this limit is created, it will be
curtailed
The limit can also be reduced by using
ulimit 1
This reduces the size to 1 block i.e 512 bytes or
1024 bytes. This change will be effective only
for the current session.
50. • sort- This command is used to sort the contents of the file.
Apart from sorting the contents, it is also used for merging two
sorted files into a third file. The sorting is done on the basis of
first character of each line. If first characters are same, second
characters are matched and so on
• sort file1 file2 file3----Sorts the contents of three files
• instead of displaying output on the screen we can store it in a
file by saying
• sort –o result file1 file2 file3
• This will sort the three files and put the contents in file result
• To avoid repeated lines in the output
• sort –u –o result file1 file2 file3
• If files are already sorted and we only have to merge them, use
–m option sort –m file1 file2
Sometimes we want to combine the contents of a file with the
input from the keyboard and then carry out the sorting
51. • We can even sort only the input from standard input
by just saying
• sort
• Sort command is also used for sorting their database
files which have its information organised in the form
of fields. For this we use sort keys. The syntax for sort
command is
• sort –r +pos1 –pos2
• Where pos1 and pos2 are the filed numbers from
where to start the sort and till which filed number
• If –pos2 is not given, key is assumed to extend till the
end of the line.
• -r indicates the sorting will be in reverser order
• To sort on a numeric field, we use the option -n
52. • cut- Like sort, cut is also a filter. It cuts or picks up a given
number of character or fields from the specified file. Say you
have a large database of employee’s information. If from this
you want to view only a few selected fields, cut is the answer.
• cut –f 2,7 filename
• this command will display the fields 2 and 7
• To cut 2 through 7
• cut –f 2-7 filename
• cut assumes that fields in the file are separated by tab , if
some other delimiter is used, we have to use –d”<delimiter
character>” with cut command as
• cut –f 2,7 –d”:” filename
• cut command can also be used to specify the number of
columns that can be cut from an input
• cut –c1-2 filename
• This will display only first two columns of each line in display
53. • Searching command-
• grep- grep is an acronym for globally search a
regular expression and print it”. The command
searches the specified input fully for a match
with the supplied pattern and displays it. While
forming the patterns to be searched, we can
use wild cards.
• grep picture newsfile
• This would search the word picture in the file
newsfile and if found the lines containing it
would be displayed on the screen
• grep picture newsfile storyfile
search in multiple files
54. • For search comprising of more than a single
word, single quotes can be used to enclose the
same
grep ‘the picture taken’ –i –n newsfile storyfile
Ignore case
Number of lines
in which pattern
was found
Single quotes for
pattern more than
one word
55. • grep realises that you may not always wish to see
what you search. Rather you want to see the lines not
containing a particular pattern. This can be done with
–v option
grep –v a* filename
this will display all the lines that do not contain a word
that start from a. Also, the lines that contain a
character ‘a’ are also matched as ‘*’ also denote zero
characters
Regular expressions wild characters used are
.-------matches on single character
*------matches repeated characters
$----- match on the end of line
[]---------matches on a class of characters
^----- match on the beginning of line
56. • Examples
• ^consists---- matches for all lines that begin
with consists
• such$-------matches lines that end with such
• B.d -----will match all the lines that contain one
character between B and d
57. • grep options
• option Meaning
• -c returns only number of matches
• -i Ignores the case while searching
• -l returns only filenames
containing a match
• -n returns line number of the matched
text as well as the text itself
• -s Suppresses error messages
• -v Return the lines that do not
contain the match
58. • Viewing files- There are two commands used
for viewing the particular portions of a file.
These are head and tail commands
• head command is used to display particular
number of lines of a file.
head -15 myfile
displays the 15 lines of the file from the
beginning
tail -15 myfile
displays the last fifteen lines of a file.
59. • I/O Redirection and Piping- In all operating
systems, there is a standard input device and a
standard output device. Sometimes it is useful
to redirect the input or output to a file or a
printer. The symbol > implies redirection of
output and the symbol < implies redirection of
input. The symbol > sends the output of a
command to a file or device such as printer.
The symbol < takes input needed for a
command from a file rather than from the
keyboard. The symbol >> adds output from the
command to the end of file without deleting the
information already in the file.
60. example cat file1 >file2
This command tries to display the output of file1
on screen but the output is redirected to the file
file2
Similarly, we can use a file as input also
cat < filename
this will display the contents of the file
We can also use both input and output
redirecting operators in a single command
cat <file1 > file2
This command takes file1 as an input and put
the contents in the file file2
61. • Piping- This command is used to connect one
command to another. It can be really useful to
redirect the output of one program so that it
becomes the input of another program.
cmd1 | cmd2
The output of one command becomes the input
for another command.
• Filters- Filters are the commands that take
input from standard input and display output on
standard output.
62. • tee command- Linux has a command to send the
output to the command to a file as well as to the
display.
who|tee logfile|sort
tee command sends the output of who to the logfile as
well as to the sort command
• tee command can also be used to send output to a
file and to the screen. This is done using the
command
who| tee file1 /dev/pts/12
It is also possible to send the errors to a file
cat file2 > newfile 2> errorfile
63. • Exercises
• Count the number of directories and files in the
current directory
• Sort the names of the users who are currently
logged and store the names in a file
• Store the calendar of current year in the file and
display the file
• Output of who should be displayed on the
screen with the value of total number of users
who have logged on displayed in the last
• Output of ls should be displayed on the screen
and from this output the lines containing the
word ‘poem’ should be counted and count
should be stored in a file file1
64. • List all the files beginning with the character ‘P’
on screen and also store them in a file called
file1
• All files present in the directory dir1 should be
deleted. Any error, if it occurs while carrying out
this operation should be stored in a file errorlog
65. • ls –l |cut –c 1|grep d| wc –l -----Number of
directories can be seen
• who | sort | cut –c 1-7 >file5
• cal 2009 | tee file1
• who|tee file1|wc –l >>file1; cat file1
• ls | tee file2 /dev/pts/12| grep file1 | wc –l >
file10
66.
67.
68. • The Vi editor- vim and gvim
• The vim editor included with most of the linux
distributions is an enhanced version of the vi editor of
unix. It includes all the features of vi editor.
• Vim and emac use a keyboard for two different
operations; to specify editing commands and to
receive character input Used for editing commands,
certain keys perform deletions, some execute changes
and others perform cursor movement.
• Usually these two functions are divided among
different keys on the keyboard
• Alphabetic keys are reserved for character input
• Function keys and control keys specify editing
commands such as deleting text or moving the cursor.
69. • The vi or vim editor operates in three modes
– Command mode
– Input or append mode
– Execution or ex mode
• In command mode , all the keys on the keyboard
become editing commands; in the input mode keys on
the keyboard become input characters.
• Some of the editing commands such as a or i enter
the input mode. On pressing i key, you leave the
command mode and enter the input mode. Pressing
ESC key returns the user to the command mode.
• With vim, you can use ctrl+O command to jump
quickly to the command mode and enter a command.
• Although the vi command mode handles most editing
operations, it cannot perform some, such a file saving
and global substitutions. For such operations, you
need to enter the line editing mode which can be done
by pressing a colon. This is the ex mode
70. • vim <filename>
• After executing the vim command, you enter Vi’s
command mode. Each key becomes a vi editing
command , and the screen becomes a window into the
text file. Text is displayed screen by screen
• After entering the text, you can leave the input mode
and return to command mode. For saving the file,
• Press :ZZ and enter--- will save the file and exit
• To save file while editing, press :w and press enter
• To quit the editing session, you can use :q. it does not
perform any saving. However if you have done some
changes to the file before exiting, you will not be
allowed to exit. To override this use, :q!
• To obtain online help, enter :help
•
71. • gvim- This is another editor provided which has
additional menu buttons on the top which can
be activated by mouse clicks. Other facilities
like cut, copy, paste etc are also provided.
72. h Left one character
l Right one character
k Up one line
j Down one line
w Forward one word
W Forward one space- delimited word
b Back one word
B Back one space- delimited word
e End of the next word
E End of next space- delimited word
0 Beginning of line
$ End of the line
Enter Beginning of the next line
- Beginning of the previous line
( Beginning of sentence
) End of sentence
{ Beginning of para
} End of para
Ctrl+F Moves forward by a screen of text
Ctrl+B Moves Back by a screen of text
73. • Commands for positioning the cursor in window
h Moves the cursor one character to
the left
Backspace Moves the cursor one character to
the left
l Moves the cursor one character to
the right
spacebar Moves the cursor one character to
the right
0 Moves the cursor to the beginning of
the current line
$ Moves the cursor to the end of the
current line
74. • Positioning by line
j Moves the cursor down by one line
in the same column
k Moves the cursor up by one line in
the same column
+ Moves the cursor down to the
beginning of the next line
- Moves the cursor upto the beginning
of the previous line
enter Moves the cursor down to the
beginning of the next line
75. • Positioning by word
w Moves the cursor to the right, to the
first character of the next word.
b Moves the cursor back to the the
first character of the previous word.
e Moves the cursor to the end of the
current word
76. • Positioning in the window
H Moves the cursor to the first line on the
screen, or home
M Moves the cursor to the middle line on
screen
L Moves the cursor to the last line on
screen
77. • Positioning in a file
Ctrl+f Scrolls the screen forward a full window,
revealing the window of text below the
current window
Ctrl+b Scrolls the back a full window, revealing
the window of text above the current
window
78. • Commands for inserting text
a Enter text input mode and appends the text after the
cursor
i Enter text input mode and inserts text text at the cursor
A Enters text input mode and appends text at the end of
current line
o Enter text input mode by opening a new line immediately
below the current line
R Enters the text input mode and overwrites from the
current cursor position onwards
O Enter text input mode by opening a new line immediately
above the current line
I Enters the text input mode and inserts text at the
beginning of current line
79. • Commands for deleting text (work in command mode)
x Deletes the character at the current cursor position
X Deletes the character to the left of the current cursor
position
dw Deletes the word from the cursor to the next space or to
the next punctuation
dd Deletes the current line
nx, ndw, ndd Deletes n characters, n words or n lines
d0 Deletes the current line from the cursor to the beginning
of the line
d$ Deletes the current line from cursor to the end of the line
80. • Miscellaneous Commands
. Repeats the action performed by the last command
u Undoes the effect of last command
U Restores all changes to the current line since you
moved the cursor to this line
J Joins the line immediately below the current linewith the
current line
~ Changes the character at the current cursor position
from upper to lower case and vice versa
:sh Temporarily returns to the shell to perform some shell
commands. Type exit to return to vi
Ctrl + l Clears and redraws the current window
81. • Commands for quitting vi
ZZ Write the buffer to the file and quits vi
:wq Write the buffer to the file and quits vi
:w filename
and :q
Write the buffer to the file filename and quits vi
:w! filename
and :q
Overwrites the existing file filename with the contents of
the buffer and quits vi
:q! Quits vi whether or not changes made to the buffer were
written to a file
:q Quits vi if changes made to the buffer were written to a
file
82. • Block commands- these are the commands that apply
to a block of text. For all the block commands,
following things hold:
• All the block commands work in ex command mode
• Line numbers should be associated with the text
before we issue any block commands. This is so
because block commands need to be told the line
numbers on which they are supposed to operate.
Inorder to make the line numbers displayed along
with the lines, we have to set number in ex mode
:set number
Use of : makes it a line editing command which is
issued at the bottom of the screen. The lines of the file
are numbered from 1 onwards
83. • Various block commands are
:nd Deletes nth line
:m,n d Deletes m to n lines where m and n are line numbers
:n mo p Moves line n after line p
:m,n mo p Moves lines m to n after line p
:m co p Copies line m after line p
:m,n co p Copies lines m to n after line p
:m,n w filename Writes lines m to n to a file
:m,n w >>
filename
Appends lines m to n to a file
: r filename Reads the contents of the file filename at current cursor
position
:r !comamnd Executes the shell command and output of the command is
read at the current cursor position
84. Command Function
/pattern Searches forward in the buffer for the next
occurrence of the pattern of text. Positions
the character under the first character of the
pattern
?pattern Searches backwards in the buffer for the first
occurrence of the patterns of text.
n Repeats the last search command
N Repeats the search command in the
opposite direction
85. • While searching a pattern you may want to
search all occurrences of the word irrespective
of its case. In such a case, we can set to
ignore the case
• :set ignorecase or :set ic
• Find and replace- We can also replace the
found pattern with a new one in vi editor
86. Command Function
:s/str1/str2 Replaces the first occurrence of str1 with
str2 in current line
:
s/str1/str2/g
Replaces all occurrences of str1 with str2 in
current line
:m,n
s/str1/str2/g
Replaces all occurrences of str1 with str2
from lines m to n
:1,$
s/str1/str2/g
Replaces all occurrences of str1 with str2
from lines 1 to last
:1,.
s/str1/str2/g
Replaces all occurrences of str1 with str2
from first line to current
:.,$
s/str1/str2/g
Replaces all occurrences of str1 with str2
from current to last
87. Command Function
:set nu Set display of line numbers on
:set nonum Set display of line numbers off
:set eb Beep the speaker when an error occurs
:set noeb Do not beep the speaker
:set ai Set autoindent on
: set noai Set auto indent off
: set ic Set ignorecase on
: set noic No ignore case
To get a list of all set options use
:set all
88. • Multiple file editing in vi
• Vi permits editing of multiple files at one go.
• vi filename1 filename2 filename3….
• The first file given as argument is opened for
editing
• At any moment , to find out the number of files
loaded for editing
• : args
• This displays the list of files with the name of
the file being currently edited enclosed within
the square brackets []
89. • After editing the current file, we can shift to next
file by typing:
• :n
• The shift to next file is only allowed if the
current file is not edited . If the current file is
edited, we have to use
• :n!
• Unlike n, there is no command available for
editing the previous file. However we can go to
the first file for editing using’
• :rew
• or :rew!
90. • If a new file is to be opened for editing instantly
• :e filename
• This filename will not be added to the previous
list of filenames.
• :f
• Displays the name of the current file
91. • Command line options in vi
Command Function
vi +100 file Loads file and places cursor on the 100th
line in the file
vi +/pattern file Loads file and places cursor on the first
occurrence of the matching pattern
view file Displays file in the read only mode of vi.
Any changes made to the buffer will not
be allowed to be written to the file
92. • Shell Script- A shell script or a shell program is a
series of commands given to the interpreter at one
time. Instead of specifying one job at a time, we give
the shell a to-do list –a program that carries out an
entire procedure. Such programs are known as shell
scripts.
• The script can be written using any of the editors of
linux. The script files are not executable and has to be
made executable using chmod command.
• When we execute a command at the dollar prompt,
they are executed in the shell that was invoked when
we logged in. As against this, when we execute a shell
script, the login shell creates a new shell,a new
command interpreter and waits idly in the background
while the new shell executes our shell script.
93. • Inorder to run a shell script,
• Type the script in vi editor
• After saving the text,
• sh <filename>
• Inorder to run the file with its name only, we
have to change the permissions of file to get
executable permission. This is done using
• chmod u+x filename
• After getting executing permissions, we can run
it as
• ./filename
94. • Interactive shell script- Two basic words in shell
vocabulary read and echo are used for making
interactive shell scripts.
• echo is to display a message
• read is used for taking input from the user