You are on page 1of 96

Introduction to problem

solving methods and


algorithm development
Dr. Subodh Srivastava
Visiting faculty, CSE,IIT (BHU)

Problem Definition
Problem are obstacles or undesirable situations, that
prevent one from achieving an objective in a give situation,
and therefore, must be solved in order that the objective is
achieved .
A computer is a tool that can be used to
implement a plan for solving a problem.
Computer programs
Sets of instructions for which computer processes data.
Instruction : A command in binary that is recognized and
executed by the computer in order to accomplish the task.

An algorithm is a plan for solving a


problem. A process or set of rules to
be followed in calculations.
A person must design an algorithm.
A person must translate an algorithm
into a computer program.

The steps involved in


solving any problem may
be generalized

The steps

Become Aware Of The Problem

Gather Information to increase the


understanding of problem situation

Define the problem in terms of objective


,variables and their interaction

Develop Evaluation
Criteria

Develop
Alternation
Solution

Choose the most appropriate


solution

Implement solution

Monitor and control the


implementation

Problem-Solving Approaches
Top- Down Design Approach Divide and
rule
Bottom-UP Design Approach
Top- Down Design Approach Divide
and rule :The problem is broken down
into smaller fragments and fragments,
further into still smaller fragments till the
fragments are small enough to be solved
easily are separately.

Problem :How to reach


Varanasi from Delhi by train.
Let us apply top down design
approach .Break this problem into
smaller problem
follows:
How to reachas
Varanasi
from
Delhi by train
How to
reach
Delhi
Railway
Station

How to
buy tickets

How to Get
the right
platform

How to find the


right plate
form No.

How to
Board the
train

How to use
escalator to go
to a platform

How to alight
from the
train

Top-Down Design

Bottom UP Design
The bottom-up approach is opposite to
that of the top-down. Here, the problem
solver has to solve the most basic
(smallest ) units in the hierarchy first
and then use to build more
sophisticated bigger units.
The pure bottom up approach is
generally not recommended because it
is difficult to anticipate which low level
units will be needed for solution of
particular problem.

A General Example
Subdividing the party planning

Subdividing the party planning

Problem
Create a list that includes each
persons name, telephone number,
and e-mail address
This list should then be printed in
alphabetical order.
The names to be included in the list are
on scraps of paper and business cards.

Main

Level 0

Enter names and numbers into list


Put list into alphabetical order
Print list
Enter names and numbers into list
While ( more names)
Enter name
Enter telephone number
Enter email address
Insert information into list

Level 1

Enter names and numbers into list (revised)


1

Set moreNames to true


While (moreNames)
Prompt for and enter name
Prompt for and enter telephone number
Prompt for and enter email address
Insert information into list
Write "Enter a 1 to continue or a 0 to stop."
Read response
If (response = 0)
Set moreNames to false

Level

Prompt for and enter name

Level 2

Write "Enter last name; press return."


Read lastName
Write "Enter first name; press return."
Read firstName
Prompt for and enter telephone number

Level 2

Write "Enter area code and 7-digit number; press return."


Read telephoneNumber
Prompt for and enter email address
Write "Enter email address; press return."
Read emailAddress

Level 2

A Computer Example

Algorithm
An algorithm is a plan for solving a
problem. A process or set of rules to
be followed in calculations.
An algorithm is a finite list of welldefined steps for solving a particular
problem. These instructions, when
executed in the specified order , solve
a problem , which the algorithm
intends to solve.

Writing an algorithm(Elements
of Program Style )
An algorithm has three execution
sequences:
Sequence Logic or sequential Logic
Selection or Conditional Logic
Iteration or Repetitive Logic
In Sequence Logic execution the
instructions are executed in obvious Linear
sequence one by one.
The sequence may be presented explicitly
,by means of numbered steps, or implicitly,
by the order in which the instruction are
written.

Selection Logic employs a number


of conditions, which lead to a selection
of one out of several alternative
instructions.
Iteration Logic allows execution of
one or more instruction repetitively , a
specified number of times or until some
condition become true.
Any language may be employed to
write an algorithm as long as the
executor of the algorithm
understands it.

An algorithm begins with START


instruction.
It ends with a STOP instruction.
One instruction is written on one line.
Each line is numbered sequentially
for identification

An algorithm begins with a START


instruction.
It ends with STOP instruction.
One instruction is written on one line.
Each line is numbered sequentially for
identification.
The instructions used are well understood
by the problem- solver who will use it.
A container of a value (Known as variable)
is represented by any word that does not
have language specific meaning ,e.g. A,B,
and C etc.

A
6 means: store 6 in the
variable named A.
A
A+1 means : add 1 to the value
contained in the variable A and store the
result back into it.
Similarly ,A
B*C would mean multiply
values in B and C ,store the result into A.
Note.
Assignment operator
means store the
right hand value into left hand variable.

1.

Example 1
To print all even numbers from 2 to 12 on the screen. Assume
that Print instruction prints the given value on Screen . Use
sequential logic.
START

Begin the
algorithm
execution

2.

PRINT 2

PRINT value 2

3.

PRINT 4

PRINT value 4

4.

PRINT 6

PRINT value 6

5.

PRINT 8

PRINT value 8

6.

PRINT 10

PRINT value 10

7.

PRINT 12

PRINT value 12

STOP

Terminate the
algorithm

Another

print the value stored


A, otherwise move
algorithm for the samein
problem
using
to next line without
selection and iterationdoing
logicanything.

4.

A+1

Add 1 to value A
contains and store
the result back into A.

5.

IF (A<=12) THEN
GOTO 3

If the value contained


in A is smaller to 12
then do not go to
next line, go to line
number 3 instead,
otherwise move to
next line. In other
words, repeat steps
(3,4,5) as long as
value contained in A
is less than or equal
to 16.

6.

STOP

Terminate the
algorithm

The algorithm may also be written using


while-do-looping logic
1.

START

Begin the algorithm


execution

2.

3.

WHILE(A < = 16) DO


(STEPS 4 to 5 )

Keep on executing
steps as long as
condition.
(A<=12) is true ,i.e.
as long as value
stored in A is less
than or equal to 12,
otherwise go to step
next to the one
specified in the loop
condition,i.e.No.6.

4.

PRINT A

Print the value stored


in A.

5.

Add 2 to value A
contains and store the

A+2

Store 2 in the variable


2.

Characteristics of Algorithm

An algorithm must have the following


characteristics:
Input: Zero or more values externally supplied to
the algorithm.
Output: Zero or more values produced by the
algorithm.
Definiteness: Each instruction must be clear and
unambiguous, i.e having one and only meaning.
Finiteness : For all cases, the algorithm must
terminate after a finite number of steps.

Effectiveness: Every instruction must be


sufficiently basic and also feasible as far
as execution is concerned .

Implementation of
Algorithm

Algorithm implementation involves the


following different phases of activities :
Problem definition: Understanding the
information given and what result is asked
for.
Problem Analysis: To analyze the problem
to understand and formulate a possible line
or attack in order to solve it.
Algorithm Design : Applying top-down or
bottom up approach and describing the
algorithm.

Implementation of
AlgorithmAlgorithm
Analysis: To evaluate the designed

algorithm and if necessary design alternative


algorithm.
Execution: Algorithms, thus developed, may
be employed by a person or a machine to
solve the problem.
In order that a computer may execute the
algorithm, the algorithm must be translated
into a language that computers understand.
An algorithm may be implemented either in
hardware or in the software embedded into
the hardware, when it is know as
firmware to distinguish it from
software.

Instead of using English like sentences, a flowchart


uses a set of symbols to represent the solution.

Example

Example 2

Software Concepts

Software is a general term which is


used to describe the instructions
that are given to computer.
These instructions can be either a single
programme or a group of programmes.
Types of Software (Generally classified
into three specific categories in the
computer world:
1. System software
2.Application software
3.Utility software

System software : This consists of all the


programmes, languages and documentation
supplied by the manufacturer of the computer.
This type of software is required to use the
computer efficiently and conveniently.
These programme allow the application developer
to write and develop their own programmes.
E.g; Operating System(Windows 98,Windows
95, Windows 07,etc.), Drivers (Sound card driver,
Display Driver),Viruses etc
Application Software :These programmes are
developed by the user in order to perform some
specific function for the organization .e.g., a
payroll system to compute the salaries of the
employees of an organization is termed as an an
application software.

Utility software: Utility software may be


considered as an application software or a
system software which is very often used
in the development of the programme.
Examples of Utility softwares : Winzip ,
Frontpage, Notepad, Web Browsers etc.

Operating System
An operating system is a program that acts as an
interface between the user and the computer
hardware and controls the execution of all kinds of
programs.
OS is the interface between hardware and user
All computers need an OS
OSes are used to run applications and control
hardware.
An operating system is the most essential system
software that manages the operation of a computer.
Without an operating system, it is not possible to use
the computer.
Popular OSes include Windows, Unix , Linux, Mac OSX

Booting(To get ready ): To make the computer


ready , we provide operating system by a process
called booting.
Or The process of loading operating system from
disk to RAM is called booting.
When we switch on the computer, the instruction
stored in ROM
automatically
executed.
Switchare
on the
computer
system
Automatic Execution of
Program

DISK

RAM

An operating system is the most essential system software that


utilises the computer system resources effectively and acts an
interface between the user and hardware.

OS as a Resource Manager for four major


resources

S.N.

Resources

Examples

Examples S/W
to According
Task.

1.

Memory

Core(Main)

Memory
Management
,paging (divide
(a piece of
software or
data) into
sections,
keeping the
most frequently
accessed in
main memory
and storing the
rest in virtual
memory)

2.

Processor

CPU,I/O
channels

Traffic
controller,
scheduler

3.

Devices

Tapes, drums,

Spooling

What Is UNIX?
UNIX is a computer operating system, a
control program that works with users to
run programs,
manage resources, and
communicate with other computer systems.

Several people can use a UNIX computer


at the same time; hence UNIX is called a
multiuser system. Any of these users can
also run multiple programs at the same
time; hence UNIX is called multitasking.
38

UNIX Operating System


UNIX OS is slightly different with other OS,
This OS is not a single large program, but a
combination of a small kernel.
It is important to know at this point whenever you log
in successfully, the system starts executing a
program called shell.
Typing Appropriate commands when user are in shell

interprets whatever command user type on terminal .

In order to execute the command, it


calls an appropriate program from
the other part of system, know as
kernel.

UNIX SYTEM ARCHITECTURE


UNIX OS has mainly following parts:
Kernel : A main functional unit,
interacts with the hardware.
Shell. An outer most unit, interact with
the user to perform functions submitted
by the user.
File system. The idea was to keep the
units related but still separate so that
the entire program did not part itself in
the memory and slow down the system.

Layered structure of UNIX


OS
The users
Shells and commands
Compilers and Interpreters
System Libraries

System call interface to the kernel


Signals terminal handling
scheduling
Character I/O stream
Page replacement
Terminal drivers
Demand paging

File system , Swapping block I/O

CPU

system Disk and tape drivers

Virtual memory
Kernel interface to the hardware

The Unix Shell


The shell is the interface to the Unix OS.
It enable you to run application programs.
It is run when you log into a Unix OS. The main
features of the shell are as under:
1. Interactive
2. Can run programs in the background
3. Input/output redirection
4. Programs can be chained or connected together via
pipes.
5. Wild-card characters are supported in filenames .
6. Scripts files simplify repeating commands sequences .

7. Provide environment variables


8. A macro language for building shell scripts.

There are many different shell available in Unix.


Some famous shells are as under in table below

Shell
Nam
e

Invok
ed
with

Defaul Develo
t
pe r
Promt

Remarks

Korn
shell

ksh

AT & T

A shell of growing popularity


upwardly compatible with the
Broune shell . Features include
command line editing using EMACS
or keystrokes and file name
expansion

Bourn sh
e
shell

AT & T

An old shell that lack aliasing


command history, command
editing, and job control . Model
after ALGOL.

C
shell

csh

UC
A popular shell in university
Berleley environments with a syntax similar
to the C language.

Bash
shell

bash

Bash $

Free
softwar

A newer, POSIX compatible shell


that attempts to be a superset of

Typical UNIX file system


The file system is organized as a tree with a
single root node called root (written f);
every non-leaf-node of the file system
structure is a directory of files,
and files at the leaf nodes of the tree are
either directories or regular files or
special device files. Idey contains device
files , such as/ dev/console,/dev0,/dev/mto and
so on,/dev/mt0 and so on;/bin contains the
binaries of essential of UNIX system programs .

dev

de
v

bin

con
sole

csh
sh

lib

usr

tmp

Ra
m

pas
swo
rd

incl
ude

Shy
am

lib

etc

gro
up

unix

Gop
al

Logging on to a UNIX
machine
Because UNIX is a multiuser operating
system, you need to start by finding a
terminal, computer, or other way to access
the system.
Connect your terminal or PC to the UNIX
system until the point where you see a login
prompt (login:) on your screen. Use the
phone and modem to dial up the computer if
you need to.

46

Connecting a PC (MS-Windows) to UNIX


System
The machine that we are going to connect is called
hyperion.labs.itu.edu.tr
Select Start Run and type telnet in the empty box
This will start the telnet program, in the telnet window

47

Connecting a PC (MS-Windows) to UNIX


System
The machine that we are going to connect is called
hyperion.labs.itu.edu.tr
Select Start Run and type telnet in the empty box
This will start the telnet program, in the telnet window

48

Choose Connect Remote System


A new window will come up to the screen, in the Host Name section, type
the above name, leave other parts as their default values

When you press connect button


the login screen will appear as
seen on the next slide.

49

To log in to your Unix


account, enter your
login name at the
login:
prompt and press
<RETURN>

login: your-login-name

You will now see the password: prompt:


Enter your password exactly and press <RETURN>. Your password will not be
displayed on the screen as you type it. Your login and password are casesensitive.

50

You are now logged into the computer and will have a prompt that reflects the
computer's name. For example, if you connect to hyperion machine, the
prompt looks like this:
[dag@hyperion dag]$
Once youve successfully entered your account name and password, you are
shown some information about the system, some news for users, and an
indication of whether you have electronic mail. The specifics will vary.
At this point, youre ready to enter your first UNIX commandexitto sign off
from the computer system. entering exit shuts down all my programs
If you have a direct connection to the computer, logging out causes the
system to prompt for another account name, enabling the next person to use
the system.

51

Shell Commands of
UNIX
Unix Commands

When you first log into a unix system, you are


presented with something that looks like the
following:

/
home/larry#

That something is called a prompt. As its name


would suggest, it is prompting you to enter a command.
Every unix command is a sequence of letters,
numbers and characters. But there are no spaces.

52

Unix is also case-sensitive. This means that cat


and Cat are different commands.
The prompt is displayed by a special
program called the shell.
Shells accept commands, and run those
commands.
They can also be programmed in their own
language. These programs are called shell
scripts.

53

There are two major types of shells in unix:


Bourne shells
C shells.

Steven Bourne wrote the original unix shell


sh, and most shells since then end in the
letters sh to indicate they are extentions on
the original idea
Linux comes with a Bourne shell called bash
written by the Free Software Foundation.
bash stands for Bourne Again Shell and is
the default shell to use running linux

54

When you first login, the prompt is


displayed by bash, and you are running
your first unix program, the bash shell.
As long as you are logged in, the bash
shell will constantly be running.

55

Unix command line

File and file system management: cat | cd | chmod |


chown | chgrp | cp | du | df | file | fsck | ln | ls | lsof | mkdir |
mount | mv | pwd | rm | rmdir | split | touch
Process management: at | chroot | crontab | kill | killall |
nice | pgrep | pidof | pkill | ps | sleep | time | top | wait |
watch
User Management/Environment:env | finger | id | mesg |
passwd | su | sudo | uname | uptime | w | wall | who | whoami
| write
Text processing:awk | cut | diff | ex | head | iconv | join |
less | more | paste | sed | sort | tail | tr | uniq | wc | xargs
Shell programming:echo | expr | printf | unset
Printing:lp
Communications:
inetd | netstat | ping | rlogin | traceroute
Searching:
find | grep | strings
Miscellaneous:
banner | bc | cal | man | size | yes
56

Programming Tools

bash Bourne Again SHell, (mostly) sh-compatible and cshcompatible, standard shell on Linux systems and Mac OS X.
csh C shell. Written by Bill Joy for BSD systems.
ksh Korn shell, standard shell on many proprietary Unix systems,
powerful successor to the Unix Bourne shell (sh), written by David
Korn,
rc originally written for Plan 9.
sh Bourne shell, only shell present on all UNIX and Unix-like
systems, written by Stephen Bourne for Version 7 Unix.
tcsh TENEX C shell, standard shell on BSD systems.
zsh Z shell.
awk Standard Unix pattern scanning and text processing tool.
perl Perl scripting language.
PHP PHP scripting language.
Python Python scripting language.

57

Unix Commands

obtaining
help
The man command displays reference pages
for the command you specify.
The UNIX man pages (man is short for
manual ) cover every command available.
To search for a man page, enter man
followed by the name of the command to
find .
For example:
bagriy@sariyer:~> man ls

58

To exit
Press q
59

man (obtaining help)


There is also a keyword function in man.
For example;
If you are interested in any commands that deal with
Postscript, the printer control language for Adobe
Type man -k ps or man -k Postscript,
youll get a listing of all commands, system calls, and
other documented parts of unix that have the word
ps (or Postscript) in their name or short
description.
This can be very useful when youre looking for a tool
to do something, but you dont know its name-or if it
even exists!
60

cat
cat command is used to concatenate or displays
the contents of a file.
To use it, type cat, and then press enter key:
/home/larry# cat

This produces the correct result and runs the cat program.
Prompt

Command
f you type this row
and then press
enter

The text indicates what we


typed to cat
To end many unix command, type end-of-file command (EOF) [hold down
the key labeled Ctrl and press d (Ctrl+d) ]
61

To display the contents of a file, type


cat filename

62

To see linux commands press Tab key,


If you want to learn commands beginning with c you can write c
then press Tab key
/home/larry# c

63

Storing information
Unix provides files and directories.
A directory is like a folder: it contains pieces of
paper, or files.
A large folder can even hold other folders-directories
can be inside directories.
In unix, the collection of directories and files is called
the file system. Initially, the file system consists of
one directory, called the root directory
Inside root directory, there are more directories, and
inside those directories are files and yet more
directories.

64

Each file and each directory has a name.


A short name for a file could be joe,
while its full name would be /home/larry/joe. The
full name is usually called the path.
The path can be divide into a sequence of directories.

For example, here is how /home/larry/joe is


read:

/home/larry/joe
The initial slash indicates the root
directory. This signifies the directory
called home. It is inside the root directory.
The second slash corresponds to the
directory larry, which is inside home.

joe is inside larry.


65

A path could refer to either a directory or a filename, so

joe could be either.


All the items before the short name must be directories.

Root Directory

Directory

Directory

home

Sub-Directory

Directory
structure

File

larry

File

joe

66

Looking at directories with Is i


The command ls lists files.
If you try ls as a command, youll see:
/home/larry# ls
/home/larry#
That is right, you will see nothing.

If you have files, ls


lists the names of

files in the
directory

67

If you want a list of files of a more active


directory, try the root directory.
/home/larry# ls /
bin etc
install mnt root user var
dev home lib
proc tmp usr vmlinux
/ is a parameter saying what directory you want a list for.
Some commands have special parameters called options or switches.
To see this try:

/home/larry# ls F /
bin etc/
install/ mnt/ root/ user/ var/
dev/ home/ lib/
proc/ tmp/ usr/ vmlinux/
The -F is an option. It displays file types.
68

An option is a special kind of parameter that starts


with a dash -
An option modifies how the program runs, but not
what the program runs on.
For ls, -F is an option that lets you see which ones
are directories, which ones are special files, which
are programs, and which are normal files.
Anything with a slash / is a directory.
ls -l file* displays files starting with file
ls l displays all details

69

Many unix commands are like ls.


They have options, which are generally
one character after a dash, and they
have parameters.

Unlike ls, some commands require


certain parameters and/or options. You
have to learn these commands.

70

passwd
With

the passwd command, you can


change the password associated with
your individual account name.
For example,

sariyer:~> passwd
Changing password for dag.
Old password:
New passwd:
Retype new passwd:
sariyer:~>

71

pwd
pwd (present working directory) tells
you your current directory.
Most commands act, by default, on the
current directory. For instance, ls without any
parameters displays the contents of the
current directory.

cd

cd is used to change directories.


The format of this command :
cd new-directory (where new-directory is the
name of the new directory you want).
72

For instance, try:


/home/larry# cd /home
/home#

If you omit the optional parameter directory, youre returned to your


home, or original directory. Otherwise, cd will change you to the specified
directory.
There are two directories used only for relative pathnames:
The directory . refers to the current directory
The directory .. refers to the parent directory
These are shortcut directories.
The directory .. is most useful in backing up:

/usr/local/bin# cd ..
/usr/local#
73

mkdir
mkdir (make directory) is used to create a new
directory,
It can take more than one parameter, interpreting
each parameter as another directory to create.

rmdir
rmdir (remove directory) is used to remove a directory,
rmdir will refuse to remove a non-existant directory,
as well as a directory that has anything in it.

74

Moving Information
The primary commands for manipulating files
under unix are cp, mv, and rm. They stand for
copy, move, and remove, respectively.

cp
cp is used to copy contents of file1 to file2
cp file1 file2 (contents of file1 is copied to file2 in
the same directory)
cp folder1/file1 folder2 (contents of file1 is copied
to file1 in the inside of folder2 directory)

75

rm
rm is used to remove a file.
rm filename ---> removes a file named filename

mv

mv is used to move a file.


rm filename ---> removes a file named
filename
looks like cp, except that it deletes the original file
after copying it.
mv will rename a file if the second parameter is a file.
If the second parameter is a directory, mv will move
the file to the new directory, keeping its shortname
the same.
76

Some Other UNIX Commands


The Power of Unix
The power of unix is hidden in small
commands that dont seem too useful when
used alone, but when combined with other
commands produce a system thats much
more powerful, and flexible than most
other operating systems.
The commands include sort, grep, more, cat, wc,
spell, diff, head, and tail.

77

Operating on Files
In addition to the commands like cd, mv, and rm,
you learned in shell section, there are other
commands that just operate on files, but not the
data in them.
These include touch, chmod, du, and df.
All of these files dont care what is in the file.

78

Some of the things these commands manipulate:


The time stamp: Each file has three dates
associated with it. These are creation time, last
modification time and last access time.
The owner: the owner of files
The group: the group of users
The permissions: read, write, execute permissions
of files. The permissions tell unix who can access
what file, or change it, or, in the case of programs,
execute it. Each of these permissions can be
toggled separately for the owner, the group, and
all the other users.
79

other
s

owne
r
group

drwxr-xr-x 2 dag users 6 Dec 6 2000 netscape


file name

read, write, execute


permissions of files

touch
touch will update the time stamps of the
files listed on the command line to the
current time.
If a file doesnt exist, touch will create it.

80

chmod

Chmod (change mode) is used to change the permissions on a file.

(owner) (group) (others)


chmod [number][number][number] file1
Number = (read)4 + (write)2 + (execute)1
Example:

Chmod 754 file1

for owner: read, write and execute permissions


(4+2+1)
for group: read and execute permissions (4+0+1)
for others: only read permission (4+0+0)
81

System Statistics

Commands in this section will display statistics about


the operating system, or a part of the operating system.

du

du (disk usage) will count the amount of disk space for a given directory,
and all its subdirectories take up on the disk.

df

df (disk filling) summarizes the amount of disk space in use.


For each file system, it shows the total amount of disk space, the amount
used, the amount available, and the total capacity of the file system thats
used.
82

uptime

It prints the amount of time the system has been


upthe amount of time from the last unix boot
uptime also gives the current time and the load
average. The load average is the average number of
jobs waiting to run in a certain time period.

who
Displays the current users of the system and when they logged in.
If given the parameters am i (as in: who am i), it displays the current
user.

83

Whats in the File?

There are two major commands used in unix for


listing files, cat, and more.

cat

cat shows the contents of the file.


cat [-nA] [file1 file2 . . . fileN]
cat is not a user friendly command-it doesnt wait for you to read the file,
and is mostly used in conjuction with pipes.
However, cat does have some useful command-line options. For instance,
n will number all the lines in the file, and A will show control characters.

84

more
more is much more useful, and is the command that youll want to use when
browsing ASCII text files
more [-l] [+linenumber}] [file1 file2 ... fileN]
The only interesting option is l, which will tell more that you aren't
interested in treating the character Ctrl-L} as a ``new page'' character. more
will start on a specified linenumber.

head
head will display the first ten lines in the listed files.
head [- lines}] [l file1 file2 ... fileN]
Any numeric option will be taken as the number of
lines to print, so head -15 frog will print the first
fifteen lines of the file frog
85

tail
Like

head, tail display only a fraction of the file.


tail also accepts an option specifying the number of
lines. tail [-lines] [l file1 file2 ... fileN]

file
file command attempts to identify what format
a particular file is written in.
file [file1 file2 ... fileN]
Since not all files have extentions or other easy to
identify marks, the file command performs some
rudimentary checks to try and figure out exactly what
it contains.

86

Information Commands
The commands that will alter a file, perform a certain
operation on the file, or display statistics on the file.

grep
grep is the generalized regular expression parser.
This is a fancy name for a utility which can only search a text file.
grep [-nvwx] [-number] { expression} [file1 file2 ... fileN]

87

wc
wc (word count) simply counts the number of
words, lines, and characters in the file(s).
wc [-clw] [file1 file2 ... fileN]
The three parameters, clw, stand for character, line,
and word respectively, and tell wc which of the three
to count.

spell

spell is very simple unix spelling program,


usually for American English. spell is a filter, like
most of the other programs weve talked about.
spell [file1 file2 ... fileN]

88

cmp
cmp compares two files.
The first must be listed on command line, while
the second is either listed as the second
parameter or is read in form standard input.
cmp is very simple, and merely tells you where
the two files first differ.
cmp file1 [ file2]

89

diff
One of the most complicated standard unix
commands is called diff.
The GNU version of diff has over twenty
command line options. It is a much more
powerful version of cmp and shows you what
the differences are instead of merely telling
you where the first one is.
diff file1 file2

90

gzip [-v#] [file1 file2 ... fileN]


gunzip [-v] [file1 file2 ... fileN]
zcat [{file1 file2 ... fileN]
These three programs are used to compress and decompress data.
gzip, or GNU Zip, is the program that reads in the original file(s) and
outputs files that are smaller.
gzip deletes the files specified on the command line and replaces them
with files that have an identical name except that they have .gz appended
to them.

91

tr
The translate characters command operates on
standard input-it doesnt accept a filename as a
parameter.
Instead, its two parameters are arbitrary strings.
It replaces all occurences of string1 in the input string2.
In addition to relatively simple commands such as tr
frog toad, tr can accept more complicated commands.
tr string1 string2

92

Editors
There are a lot of available editors under linux
operating system.
Amongst these vi is the most common one. One can
claim that every unix system has vi.
The other famous editor is emacs which has some
artificial intelligence properties.
The mailing facility pine uses the pico editor.
However, perhaps the simplest one of the editors is
joe.
joe has a lot of flexible features of emacs and pico
beside the user friendliness of the turbo type of
editors.

93

You might also like