0% found this document useful (0 votes)
9 views

SE Operating System Manual+Programs-rpc 10 prog

This document provides an introduction to basic UNIX commands and the BASH shell, highlighting its features and differences from the DOS command prompt. It covers command syntax, special characters, navigating the Linux file system, and using built-in help for commands. Additionally, it explains file manipulation commands and techniques for finding files within the Linux environment.

Uploaded by

hardikpatil672
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)
9 views

SE Operating System Manual+Programs-rpc 10 prog

This document provides an introduction to basic UNIX commands and the BASH shell, highlighting its features and differences from the DOS command prompt. It covers command syntax, special characters, navigating the Linux file system, and using built-in help for commands. Additionally, it explains file manipulation commands and techniques for finding files within the Linux environment.

Uploaded by

hardikpatil672
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/ 33

Shri Sant Gadge Baba College of Engineering & Technology,

Bhusawal
Department of Computer Science & Engineering
Class: S.E Sem-IV

Shri Sant Gadge Baba College of Engineering & Technology,


Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : ________________________ Date :___________________________________

Aim: Introduction to some basic commands of UNIX.

Theory:

What is a command shell?

A program that interprets commands

Allows a user to execute commands by typing them manually at a terminal, or


automatically in programs called shell scripts.

A shell is not an operating system. It is a way to interface with the operating system and
run commands.

What is BASH?

BASH = Bourne Again SHell

Bash is a shell written as a free replacement to the standard Bourne Shell (/bin/sh)
originally written by Steve Bourne for UNIX systems.

It has all of the features of the original Bourne Shell, plus additions that make it easier to
program with and use from the command line.

Since it is Free Software, it has been adopted as the default shell on most Linux systems.

How is BASH different from the DOS command prompt?


Case Sensitivity: In Linux/UNIX, commands and filenames are case sensitive,
meaning that typing “EXIT” instead of the proper “exit” is a mistake.

“\” vs. “/”: In DOS, the forward-slash “/” is the command argument delimiter, while
the backslash “\” is a directory separator. In Linux/UNIX, the “/” is the directory
separator, and the “\” is an escape character. More about these special characters in a
minute!

Filenames: The DOS world uses the “eight dot three” filename convention, meaning
that all files followed a format that allowed up to 8 characters in the filename, followed
by a period (“dot”), followed by an option extension, up to 3 characters long (e.g.
FILENAME.TXT). In UNIX/Linux, there is no such thing as a file extension. Periods can
be placed at any part of the filename, and “extensions” may be interpreted differently by
all programs, or not at all.

Special Characters
Before we continue to learn about Linux shell commands, it is important to know that
there are many symbols and characters that the shell interprets in special ways. This
means that certain typed characters: a) cannot be used in certain situations, b) may be
used to perform special operations, or, c) must be “escaped” if you want to use them in a
normal way.

Charact Description
er
\ Escape character. If you want to reference a special character, you must
“escape” it with a backslash first.

Example: touch /tmp/filename\*


/ Directory separator, used to separate a string of directory names.

Example: /usr/src/linux
. Current directory. Can also “hide” files when it is the first character in a
filename.
.. Parent directory
~ User's home directory
* Represents 0 or more characters in a filename, or by itself, all files in a
directory.

Example: pic*2002 can represent the files pic2002, picJanuary2002,


picFeb292002, etc.
? Represents a single character in a filename.

Example: hello?.txt can represent hello1.txt, helloz.txt, but not

hello22.txt
[] Can be used to represent a range of values, e.g. [0-9], [A-Z], etc.

Example: hello[0-2].txt represents the names hello0.txt, hello1.txt, and


hello2.txt
| “Pipe”. Redirect the output of one command into another command.

Example: ls | more
> Redirect output of a command into a new file. If the file already exists,
over-write it.

Example: ls > myfiles.txt


>> Redirect the output of a command onto the end of an existing file.

Example: echo “Mary 555-1234” >> phonenumbers.txt


< Redirect a file as input to a program.

Example: more < phonenumbers.txt


; Command separator. Allows you to execute multiple commands on a single
line.

Example: cd /var/log ; less messages


&& Command separator as above, but only runs the second command if the
first one finished without errors.

Example: cd /var/logs && less messages


& Execute a command in the background, and immediately get your shell
back.

Example: find / -name core > /tmp/corefiles.txt &

Executing Commands
The Command PATH:
Most common commands are located in your shell's “PATH”, meaning that you can just
type the name of the program to execute it.

Example: Typing “ ls” will execute the “ ls” command.

Your shell's “PATH” variable includes the most common program locations, such as

/bin, /usr/bin, /usr/X11R6/bin, and others.

To execute commands that are not in your current PATH, you have to give the complete
location of the command.

Examples: /home/bob/myprogram

./program (Execute a program in the current directory)

~/bin/program (Execute program from a personal bin directory)

Command Syntax
Commands can be run by themselves, or you can pass in additional arguments to
make them do different things. Typical command syntax can look something like
this:
command [-argument] [-argument] [--argument] [file]
Examples: ls List files in current directory
ls -l Lists files in “long” format
ls -l --color As above, with colorized output
cat filename Show contents of a file
cat -n filename Show contents of a file, with line numbers
Getting Help
When you're stuck and need help with a Linux command, help is usually only a few
keystrokes away! Help on most Linux commands is typically built right into the
commands themselves, available through online help programs (“man pages” and
“info pages”), and of course online.

Using a Command's Built-In Help


Many commands have simple “help” screens that can be invoked with special
command flags. These flags usually look like “-h” or “--help”.
Example: grep --help
Online Manuals: “Man Pages”
The best source of information for most commands can be found in the online manual
pages, known as “man pages” for short. To read a command's man page, type “man
command”.
Examples: man ls Get help on the “ls” command.
man man A manual about how to use the manual!
To search for a particular word within a man page, type “/word”. To quit from a man
page, just type the “Q” key.
Sometimes, you might not remember the name of Linux command and you need to
search for it. For example, if you want to know how to change a file's permissions, you
can search the man page descriptions for the word “permission” like this:
man -k permission
If you look at the output of this command, you will find a line that looks something like:
chmod (1) - change file access permissions
Now you know that “chmod” is the command you were looking for. Typing “man
chmod” will show you the chmodcommand's manual page!

Info Pages
Some programs, particularly those released by the Free Software Foundation, use info
pages as their main source of online documentation. Info pages are similar to man
page, but instead of being displayed on one long scrolling screen, they are presented
in shorter segments with links to other pieces of information. Info pages are accessed
with the “info” command, or on some Linux distributions, “pinfo” (a nicer info
browser).
For example: info df Loads the “df” info page.
Navigating the Linux File system
The Linux file system is a tree-like hierarchy hierarchy of directories and files. At the
base of the file system is the “/” directory, otherwise known as the “root” (not to be
confused with the root user). Unlike DOS or Windows filesystems that have multiple
“roots”, one for each disk drive, the Linux filesystem mounts all disks somewhere
underneath the / file system. The following table describes many of the most common
Linux directories.
The Linux Directory Layout
Directory Description
The nameless base of the filesystem. All other directories, files,
drives, and devices are attached to this root. Commonly (but
incorrectly) referred to as the “slash” or “/” directory. The “/” is just
a directory separator, not a directory itself.
/bin Essential command binaries (programs) are stored here (bash, ls,
mount, tar, etc.)
/boot Static files of the boot loader.
/dev Device files. In Linux, hardware devices are acceessd just like other
files, and they are kept under this directory.
/etc Host-specific system configuration files.
/home Location of users' personal home directories (e.g. /home/susan).
/lib Essential shared libraries and kernel modules.
/proc Process information pseudo-filesystem. An interface to kernel data
structures.
/root The root (superuser) home directory.
/sbin Essential system binaries (fdisk, fsck, init, etc).
/tmp Temporary files. All users have permission to place temporary files
here.
/usr The base directory for most shareable, read-only data (programs,
libraries, documentation, and much more).
/usr/bin Most user programs are kept here (cc, find, du, etc.).
/usr/include Header files for compiling C programs.
/usr/lib Libraries for most binary programs.
/usr/local “Locally” installed files. This directory only really matters in
environments where files are stored on the network. Locally-
installed files go in
/usr/local/bin, /usr/local/lib, etc.). Also often used for software
packages installed from source, or software not officially shipped
with the distribution.
/usr/sbin Non-vital system binaries (lpd, useradd, etc.)
/usr/share Architecture-independent data (icons, backgrounds, documentation,
terminfo, man pages, etc.).
/usr/src Program source code. E.g. The Linux Kernel, source RPMs, etc.
/usr/X11R6 The X Window System.
/var Variable data: mail and printer spools, log files, lock files, etc.
Commands for Navigating the Linux Filesystems
The first thing you usually want to do when learning about the Linux filesystem is
take some time to look around and see what's there! These next few commands will:
a) Tell you where you are,
b) take you somewhere else, and c) show you what's there. The following table
describes the basic operation of the pwd, cd, and ls commands, and compares them to
certain DOS commands that you might already be familiar with.

Linux Command DOS Command Descripti


on
pwd cd “Print Working Directory”. Shows the
current location in the directory tree.
cd cd, chdir “Change Directory”. When typed all by itself,
it returns you to your home directory.
cd directory cd directory Change into the specified directory
name. Example: cd /usr/src/linux
cd ~ “~” is an alias for your home directory. It can
be used as a shortcut to your “home”, or
other directories relative to your home.
cd .. cd.. Move up one directory. For example, if you
are in
/home/vic and you type “cd ..”, you will end
up in /home.
cd - Return to previous directory. An easy way to
get back to your previous location!
ls dir /w List all files in the current directory, in
column format.
ls directory dir directory List the files in the specified directory.
Example: ls /var/log
ls -l dir List files in “long” format, one file per line.
This also shows you additional info about the
file, such as ownership, permissions, date,
and size.
ls -a dir /a List all files, including “hidden” files. Hidden
files are those files that begin with a “.”, e.g.
The
.bash_historyfile in your home directory.
ls -ld A “long” list of “directory”, but instead of
directory showing the directory contents, show the
directory's detailed information. For
example, compare the output of the
following two commands:
ls -l /usr/bin ls -ld
/usr/bin
ls /usr/bin/d* dir d*.* List all files whose names begin with the
letter “d” in the /usr/bindirectory.
Piping and Re-Direction
Before we move on to learning even more commands, let's side-track to the topics of
piping and re-direction. The basic UNIX philosophy, therefore by extension the Linux
philosophy, is to have many small programs and utilities that do a particular job very
well. It is the responsibility of the programmer or user to combine these utilities to
make more useful command sequences.

Piping Commands Together


The pipe character, “|”, is used to chain two or more commands together. The output
of the first command is “piped” into the next program, and if there is a second pipe,
the output is sent to the third program, etc. For example:
ls -la /usr/bin | less
In this example, we run the command “ls -la /usr/bin”, which gives us a long listing of
all of the files in /usr/bin. Because the output of this command is typically very long,
we pipe the output to a program called “less”, which displays the output for us one
screen at a time.

Redirecting Program Output to Files


There are times when it is useful to save the output of a command to a file, instead of
displaying it to the screen. For example, if we want to create a file that lists all of the
MP3 files in a directory, we can do something like this, using the “>” redirection
character:
ls -l /home/vic/MP3/*.mp3 > mp3files.txt
A similar command can be written so that instead of creating a new file called
mp3files.txt, we can append to the end of the original file:
ls -l /home/vic/extraMP3s/*.mp3 >> mp3files.txt

Other Linux Commands


The following sections describe many other commands that you will find on most
Linux systems. I can't possibly cover the details of all of these commands in this
document, so don't forget that you can check the “man pages” for additional
information. Not all of the listed commands will be available on all Linux or UNIX
distributions.

Working With Files and Directories


These commands can be used to: find out information about files, display files, and
manipulate them in other ways (copy, move, delete).
Linux DOS Description
Command Command
file Find out what kind of file it is.
For example, “file /bin/ls” tells us that it is a Linux
executable file.
cat type Display the contents of a text file on the screen. For
example: cat mp3files.txt would display the file we
created in the previous section.
head Display the first few lines of a text file.
Example: head /etc/services
tail Display the last few lines of a text file.
Example: tail /etc/services
tail -f Display the last few lines of a text file, and then
output appended data as the file grows (very useful
for following log files!).
Example: tail -f /var/log/messages
cp copy Copies a file from one location to
another. Example: cp mp3files.txt
/tmp
(copies the mp3files.txt file to the /tmp directory)
mv rename, Moves a file to a new location, or renames it.
ren, move For example: mv mp3files.txt /tmp
(copy the file to /tmp, and delete it from the original
location)
rm del Delete a file. Example: rm /tmp/mp3files.txt
mkdir md Make Directory. Example: mkdir /tmp/myfiles/
rmdir rd, rmdir Remove Directory. Example: rmdir /tmp/myfiles/

Finding Things
The following commands are used to find files. “ls” is good for finding files if you
already know approximately where they are, but sometimes you need more powerful
tools such as these:

Linux Description
Command
which Shows the full path of shell commands found in your path. For
example, if you want to know exactly where the “grep” command is
located on the filesystem, you can type “which grep”. The output
should be something like: /bin/grep
whereis Locates the program, source code, and manual page for a command
(if all information is available). For example, to find out where “ls”
and its man page are, type: “whereis ls” The output will look
something like:
ls: /bin/ls /usr/share/man/man1/ls.1.gz

locate A quick way to search for files anywhere on the filesystem. For
example, you can find all files and directories that contain the name
“mozilla” by typing: locate mozilla
find A very powerful command, but sometimes tricky to use. It can be
used to search for files matching certain patterns, as well as many
other types of searches. A simple example is:
find . -name \*mp3
This example starts searching in the current directory “.” and all
sub- directories, looking for files with “mp3” at the end of their
names.

Informational Commands
The following commands are used to find out some information about the user or the
system.

Linux Command Explanation


ps Lists currently running process (programs).
w Show who is logged on and what they are doing.
id Print your user-id and group id's
df Report filesystem disk space usage (“Disk Free” is how I
remember it)
du Disk Usage in a particular directory. “du -s” provides a
summary for the current directory.
top Displays CPU processes in a full-screen GUI. A great way to see
the activity on your computer in real-time. Type “Q” to quit.
free Displays amount of free and used memory in the system.
cat /proc/cpuinfo Displays information about your CPU.
cat /proc/meminfo Display lots of information about current memory usage.
uname -a Prints system information to the screen (kernel version,
machine type, etc.)
Other Utilities
Here are some other commands that are useful to know.

Linux Description
Command
clear Clear the screen
echo Display text on the screen. Mostly useful when writing shell scripts.
For example: echo “Hello World”
more Display a file, or program output one page at a time. Examples:
more mp3files.txt ls -
la | more
less An improved replacement for the “more” command. Allows you to
scroll backwards as well as forwards.
grep Search for a pattern in a file or program output. For example, to
find out which TCP network port is used by the “nfs” service, you
can do this:
grep “nfs” /etc/services
This looks for any line that contains the string “nfs” in the file
“/etc/services” and displays only those lines.
lpr Print a file or program output. Examples:
lpr mp3files.txt - Print the mp3files.txt file
ls -la | lpr - Print the output of the “ls -la” command.
sort Sort a file or program output. Example: sort mp3files.txt
su “Switch User”. Allows you to switch to another user's account
temporarily. The default account to switch to is the root/superuser
account. Examples:
su - Switch the root account
su - - Switch to root, and log in with root's environment
su larry- Switch to Larry's account
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________

Signature of Practical In-Charge


Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : ________________________ Date :___________________________________

Aim: Shell programming for file handling.

Theory:

Listing Files

To list the files and directories stored in the current directory, use the following
command −
$ls
Here is the sample output of the above command −
$ls

bin hosts lib res.03


ch07 hw1 pub test_results
ch07.bak hw2 res.01 users
docs hw3 res.02 work
The command ls supports the -l option which would help you to get more
information about the listed files −
$ls -l
total 1962188

drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 uml


-rw-rw-r-- 1 amrood amrood 5341 Dec 25 08:38 uml.jpg
drwxr-xr-x 2 amrood amrood 4096 Feb 15 2006 univ
drwxr-xr-x 2 root root 4096 Dec 9 2007 urlspedia
-rw-r--r-- 1 root root 276480 Dec 9 2007 urlspedia.tar
drwxr-xr-x 8 root root 4096 Nov 25 2007 usr
drwxr-xr-x 2 200 300 4096 Nov 25 2007 webthumb-1.01
-rwxr-xr-x 1 root root 3192 Nov 25 2007 webthumb.php
-rw-rw-r-- 1 amrood amrood 20480 Nov 25 2007 webthumb.tar
-rw-rw-r-- 1 amrood amrood 5654 Aug 9 2007 yourfile.mid
-rw-rw-r-- 1 amrood amrood 166255 Aug 9 2007 yourfile.swf
drwxr-xr-x 11 amrood amrood 4096 May 29 2007 zlib-1.2.3
$
Here is the information about all the listed columns −
 First Column − Represents the Zile type and the permission given on the
file. Below is the description of all type of files.
 Second Column − Represents the number of memory blocks taken by the
file or directory.
 Third Column − Represents the owner of the Zile. This is the Unix user who
created this file.
 Fourth Column − Represents the group of the owner. Every Unix user will
have an associated group.
 Fifth Column − Represents the Zile size in bytes.
 Sixth Column − Represents the date and the time when this Zile was created
or modified for the last time.
 Seventh Column − Represents the Zile or the directory name.
In the ls -l listing example, every file line begins with a d, -, or l. These characters
indicate the type of the file that's listed.
Sr.No. Prefix & Description

1 -
Regular file, such as an ASCII text file, binary executable, or hard link.

2 b
Block special file. Block input/output device file such as a physical
hard drive.

3 c
Character special file. Raw input/output device file such as a physical
hard drive.

4 d
Directory file that contains a listing of other files and directories.

5 l
Symbolic link file. Links on any regular file.

6 p
Named pipe. A mechanism for interprocess communications.

7 s
Socket used for interprocess communication.
Metacharacters
Metacharacters have a special meaning in Unix. For example, * and ? are
metacharacters. We use * to match 0 or more characters, a question mark (?)
matches with a single character.
For Example −
$ls ch*.doc
Displays all the files, the names of which start with ch and end with .doc −
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc ch06-2.doc
ch01-2.doc ch02-1.doc c
Here, * works as meta character which matches with any character. If you want to
display all the files ending with just .doc, then you can use the following command

$ls *.doc

Hidden Files
An invisible file is one, the first character of which is the dot or the period
character (.). Unix programs (including the shell) use most of these files to store
configuration information.
Some common examples of the hidden files include the files −
 .profile − The Bourne shell ( sh) initialization script
 .kshrc − The Korn shell ( ksh) initialization script
 .cshrc − The C shell ( csh) initialization script
 .rhosts − The remote shell conZiguration Zile
To list the invisible files, specify the -a option to ls −
$ ls -a

. .profile docs lib test_results


.. .rhosts hosts pub users
.emacs bin hw1 res.01 work
.exrc ch07 hw2 res.02
.kshrc ch07.bak hw3 res.03
$
 Single dot (.) − This represents the current directory.
 Double dot (..) − This represents the parent directory.

Creating Files
You can use the vi editor to create ordinary files on any Unix system. You simply
need to give the following command −
$ vi filename
The above command will open a file with the given filename. Now, press the
keyi to come into the edit mode. Once you are in the edit mode, you can start
writing your content in the file as in the following program −
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
Once you are done with the program, follow these steps −
 Press the key esc to come out of the edit mode.
 Press two keys Shift + ZZ together to come out of the file completely.
You will now have a file created with filename in the current directory.
$ vi filename
$

Editing Files
You can edit an existing file using the vi editor. We will discuss in short how to
open an existing file −
$ vi filename
Once the file is opened, you can come in the edit mode by pressing the key iand
then you can proceed by editing the file. If you want to move here and there inside
a file, then first you need to come out of the edit mode by pressing the key Esc.
After this, you can use the following keys to move inside a file −
 l key to move to the right side.
 h key to move to the left side.
 k key to move upside in the file.
 j key to move downside in the file.
So using the above keys, you can position your cursor wherever you want to edit.
Once you are positioned, then you can use the i key to come in the edit mode. Once
you are done with the editing in your file, press Esc and finally two keys Shift +
ZZ together to come out of the file completely.

Display Content of a File


You can use the cat command to see the content of a file. Following is a simple
example to see the content of the above created file −
$ cat filename
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
$
You can display the line numbers by using the -b option along with
the catcommand as follows −
$ cat -b filename
1 This is unix file....I created it for the first time.....
2 I'm going to save this content in this file.
$

Counting Words in a File


You can use the wc command to get a count of the total number of lines, words,
and characters contained in a file. Following is a simple example to see the
information about the file created above −
$ wc filename
2 19 103 filename
$
Here is the detail of all the four columns −
 First Column − Represents the total number of lines in the Zile.
 Second Column − Represents the total number of words in the Zile.
 Third Column − Represents the total number of bytes in the file. This is the
actual size of the file.
 Fourth Column − Represents the Zile name.
You can give multiple files and get information about those files at a time.
Following is simple syntax −
$ wc filename1 filename2 filename3

Copying Files
To make a copy of a file use the cp command. The basic syntax of the command is

$ cp source_file destination_file
Following is the example to create a copy of the existing file filename.
$ cp filename copyfile
$
You will now find one more file copyfile in your current directory. This file will
exactly be the same as the original file filename.

Renaming Files
To change the name of a file, use the mv command. Following is the basic syntax −
$ mv old_file new_file
The following program will rename the existing file filename to newfile.
$ mv filename newfile
$
The mv command will move the existing file completely into the new file. In this
case, you will find only newfile in your current directory.

Deleting Files
To delete an existing file, use the rm command. Following is the basic syntax −
$ rm filename
Caution − A Zile may contain useful information. It is always recommended to be
careful while using this Delete command. It is better to use the -i option along
with rm command.
Following is the example which shows how to completely remove the existing
file filename.
$ rm filename
$
You can remove multiple files at a time with the command given below −
$ rm filename1 filename2 filename3
$

Standard Unix Streams


Under normal circumstances, every Unix program has three streams (files)
opened for it when it starts up −
 stdin − This is referred to as the standard input and the associated file
descriptor is 0. This is also represented as STDIN. The Unix program will
read the default input from STDIN.
 stdout − This is referred to as the standard output and the associated file
descriptor is 1. This is also represented as STDOUT. The Unix program will
write the default output at STDOUT
 stderr − This is referred to as the standard error and the associated file
descriptor is 2. This is also represented as STDERR. The Unix program will
write all the error messages at STDERR.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________

Signature of Practical In-Charge


Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : ________________________ Date :___________________________________

Aim: Shell Script programming using the commands grep, awk, and sed

Theory:
The grep Command
The grep command searches a file or files for lines that have a certain pattern. The syntax
is −
$grep pattern file(s)
The name "grep" comes from the ed (a Unix line editor) command g/re/pwhich means
“globally search for a regular expression and print all lines containing it”.
A regular expression is either some plain text (a word, for example) and/or special
characters used for pattern matching.
The simplest use of grep is to look for a pattern consisting of a single word. It can be used
in a pipe so that only those lines of the input files containing a given string are sent to the
standard output. If you don't give grep a filename to read, it reads its standard input;
that's the way all filter programs work −
$ls -l | grep "Aug"
-rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02
-rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07
-rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$
There are various options which you can use along with the grep command −
Sr.No. Option & Description

1 -v
Prints all lines that do not match pattern.

2 -n
Prints the matched line and its line number.
3 -l
Prints only the names of files with matching lines (letter "l")

4 -c
Prints only the count of matching lines.

5 -i
Matches either upper or lowercase.
Let us now use a regular expression that tells grep to find lines with "carol", followed by
zero or other characters abbreviated in a regular expression as ".*"), then followed by
"Aug".−
Here, we are using the -i option to have case insensitive search −
$ls -l | grep -i "carol.*aug"
-rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros
$

Sed Command
SED command in UNIX is stands for stream editor and it can perform lot’s 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 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.
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
Consider the below text file as an input.
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Sample Commands
Replacing or substituting string : Sed command is mostly used to replace the text in a
file. The below simple sed command replaces the word “unix” with “linux” in the file.
$sed 's/unix/linux/' geekfile.txt
Output :

linux is great os. unix is opensource. unix is free os.


learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix
.unix is a powerful.
Here the “s” specifies the substitution operation. The “/” are delimiters.
The “unix” is the search pattern and the “linux” is the replacement
string.
By default, the sed command replaces the first occurrence of the pattern
in each line and it won’t replace the second, third…occurrence in the
line.
Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags
to replace the first, second occurrence of a pattern in a line. The below command
replaces the second occurrence of the word “unix” with “linux” in a line.
$sed 's/unix/linux/2' geekfile.txt
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.
Replacing all the occurrence of the pattern in a line : The substitute flag
/g (global replacement) specifies the sed command to replace all the
occurrences of the string in the line.
$sed 's/unix/linux/g' geekfile.txt
Output :
linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.
Replacing from nth occurrence to all occurrences in a line : Use the
combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a
pattern in a line. The following sed command replaces the third, fourth, fifth… “unix” word
with “linux” word in a line.
$sed 's/unix/linux/3g' geekfile.txt
Output:
unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.
Parenthesize first character of each word : This sed example prints the
first character of every word in paranthesis.
$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
Output:
(W)elcome (T)o (T)he (G)eek (S)tuff
Replacing string on a specific line number : You can restrict the sed
command to replace the string on a specific line number. An example is
$sed '3 s/unix/linux/' geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux
Printing only the replaced lines : Use the -n option along with the /p print flag to
display only the replaced lines. Here the -n option suppresses the duplicate rows
generated by the /p flag and prints the replaced lines only one time.
$sed -n 's/unix/linux/p' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Duplicating the replaced line with /p flag : The /p print flag prints the
replaced line twice on the terminal. If a line does not have the search
pattern and is not replaced, then the /p prints that line only once.
$sed 's/unix/linux/p' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
If you use -n alone without /p, then the sed does not print anything.
Replacing string on a range of lines : You can specify a range of line
numbers to the sed command for replacing a string.
$sed '1,3 s/unix/linux/' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful.
Here the sed command replaces the lines with range from 1 to 3.
Another example is
$sed '2,$ s/unix/linux/' geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful
Here $ indicates the last line in the file. So the sed command
replaces the text from second line to last line in the file.
Deleting lines from a particular file : SED command can also be used for
deleting lines from a particular file. SED command is used for
performing deletion operation without even opening the file
Examples:
1. To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt
Example:
$ sed '5d' filename.txt
2. To Delete a last line
Syntax:
$ sed '$d' filename.txt
3. To Delete line from range x to y
Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt
4. To Delete from nth to last line
Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt
5. To Delete pattern matching line
Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt

Awk Command:
1. AWK Operations:
(a) Scans a file line by line
(b) Splits each input line into fields
(c) Compares input line/fields to pattern
(d) Performs action(s) on matched lines

2. Useful For:
(a) Transform data files
(b) Produce formatted reports

3. Programming Constructs:
(a) Format output lines
(b) Arithmetic and string operations
(c) Conditionals and loops

Syntax:
awk options 'selection _criteria {action }' input-file >
output-file
Options:
-f program-file : Reads the AWK program source from the
file
program-file, instead of from the
first command line argument.
-F fs : Use fs for the input field separator
Consider the following text file as the input file for all cases below.

$cat > employee.txt


ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000

1. Default behavior of Awk : By default Awk prints every line of data


from the specified file.

$ awk '{print}' employee.txt


Output:

ajay manager account 45000


sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
In the above example, no pattern is given. So the actions are applicable to all
the lines. Action print without any argument prints the whole line by default,
so it prints all the lines of the file without failure.
2. Print the lines which matches with the given pattern.

$ awk '/manager/ {print}' employee.txt


Output:

ajay manager account 45000


varun manager sales 50000
amit manager account 47000
In the above example, the awk command prints all the line which matches
with the ‘manager’.

3. Spliting a Line Into Fields : For each record i.e line, the awk command
splits the record delimited by whitespace character by default and stores it
in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and
$4 respectively. Also, $0 represents the whole line.

$ awk '{print $1,$4}' employee.txt


Output:

ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000
In the above example, $1 and $4 represents Name and Salary fields
respectively.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge
Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 4_____________________ Date :___________________________________

Aim: Implementation of various CPU scheduling algorithms (FCFS).

Theory:
Basic Terms must related to CPU scheduling
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from
time of submission of the process to the time of completion of the process(Wall
clock time).

Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process
has been waiting in the ready queue to acquire get control on the CPU.
In simple terms, the duration for which a process gets control of the CPU is the

CPU burst time and the concept of gaining control of the CPU is the CPU burst.

FCFS CPU SCHEDULING ALGORITHM: For FCFS scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. The scheduling is
performed on the basis of arrival time of the processes irrespective of their other
parameters. Each process will be executed according to its arrival time. Calculate
the waiting time and turnaround time of each of the processes accordingly.
Disadvantages: This scheduling method is nonpreemptive, that is, the process will
run until it finishes. Because of this nonpreemptive scheduling, short processes
which are at the back of the queue have to wait for the long process at the front to
finish.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge

Shri Sant Gadge Baba College of Engineering & Technology,


Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 5_____________________ Date :___________________________________

Aim: Implementation of various CPU scheduling algorithms (SJF).

Theory:
Basic Terms must related to CPU scheduling
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from
time of submission of the process to the time of completion of the process(Wall
clock time).

Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process
has been waiting in the ready queue to acquire get control on the CPU.
In simple terms, the duration for which a process gets control of the CPU is the

CPU burst time and the concept of gaining control of the CPU is the CPU burst.

SJF CPU SCHEDULING ALGORITHM: For SJF scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. Arrange all the jobs
in order with respect to their burst times. There may be two jobs in queue with the
same execution time, and then FCFS approach is to be performed. Each process will
be executed according to the length of its burst time. Then calculate the waiting
time and turnaround time of each of the processes accordingly.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge

Shri Sant Gadge Baba College of Engineering & Technology,


Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 6_____________________ Date :___________________________________

Aim: Implementation of various CPU scheduling algorithms (Priority


Scheduling).

Theory:
Basic Terms must related to CPU scheduling
Turnaround Time
It is the amount of time taken to execute a particular process, i.e. The interval from
time of submission of the process to the time of completion of the process(Wall
clock time).

Waiting Time
The sum of the periods spent waiting in the ready queue amount of time a process
has been waiting in the ready queue to acquire get control on the CPU.
In simple terms, the duration for which a process gets control of the CPU is the

CPU burst time and the concept of gaining control of the CPU is the CPU burst.

PRIORITY CPU SCHEDULING ALGORITHM: For priority scheduling algorithm,


read the number of processes/jobs in the system, their CPU burst times, and the
priorities. Arrange all the jobs in order with respect to their priorities. There may
be two jobs in queue with the same priority, and then FCFS approach is to be
performed. Each process will be executed according to its priority. Calculate the
waiting time and turnaround time of each of the processes accordingly.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge

Shri Sant Gadge Baba College of Engineering & Technology,


Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 7_____________________ Date :___________________________________

Aim: Write a C program to simulate page replacement algorithms (Optimal)

Theory:

Optimal page replacement algorithm has the lowest page-fault rate of all algorithms and
will never suffer from Belady's anomaly. The basic idea is to replace the page that will not
be used for the longest period of time. Use of this page-replacement algorithm guarantees
the lowest possible page fault rate for a fixed number of frames. Unfortunately, the
optimal page-replacement algorithm is difficult to implement, because it requires future
knowledge of the reference string.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________

Signature of Practical In-Charge


Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 8____________________ Date :___________________________________

Aim: Concurrent programming; use of process system calls (fork and v-fork).

Theory:

Differentiate between fork and vfork

fork Function

An existing process can create a new one by calling the fork function.

#include <unistd.h>

pid_t fork(void);

Returns: 0 in child, process ID of child in parent, –1 on error

The new process created by fork is called the child process. This function is called
once but returns twice. The only difference in the returns is that the return value in
the child is 0, whereas the return value in the parent is the process ID of the new
child. The reason the child's process ID is returned to the parent is that a process
can have more than one child, and there is no function that allows a process to
obtain the process IDs of its children. The reason fork returns 0 to the child is that
a process can have only a single parent, and the child can always call getppid to
obtain the process ID of its parent. (Process ID 0 is reserved for use by the kernel,
so it's not possible for 0 to be the process ID of a child.)

Both the child and the parent continue executing with the instruction that follows
the call to fork. The child is a copy of the parent. For example, the child gets a copy
of the parent's data space, heap, and stack. Note that this is a copy for the child; the
parent and the child do not share these portions of memory.

vfork Function
The function vfork has the same calling sequence and same return values as fork.
But the semantics of the two functions differ. The vfork function originated with
2.9BSD. Some consider the function a blemish, but all the platforms covered in this
book support it. In fact, the BSD developers removed it from the 4.4BSD release,
but all the open source BSD distributions that derive from 4.4BSD added support
for it back into their own releases. The vfork function is marked as an obsolete
interface in Version 3 of the Single UNIX Specification. The vfork function is
intended to create a new process when the purpose of the new process is to exec a
new program (step 2 at the end of the previous section). The bare-bones shell in
the program from Figure 1.7 is also an example of this type of program. The vfork
function creates the new process, just like fork, without copying the address space
of the parent into the child, as the child won't reference that address space; the
child simply calls exec (or exit) right after the vfork. Instead, while the child is
running and until it calls either exec or exit, the child runs in the address space of
the parent. This optimization provides an efficiency gain on some paged virtual-
memory implementations of the UNIX System. (As we mentioned in the previous
section, implementations use copy-on-write to improve the efficiency of a fork
followed by an exec, but no copying is still faster than some copying.) Another
difference between the two functions is that vfork guarantees that the child runs
first, until the child calls exec or exit. When the child calls either of these functions,
the parent resumes. (This can lead to deadlock if the child depends on further
actions of the parent before calling either of these two functions.)
Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_________________________________________________________Signature of Practical In-Charge
Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : 9_____________________ Date :___________________________________

Aim: Program which shows the performance improvement in using threads


as compared with process.(Examples like Matrix Multiplication)

Theory: A typical UNIX process can be thought of as having a single thread of control:
each process is doing only one thing at a time. With multiple threads of control, we can
design our programs to do more than one thing at a time within a single process, with
each thread handling a separate task. This approach can have several benefits.

 We can simplify code that deals with asynchronous events by assigning a separate
thread to handle each event type. Each thread can then handle its event using a
synchronous programming model. A synchronous programming model is much
simpler than an asynchronous one.
 Multiple processes have to use complex mechanisms provided by the operating
system to share memory and file descriptors. Threads, on the other hand,
automatically have access to the same memory address space and file descriptors.
 Some problems can be partitioned so that overall program throughput can be
improved. A single process that has multiple tasks to perform implicitly serializes
those tasks, because there is only one thread of control. With multiple threads of
control, the processing of independent tasks can be interleaved by assigning a
separate thread per task. Two tasks can be interleaved only if they don't depend on
the processing performed by each other.
 Similarly, interactive programs can realize improved response time by using
multiple threads to separate the portions of the program that deal with user input
and output from the other parts of the program.

Thread related functions:

Thread Equality:
#include <pthread.h>
int pthread_equal(pthread_t tid1, pthread_t tid2);
Returns: nonzero if equal, 0 otherwise

Thread self
#include <pthread.h>
pthread_t pthread_self(void);
Returns: the thread ID of the calling thread
This function can be used with pthread_equal when a thread needs to identify data
structures that are tagged with its thread ID. For example, a master thread might place
work assignments on a queue and use the thread ID to control which jobs go to each
worker thread. A single master thread places new jobs on a work queue. A pool of three
worker threads removes jobs from the queue. Instead of allowing each thread to process
whichever job is at the head of the queue, the master thread controls job assignment by
placing the ID of the thread that should process the job in each job structure. Each worker
thread then removes only jobs that are tagged with its own thread ID.

The traditional UNIX process model supports only one thread of control per process.
Conceptually, this is the same as a threads-based model whereby each process is made up
of only one thread. With pthreads, when a program runs, it also starts out as a single
process with a single thread of control. As the program runs, its behavior should be
indistinguishable from the traditional process, until it creates more threads of control.
Additional threads can be created by calling the pthread_create function.

#include <pthread.h>

int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void


*(*start_rtn)(void), void *restrict arg);

Returns: 0 if OK, error number on failure

The memory location pointed to by tidp is set to the thread ID of the newly created thread
when pthread_create returns successfully. The attr argument is used to customize various
thread attributes. We'll set this to NULL to create a thread with the default attributes.
The newly created thread starts running at the address of the start_rtn function. This
function takes a single argument, arg, which is a typeless pointer. If you need to pass more
than one argument to the start_rtn function, then you need to store them in a structure
and pass the address of the structure in arg. When a thread is created, there is no
guarantee which runs first: the newly created thread or the calling thread. The newly
created thread has access to the process address space and inherits the calling thread's
floating-point environment and signal mask; however, the set of pending signals for the
thread is cleared.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________

Signature of Practical In-Charge


Shri Sant Gadge Baba College of Engineering & Technology,
Bhusawal
Department of Computer Science & Engineering
Session :________________________ Semester :___________________________________
Name :________________________ Roll No :___________________________________
Class: : ________________________ Batch :___________________________________
Exp No : ________________________ Date :___________________________________

Aim: Signal Handling for various cases

Theory:
signal Function
Signals are a way for a process to be notified of asynchronous events.
Some examples:
 a timer you set has gone off (SIGALRM)
 SIGTSTP stop signal.
 SIGINTR interrupt signal.
 some I/O you requested has occurred (SIGIO)
 a user resized the terminal ”window” (SIGWINCH)
 a user disconnected from the system (SIGHUP)

The simplest interface to the signal features of the UNIX System is the signal function.

#include <signal.h>
void (*signal(int signo, void (*func)(int)))(int);
Here:
 signo is the name of the signal.
 (*func): SIG_IGN which requests that we ignore the signal signo OR SIG_DFL which
requests that we accept the default action for signal signo OR the address of a
function which should catch or handle a signal.

The signo argument is just the name of the signal. The value of func is (a) the constant
SIG_IGN, (b) the constant SIG_DFL, or (c) the address of a function to be called when the
signal occurs. If we specify SIG_IGN, we are telling the system to ignore the signal.
(Remember that we cannot ignore the two signals SIGKILL and SIGSTOP.) When we
specify SIG_DFL, we are setting the action associated with the signal to its default value.
When we specify the address of a function to be called when the signal occurs, we are
arranging to "catch" the signal. We call the function either the signal handler or the signal-
catching function.
The prototype for the signal function states that the function requires two arguments and
returns a pointer to a function that returns nothing (void). The signal function's first
argument, signo, is an integer. The second argument is a pointer to a function that takes a
single integer argument and returns nothing. The function whose address is returned as
the value of signal takes a single integer argument (the final (int)). In plain English, this
declaration says that the signal handler is passed a single integer argument (the signal
number) and that it returns nothing. When we call signal to establish the signal handler,
the second argument is a pointer to the function. The return value from signal is the
pointer to the previous signal handler.
The signal function is defined by ISO C, which doesn't involve multiple processes, process
groups, terminal I/O, and the like. Therefore, its definition of signals is vague enough to be
almost useless for UNIX systems.

Conclusion:______________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
_______________________________________________________________________________________________________
______________________

Signature of Practical In-Charge

You might also like