Unit 5 OS

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

Unit 5

os
linux
• Linux is the best-known and most widely used open source
operating system. One of the biggest advantages of Linux is that it
offers a much wider range of compatible hardware over other
operating systems (such as Mac OS, or Windows).
• In linux, everything on the computer system is a file. There are
many types of files (and some files are more special than others),
but you should keep in mind that essentially everything is some
sort of file: text files are files the way you normally think of them,
images are another sort of normal file, directories are actually files
that contain other files, commands are files that tell the shell what
to do, programs (usually called executables as opposed to the
program source code files) are another form of file that tells the
computer what to do, etc. Even physical devices such as hard-
drives are represented by device files.
Shell:- It is an interface among the kernel and
user. It can afford the services of kernel. It can take
commands through the user and runs the
functions of the kernel. The shell is available in
distinct types of OSes. These operating systems
are categorized into two different types, which are
the graphical shells and command-line shells.
• The graphical line shells facilitate the graphical
user interface, while the command line shells
facilitate the command line interface. Thus, both
of these shells implement operations. However,
the graphical user interface shells work slower as
compared to the command-line interface shells.
ADVANTAGES OF OS
1. open Source: As it is open-source, its source code is easily available. Anyone
having programming knowledge can customize the operating system. One
can contribute, modify, distribute, and enhance the code for any purpose.
2. Security : The Linux security feature is the main reason that it is the most
favorable option for developers. It is not completely safe, but it is less
vulnerable than others. Each application needs to authorize by the admin
user. The virus is not executed until the administrator provides the access
password. Linux systems do not require any antivirus program.
3. Free Certainly, the biggest advantage of the Linux system is that it is free to
use. We can easily download it, and there is no need to buy the license for it.
It is distributed under GNU GPL (General Public License). Comparatively, we
have to pay a huge amount for the license of the other operating systems.
4. Lightweight Linux is lightweight. The requirements for running Linux are
much less than other operating systems. In Linux, the memory footprint and
disk space are also lower. Generally, most of the Linux distributions required
as little as 128MB of RAM around the same amount for disk space.
5. Stability Linux is more stable than other operating
systems. Linux does not require to reboot the system to
maintain performance levels. It rarely hangs up or slow
down. It has big up-times.
6. Performance Linux system provides high performance
over different networks. It is capable of handling a large
number of users simultaneously.
7. Flexibility Linux operating system is very flexible. It can
be used for desktop applications, embedded systems,
and server applications too. It also provides various
restriction options for specific computers. We can
install only necessary components for a system.
8. Multitasking : It is a multitasking operating system as it can run multiple
tasks simultaneously without affecting the system speed.
9. Privacy: Linux always takes care of user privacy as it never takes much
private data from the user. Comparatively, other operating systems ask
for the user's private data.
10. Networking : Linux facilitates with powerful support for networking.
The client-server systems can be easily set to a Linux system. It provides
various command-line tools such as ssh, ip, mail, telnet, and more for
connectivity with the other systems and servers. Tasks such as network
backup are much faster than others.
11.Compatibility: Linux is compatible with a large number of file formats as
it supports almost all file formats.
12. Installation: Linux installation process takes less time than other
operating systems such as Windows. Further, its installation process is
much easy as it requires less user input. It does not require much more
system configuration even it can be easily installed on old machines
having less configuration.
Linux Features

• Multiuser capability: Multiple users can access the same system


resources like memory, hard disk, etc. But they have to use
different terminals to operate.
• Multitasking: More than one function can be performed
simultaneously by dividing the CPU time intelligently.
• Portability: Portability doesn't mean it is smaller in file size or can
be carried in pen drives or memory cards. It means that it support
different types of hardware.
• Security: It provides security in three ways namely authenticating
(by assigning password and login ID), authorization (by assigning
permission to read, write and execute) and encryption (converts
file into an unreadable format).
• Live CD/USB: Almost all Linux distros provide live CD/USB so that
users can run/try it without installing it.
• Graphical User Interface (X Window system): Linux is
command line based OS but it can be converted to GUI
based by installing packages.
• Support's customized keyboard: As it is used
worldwide, hence supports different languages
keyboards.
• Application support: It has its own software repository
from where users can download and install many
applications.
• File System: Provides hierarchical file system in which
files and directories are arranged.
• Open Source: Linux code is freely available to all and is
a community based development project.
Types of Files:

In Linux system, everything is a file and if it is not a file, it is a


process. A file doesn't include only text files, images and compiled
programs but also include partitions, hardware device drivers and
directories. Linux consider everything as as file. Files are always
case sensitive. 
TYPES OF FILES:
• Regular files (-): It contain programs, executable files and text files.
• Directory files (d): It is shown in blue color. It contain list of files.
• Special files
– Block file (b)
– Character device file (c)
– Named pipe file (p)
– Symbolic link file (l)
– Socket file (s)
Regular or ordinary files

• Regular or ordinary files store data of various content types


such as text, audio, video, images, scripts, and programs.
There are hundreds of content types. In Linux, regular files
can be created with or without an extension.
• An extension is a group of characters that is used with the
file name to give it a special identity or to group it with files
of the similar content type. For easy recognition and
processing, files of different content types often use well-
known file extensions.
• Although the Linux file system does not need file extensions,
still you should use them. They help us in identifying the
types of content that are stored in files. For example, if a file
has a .mp4 extension, you may know that it is a video file.
Directory files

• To organize files in a hierarchy, file systems use


directories. Directories are also files, but instead of
storing data, they store the location of other files.
To store the location of files placed in the directory,
the directory uses directory entries. Each directory
entry stores the name and location of a single file.
• Linux file system starts with a directory called / or
root directory. All files and directory files are
created under this directory. Except the root
directory, each directory has a parent directory.
Special files
• Linux treats all hardware devices (such as hard drives, printers, monitors,
terminal emulators, and CD/DVD drives) as special files. This means that an
application program can access and use files and devices in the same way. This
feature makes developing programs in Linux easier and flexible.
• Linux places all special files or device files under the /dev directory. There are
two types of special files: a character special file and a block special file. A
character special file represents a device that transfers data in bytes such as a
monitor or a printer. A block special file represents a device that transfers data in
blocks such as a hard drive.
• Link files
• Link files allow us to use a file with a different filename and from a different
location. For this, we use link files. A link file is a pointer to another file. There
are two types of links: a hard link and a symbolic or soft link.
• A hard link creates a mirror copy of the original file. A hard link cannot be
created to a directory or a file on another filesystem. A soft or symbolic link
creates a pointer to the original file. A soft link can be created to a directory or a
file on another filesystem.
• Socket files: A socket is a communication endpoint that applications
use to exchange data. For example, if an application wants to
communicate with another application, it connects with the socket of
that application. Each application that provides services to other
applications or remote clients uses a socket to accept connections.
• Each socket has an associated IP address and port number that allow
it to accept connections from clients. For example, if an application of
the local system wants to communicate with another application of a
remote system, it connects to the socket of that application by using
the associated IP address and port number of that socket.
• Sockets are very complicated. To make the communication process
easier between local applications, Linux uses socket files. Socket files
allow applications of the local system to exchange data without going
through the complex process of networking and sockets.
• Socket files are the special files that use a file name as their address
instead of an IP address and port number. Socket files the use
 sendmsg() and recvmsg() system calls to enable inter-process
communication between local applications.
• Named pipe files: Linux allows us to send the
output of any process or command to another
process or command as the input. This feature is
known as the pipe. Pipes work only when both
processes are started by the same user and exist in
the same parent process space. If processes are
executed under different user names and
permissions, then standard pipes do not work. In
such circumstances, named pipes are used. Named
pipes are similar to the standard pipes except that
they can be accessed as part of the filesystem.
File Handling Commands

• pwd Command: Pwd returns the full path name of the


current remote working directory. In the above example, the
command output is /home/abc, which implies that we are
working in the abc directory, which is inside the home
directory.
• Syntax:
$ pwd -L: Prints the symbolic path.
$ pwd -P: Prints the actual path.
• mkdir Command “mkdir”  also known as “ make directory”.
This command is used to create single or multiple directories.
Syntax:
• mkdir <options> <directories>
• cd Command: The command cd also sometimes called as
chdir , it allows you to move between directories. The cd
command accepts an argument, generally the name of the
folder to which you wish to move. Thus the entire command
is cd your-directory.
• Syntax :
$ cd <directory_name> (Will move to the desired directory)
$ Cd .. (will move back to the previous directory)
• ls Command The “ls” command lists files that present the
directory you want to work with, except for hidden files. In
the above example, ls list all the files present in the
directory named as xyz.
• Syntax :
$ ls
• touch Command: The primary use of the touch command is to alter a
timestamp. The function is commonly used for file creation, even though
this is not its core role. The terminal program may modify and access
times for any specified file. The touch command only creates a file if it
does not already exist.
• Syntax: $ touch <options> <file or directory name>
option meaning
-a Changes the access time
-c if the file does not exist, do not create it

-d update the access and modification times

-m change the modification time only

-r use the access and modification times of


the file
-t creates a file using a specified time
Cat Command: Cat or concatenate is often used in
Linux. It reads the files' content and gives the content's
output on the terminal screen. example, use the
command cat to read the file1.txt ,we use the
command cat to read the file1.txt and see the output
below the command.
• mv Command The mv command is often used in Linux;
this command is used to move a file from one directory
to another directory. There is also a second use of the
mv command. We can change the name of any existing
file to a new name, whichever we desire.
changing the name of a.txt to b.txt. Here we are using
the mv command, which will change the name of the
file a.txt to b.txt.
• Syntax: 
$ mv <filename> <new file name>
• cp Command: Cp means to copy. The
command is used to copy files or groups of
files. In the above example, we are using the cp
command to copy the content of file3.txt and
will paste it into the file named file1.txt.
Syntax: cp <file/directory-sources> <destination>
• rm Command :“rm”  also know as “remove” .
This command is used to remove a file. If we
use the rm command, the file will be
permanently deleted. In GUIs, we got recycle
bin and trash for recovering files. So we need
to take extra care while using the rm
command.
Renaming a File: mv command could be used
to rename a file. It will rename the filename to
new_filename or in other words, it will
remove the filename file and would be
creating a new file with the new_filename
with the same content and name as that of
the filename file.
$mv filename new_filename
• rmdir Command:The rmdir command deletes
an empty directory. For example, to delete or
remove the abc directory, run the command:
$ rmdir abc
File Permissions

All the three owners (user owner, group, others) in the


Linux system have three types of permissions defined.
Nine characters denotes the three types of permissions.
• Read (r) : The read permission allows you to open and
read the content of a file. But you can't do any editing or
modification in the file.
• Write (w) : The write permission allows you to edit,
remove or rename a file. For instance, if a file is present
in a directory, and write permission is set on the file but
not on the directory, then you can edit the content of
the file but can't remove, or rename it.
• Execute (x): In Unix type system, you can't run or
execute a program unless execute permission is set. But
in Windows, there is no such permission available.
Linux file ownership
Every Linux system have three types of owner:
• User: A user is the one who created the file. By
default, whosoever, creates the file becomes the
owner of the file. A user can create, delete, or
modify the file.
• Group: A group can contain multiple users. All
the users belonging to a group have same access
permission for a file.
• Other: Any one who has access to the file other
than user and group comes in the category
of other. Other has neither created the file nor is
a group member.
Let’s see file permissions in Linux with examples:
• ls – l on terminal gives
permission On directory On file

r (read) read file content (cat) Read directory content (ls)

w (write) change file content (vi) create file in directory


(touch)

x (execute) execute the file enter the directory (cd)


Operator Description
+ Adds a permission to a file or directory
– Removes the permission
Sets the permission and overrides the
=
permissions set earlier.

User Denotations

u user/owner

g group

o other
Number Number Number
Permission Permission Permission

TypeSymbol TypeSymbol TypeSymbol


0
No Permission —
1
Execute –x
2
Write -w-
3 Execute + Write -wx
4
Read r–
5
Read + Execute r-x
6
Read +Write rw-
7
Read + Write +Execute rwx
Permission Set

File permissions for (-rw-rw-r--)


• How to Change Directory Permissions in Linux for
the Group Owners and Others?
The command for changing directory permissions
for group owners is similar, but add a “g” for group
or “o” for users:
• Linux chmod command is used to change the
access permissions of files and directories. It stands
for change mode. It can not change the permission
of symbolic links. Even, it ignores the symbolic links
come across recursive directory traversal. In
the Linux file system, each file is associated with a
particular owner and have permission access for
different users. The user classes may be:
Owner, Group Member,Others (Everybody else)
The file permissions in Linux are the following three
types:
read (r)
write (w)
execute (x)

$chmod <groupName>+<permissionName> <fileNam
e>  

• chmod g+w filename
• chmod g-wx filename
• chmod o+w filename
• chmod o-rwx foldername
Changing ownership in Linux:

• Linux chown command is used to change a file's ownership,


directory, or symbolic link for a user or group. The chown
stands for change owner. In Linux, each file is associated
with a corresponding owner or group.
• The Linux system may have multiple users. Every user has a
unique name and user ID. If only a user is available in the
system, the user will be the owner of each file.
• The Linux system may have multiple users. Every user has a
unique name and user ID. If only a user is available in the
system, the user will be the owner of each file.
• Users can be listed in different groups. The group allows us
to set permission on the group level instead of setting
permission on an individual level.
Permissions and Ownership: To secure and protect
directories and files in Linux, we can use permissions for
controlling what a user can implement with a directory
or file. Linux utilizes three different types of permissions,
which are mentioned below:
• Read: It permits the user for reading files and enables
the user for reading directories or subdirectories saved
inside it.
• Write: It permits the user to delete and modify a file. It
also permits a user to change its contents (delete, create,
and rename files inside it) for directories. Changes make
an impact on them until the execute permission isn't
provided to the directory.
• Execute: It permits it to get run on a file. For instance, if
we have any file called exe.sh until we don't provide with
it the execute permission, it will not run.
chown [OPTION]... [OWNER][:[GROUP]] FILE...  
Linux expr command
• The expr command is used to evaluate a given expression and display its
standard output. Each separated expression is considered as an argument.
These expressions could be integer and string expressions, including
regular expressions. If expressions are not passed properly, it will prevent
the execution of the command. The expr command supports the following
operators:
for integer: addition, subtraction, multiplication, division, and modulus.
For strings: regular expression, set of characters in a string.
• It will be useful if we want to perform an operation while working on the
terminal, such as searching for a substring in a string, searching its index,
performing arithmetic operations, and more. So, the expr command allows
us to perform all these tasks from the terminal.
• The expr command supports the following options to make it more useful.
Options:
• --help: The 'help' option will display its documentation that contains its
description and other useful information. If we get stuck while using the
expr command, it will be . To get help, execute the command as follows:
• $ expr --help  
• --version: It is used to display the version
information. It will display the information, such
as it is open-sourced, warranty info, its specific
version, and the author's name. To display the
version info, execute the command as follows:
• $ expr --version  
• To perform basic arithmetic operations, execute
the expr command, and pass the integers and
operators on it. Remember, every
token(expressions) should be separated by a
space. Consider the following examples:
• Addition: Add two numbers, 15 and 12.
• $ expr 15 + 12  
GREP COOMAND IN LINUX
• The 'grep' command stands for "global regular expression print". grep
command filters the content of a file which makes our search easy.
• It is a command-line utility to search plain-text data groups for lines
that are the same as a regular expression. The name "grep" comes
from the command, i.e., ed, which contains the same effect.
• The grep filter finds a file for a specific character pattern and shows
every line that includes that pattern. Characters should be in quotation
marks if they occur in the pattern parameter with a special meaning
for the shell. Usually, we must enclose the whole pattern in one
quotation mark if the pattern parameter is not a common string.
• In an expression like [a-z], the minus sign (-) cml describes a range
based on the current collating order. A collating order may specify
equivalent classes in character ranges for use. The grep command
assumes stdin when no files are mentioned.
$ grep "string" file name
GREP OPTIONS

• -i, --ignore-case: Ignores case distinctions in patterns and input data.


grep -i <searchWord> <fileName> 
• -v, --invert-match: Selects the non-matching lines of the provided input pattern.
grep -v <searchWord> <fileName>  
• -n, --line-number: Prefix each line of the matching output with the line number
in the input file.
grep -n <searchWord> <fileName>  
• -w: Find the exact matching word from the input file or string.
grep -w<searchWord> <fileName>  
• -c: Count the number of occurrences of the provided pattern.
grep -c <searchWord> <fileName>  
• In the coming examples, we will use the
file fruits.txt with the following content:
apples and pears
citrus – oranges, grapefruits, mandarins and limes
stone fruit – nectarines, apricots, peaches and plums
tropical and exotic – bananas and mangoes
berries – strawBERRIES, raspberries, blueberries, kiwifruit and passionfruit
melons – watermelons, rockmelons and honeydew melons tomatoes and
avocados.
How to Find the Line Numbers Against Matching Input using -n
There are times when we want to get the line numbers against the
matching string. For that, we can supply the -n flag to grep like this:
$grep -n "berries" fruits.txt
LINE 5: berries – strawBERRIES, raspberries, blueberries, kiwifruit
and passionfruit
• How to Find a Matching String with grep
• If we want to find the string "fruit" in the file fruits.txt, we can do
so like this:
$ grep "fruit" fruits.txt.
citrus – oranges, grapefruits, mandarins and limes stone fruit –
nectarines, apricots, peaches and plums tropical and exotic –
bananas and mangoes berries – strawBERRIES, raspberries,
blueberries, kiwifruit and passionfruit
• How to Ignore Case Distinctions using -i
We can command grep to return results while ignoring the case
of the matching string. Let's find the word "berries" from our
sample file. It should match all occurrences of "berries"
regardless of their case.
$grep -i "berries" fruits.txt
berries – strawBERRIES, raspberries, blueberries, kiwifruit and
passionfruit
How to Select the Non-Matching Lines using -v .We can reverse the results of the
grep command to include non-matching results. Let's say, if we want to get all the
lines that do not contain the word "berries", the command would look like this:
$grep -v "berries" fruits.txt
// apples and pears citrus – oranges, grapefruits, mandarins and limes stone fruit –
nectarines, apricots, peaches and plums tropical and exotic – bananas and mangoes
melons – watermelons, rockmelons and honeydew melons tomatoes and avocados.
How to Find the Exact Matching Word from the Input File or String using -w
• If you were to match an exact word rather than just the occurrence of a pattern, you
can do so by using the -w flag.
• If we grep "fruit" without any flags, it would return any occurrence of the word
"fruit".  This would include occurrences like "grapefruit", "dragonfruit" and so on like
this:
$ grep "fruit" fruits.txt.
// citrus – oranges, grapefruits, mandarins and limes stone fruit – nectarines,
apricots, peaches and plums tropical and exotic – bananas and mangoes berries –
strawBERRIES, raspberries, blueberries, kiwifruit and passionfruit
• But, if we only want "fruit" in our results, we can use the -w flag like this:
$grep -w "fruit" fruits.txt
// stone fruit – nectarines, apricots, peaches and plums
• How to Count the Number of Occurrences of
the Provided Pattern using -c
• We can count the number of times the
matched string appears in the file. This is how
the -c flag works:
$grep -c "fruit" fruits.txt
// 4
The word "fruit" appears 4 times in the file.
RELATIONAL OPERATORS IN LINUX

the following relational operators that are specific to numeric


values. These operators do not work for string values unless their
value is numeric.Assume variable a holds 10 and variable b holds
20 then −
• -eq: Checks if the value of two operands are equal or not; if yes,
then the condition becomes true.[
$a -eq $b ] is not true.
• -ne: Checks if the value of two operands are equal or not; if
values are not equal, then the condition becomes true.
[ $a -ne $b ] is true.
• -gt: Checks if the value of left operand is greater than the value of
right operand; if yes, then the condition becomes true.
[ $a -gt $b ] is not true.
• -lt: Checks if the value of left operand is less than
the value of right operand; if yes, then the
condition becomes true.
[ $a -lt $b ] is true.
• -ge: Checks if the value of left operand is greater
than or equal to the value of right operand; if yes,
then the condition becomes true.
[ $a -ge $b ] is not true.
• -le: Checks if the value of left operand is less than
or equal to the value of right operand; if yes, then
the condition becomes true.
[ $a -le $b ] is true
LOGICAL OPERATORS IN LINUX
• Suppose a=10 and b=20;
• !: This is logical negation. This inverts a true
condition into false and vice versa.[ ! false ] is
true.
• -o: This is logical OR. If one of the operands is
true, then the condition becomes true.
[ $a -lt 20 –o $b -gt 100 ] is true.
• -a:This is logical AND. If both the operands are
true, then the condition becomes true
otherwise false.[ $a -lt 20 -a $b -gt 100 ] is
false.
STRING OPERATORS 
Assume variable a holds "abc" and variable b holds "efg" then −
= :Checks if the value of two operands are equal or not; if yes, then the
condition becomes true.
[ $a = $b ] is not true.
!= : Checks if the value of two operands are equal or not; if values are not
equal then the condition becomes true.
[ $a != $b ] is true.
-z :Checks if the given string operand size is zero; if it is zero length, then it
returns true.
[ -z $a ] is not true.
-n :Checks if the given string operand size is non-zero; if it is nonzero length,
then it returns true.
[ -n $a ] is not false.
Str :Checks if str is not the empty string; if it is empty, then it returns false.
[ $a ] is not false.
Shell script to print two strings

  
#Declaring the first String   
str1="We welcome you"  
  
#Declaring the Second String  
str2=" on GEHU."  
  
#Combining first and second string  
str3="$str1$str2"  
  
#Printing a new string by combining both   
echo $str3  
# OUTPUT We welcome you on GEHU

You might also like