What is Unix?
UNIX is an operating system which was first
developed in the 1960s, and has been under constant development ever since. By
operating system, we mean the suite of programs which make the computer work.
Unix is a multi user and multitasking Operating System.
Why Unix?
o Before
Unix has come, Operating systems were designed in low level language.
o So they
were hardware specific.
o Unix is
written in C language and hence can be used in different systems.
Features of Unix:
o Multi user Operating System – Unix is a
multi-user operating system allowing several users to work on the computer
system at the same time. The users can sign on the system through multiple
terminals connected to a cost computer. To provide a multi user environment
Unix uses time sharing method.
o Multi tasking Operating System – Unix allows a
user to perform a number of tasks at the same time. This can be done by placing
some task in the back ground while the user continues to work in the
foreground.
o Portability – Portability refers to the
ability of software that operate one machine to operate as efficiently on
another, different machine, with out making major changes to the software.
o Modularity – The Unix system comes with a
large number of utility program which are designed to independently perform a
specific task. These utilities can also be designed to work together under the
direction of the Unix shell.
o System Security – Unix being a nulti-user
operating system offers protection to one uses information from another. It
maintains a list of users who are allowed to access the system. New users have
to added to the list before they can have an access to the system.
o File structure & Security – Unix has
hierarchical file structure. It is like an upside down tree with the roots at
the top. The Unix file structure allows the files to grow dynamically. Unix
allows the file to be organized into various directories for easy access. Unix
provides security at file & directory level. Read, Write and Execute
permission are given to each file at three levels.
User- Owner of file.
Group- The members of a group have the same group-id
but different user ids.
Others- The other members are the one who are
neither the owner nor group members of a file.
o Device Independence – Unix system
considered all device connected to it as files, this allow I/O independent of
the H/W device actually accessed. The special files through which the devices
are accessed are called device driver.
Unix Architecture:
The UNIX operating system is made up of three parts; the
kernel, the shell and the programs.
The kernel of UNIX is the hub of
the operating system: it allocates time and memory to programs and handles the
filestore and communications in response to system calls.
As an illustration of the way that the
shell and the kernel work together, suppose a user types rm myfile (which has
the effect of removing the file myfile). The shell searches the filestore for
the file containing the program rm, and then requests the kernel, through
system calls, to execute the program rm on myfile. When the process rm myfile
has finished running, the shell then returns the UNIX prompt % to the user,
indicating that it is waiting for further commands.
The
shell acts as an interface between the user and the kernel. When a user
logs in, the login program checks the username and password, and then starts
another program called the shell. The shell is a command line interpreter
(CLI). It interprets the commands the user types in and arranges for them to be
carried out. The commands are themselves programs: when they terminate, the
shell gives the user another prompt (% on our systems).
Unix File Systems:
Everything in UNIX is either a file or a process. A process
is an executing program identified by a unique PID (process identifier). A file
is a collection of data. They are created by users using text editors, running
compilers etc. .
The
Directory Structure :
All the files are grouped together in the
directory structure. The file-system is arranged in a hierarchical structure,
like an inverted tree. The top of the hierarchy is traditionally called root
(written as a slash / )
In the diagram above, we see that the home directory of the
undergraduate student "ee51vn" contains two sub-directories (docs and
pics) and a file called report.doc.
The full path to the file report.doc is
"/home/its/ug1/ee51vn/report.doc"
File Attributes:
o
Name (e.g. “test.log”)
o
Owner (e.g. “mckv”)
o
Access Rights (e.g. Read, Write, Execute )
o
Other attributes (e.g. date of cration)
File Types:
Every
item in a Unix file system belongs to one of the four possible types:
A. Ordinary/Regular
Files –
o Contains
text, data, or program information.
o Cannot
contain another file, or directory.
o Can be
thought of as one-dimensional array of bytes.
o Two types
of ordinary files – Text file, Binary file.
B. Directory
Files
o Contains
directory(s) and / or file(s) within it.
o Has one
line for each item contained within the directory.
o Each line
in a directory file contains only the name of the item, and a numerical
reference to the location of the item called
inode number.
o inode
number is an index to a table known as
the inode table. The inode stores all
information about the file except its name & contents.
C. Device/Special
Files
o Physical
devices (printers, terminals etc.) are represented as “files”
o This way,
the commands used to access ordinary files also work
with the physical devices.
D. Symbolic
Links
o A
symbolic link is an indirect pointer to a file.
o Two types
of Links- Hard Link, Soft Link
o Hard /
Soft links can be created using ln command
Hard Link-
Ø Ii is
another name for a file.
Ø All names
provided to a single file have the same inode number.
Soft
Link-
Ø It
provides the path name of the file.
Ø Windows
shortcuts are like soft links.
Inode:
Ø Inode is
a data structure containing useful information about an item in the unix file
system.
Inode reside on disk and do
not have name. They have indices (numbers) indicating their positions in the
array of indoes as shown in following figure.
Ø Contents
of Inode-
o The
location of the file on the disk.
o The
file’s type (e.g. ordinary file, directory, symbolic link).
o The
file’s size, in bytes, if applicable.
o The time,
the file’s contents were last modified.
o The time,
the file was last accessed for read(), exec(), etc.
o A
reference count: the number of links the file has.
o The
file’s owner (a UID)
o The
file’s group (a GID)
o The
file’s mode bits (also called file permission or permission bits).
How does Unix access files ?
>
Suppose we want to see the contents of a file
reports in the current directory mydir.
>
Firs it would look up for the inode number entry in the directory for the file
reports.
>
Using this inode number it accesses the inode table to find out whether we have
the read permission for that file.
>
If the permission is there, the contents of the file are read from the disk
address mentioned in the inode entry of reports and displays it.
Path
Name-
>
Every item in the file system with a name can be specified with a path name.
>
Path name represents the path to the entry from the root of the file system.
>
Path names can be absolute or relative.
Unix users:
o Super
user
a) Can also
be referred to as a System Administrator.
b) Has an
overall authority on Unix OS.
c) Responsible
for OS maintenance, backup and recovery, user management etc.
d) Super
user login is root and prompt is #
o Owner
a) Is a user
who creates a file.
b) For every
Unix file there can be only one owner.
c) File
owner can assign the file permission to group and other users.
o Group
a) In Unix,
groups can be formed based on area of work.
b) Super
user can create a group and assign members to it.
c) Owner of
a file can decide what permissions to be given to group members.
o Others
a) User who
is not a owner and does not belong to any specific group is referred to as a
other user.
b) Owner of
a file can decide which permissions to be given to other users.
Assg-1. Study and implement
general purpose commands in UNIX. (Date, Who, Who am I, Cal, Echo, Clear, Mesg,
Mail, and Login Command).
‘Date’ Commands-
$
date commands prints or sets the system date and time.
$ date
$
date -r
TestFile
>
It displays the last modification time of the file “TestFile”
>
date command can be used with suitable format specifiers as argument. Each
format is preceded by a + symbol, followed by the % operator, and a single character
describing the format.
Sequence
Interpretation
+%a abbreviated
weekday name (Mon .. Sun)
+%b abbreviated
month name (Jan .. Dec)
+%d day
of month (01 .. 31)
+%r time
(12- hour)
+%T time
(24- hour)
+%y last
two digits of year (00 .. 99)
+%D date
(mm/dd/yy)
e.g. $
date ‘Today is %a %m %Y’
‘Cal ’
Commands-
$
cal command prints the calendar of the current month.
>
$ cal 2007 – commands prints the calendar of the specified year.
>
$ cal 2 2007 – command prints the calendar of the specified month.
‘Who’ Commands-
$
who command shows who is logged on.
‘Who am i’ Commands-
$
who am i command shows who are you.
>
We can type a command over two or more lines. A backslash character before the
end of the line followed by a new line is taken to be continuation of the line.
$
who \
>
am \
>
i \
‘echo’ Commands-
$ echo
command, like its counterparts in MSDOS, displays its argument (excluding the
option)
>
Syntax: echo [OPTION]... [STRING]...
> $ echo Good Morning
Good
Morning
>
$ echo * -- show a list of all files in the current directory.
>
$ echo \* OR $ echo ‘*’ -- show *
>
$ echo ‘Enter your name : \c’
Enter your name : $ _ Observe that the prompt has been returned,
not in the next line, but at the end of the echoed string.
>
$ echo ‘This is system program lab’ > lab/data.txt
--write
message into a file.
>
$ echo "Today's date is $(date)"
‘Clear’ Commands-
>
$ clear - is a standard Unix computer operating system command which is used
to clear the screen.
Assg-2. Study and implement all
the directory oriented commands of Unix (cd, mkdir, rmdir, pwd command)
‘cd’ Commands-
>
Changes the directory.
>
Syntax—cd [directory]
‘mkdir’ Commands-
>
The mkdir command creates a single directories or multiple directories.
>
Syntax—mkdir [options] directories
>
Options- -m mode
sets the access mode for the new directory.
-p
mode if the parent directories
don’t exist, this command creates them.
>
e.g. mkdir -m 4tech mkdir -p tech/lab/data
Assg-3. Study and implement all
the file oriented commands of Unix (lslist files, cat, cp, rm)
‘ls’ Commands-
>
The ls command lists all files in the directory that match the name. If name is
left blank, it will list all of the files in the directory.
> The syntax for the ls command is:
ls [options] [names]
Options:
-a Displays all files.
-b Displays nonprinting characters in octal.
-c Displays files by file timestamp.
-C Displays files in a columnar format (default)
-d Displays only directories.
-f Interprets each name as a directory, not a file.
-F Flags filenames.
-g Displays the long format listing, but exclude the owner name.
-i Displays the inode for each file.
-l Displays the long format listing.
-L Displays the file or directory referenced by a symbolic link.
-m Displays the names as a comma-separated list.
-n Displays the long format listing, with GID and UID numbers.
-o Displays the long format listing, but excludes group name.
-p Displays directories with /
-q Displays all nonprinting characters as ?
-r Displays files in reverse order.
-R Displays subdirectories as well.
-t Displays newest files first. (based on timestamp)
-u Displays files by the file access time.
-x Displays files as rows across the screen.
-1 Displays each entry on a line.
>
e.g. ls -la
Vi
editor:
The vi editor is available on almost all Unix
systems and in Linux. vi can be used from any type of terminal because it does
not depend on arrow keys and function keys--it uses the standard alphabetic
keys for commands.
vi is short for visual editor. It displays a
window into the file being edited that shows 24 lines of text. vi is a text
editor, not a "what you see is what you get" word processor. vi lets
user add, change, and delete text, but does not provide such formatting capabilities
as centering lines or indenting paragraphs.
Starting vi:
One may
use vi to open an already existing file by typing
vi filename
where "filename" is the name of the
existing file. If the file is not in the current directory, then he/she must
use the full pathname.
Or one may create a new file by typingvi newnamewhere "newname" is the name one wish to give the new file. For writing c++ codes in the new file the extension must be .cpp otherwise the compiler will not recognize that as a c++ program file.
vi Modes
:
vi has two modes:
- command mode
- insert mode
In command mode, the letters of the keyboard
perform editing functions (like moving the cursor, deleting text, etc.). To
enter command mode, one should press the escape <Esc> key.
In insert mode, the letters type form words and
sentences. Unlike many word processors, vi starts up in command mode.
Entering
Text:
In order to begin entering text in this empty
file, one must change from command mode to insert mode. To do this, user must
type
i
Nothing appears to change, but user is now in
insert mode and can begin typing text. In general, vi's commands do not display
on the screen and do not require the Return key to be pressed.
Type a few short lines and press <Return>
at the end of each line. If anyone types a long line, he/she will notice the vi
does not word wrap, it merely breaks the line unceremoniously at the edge of
the screen.
If anyone makes a mistake, pressing
<Backspace> or <Delete> may remove the error, depending on user’s
terminal type.
Moving
the Cursor:
To move the cursor to another position, one must
be in command mode. If one has just finished typing text, he/she is still in
insert mode. Go back to command mode by pressing <Esc>. If one is not
sure which mode he/she is in, press <Esc> once or twice until one hears a
beep. When he/she hears the beep, he/she is in command mode.
The cursor is controlled with four keys: h, j, k,
l.
Key Cursor Movement
--- ---------------
h left one space
j down one line
k up one line
l right one space
When the user has gone as far as possible in one
direction, the cursor stops moving and he/she hears a beep. For example, one
cannot use l to move right and wrap around to the next line, he must use j to
move down a line.
Basic
Editing:
Editing commands require that user be command
mode. Many of the editing commands have a different function depending on
whether they are typed as upper- or lowercase. Often, editing commands can be
preceded by a number to indicate a repetition of the command.
Deleting
Characters:
To delete a character from a file, move the
cursor until it is on the incorrect letter, then type
x
The character under the cursor disappears. To
remove four characters (the one under the cursor and the next three) type
4x
To delete the character before the cursor, type
X (uppercase)
Deleting Words:
To delete a word, move the cursor to the first
letter of the word, and type
dw
This command deletes the word and the space
following it.
To delete three words type
3dw
Deleting
Lines:
To delete a whole line, type
dd
The cursor does not have to be at the beginning
of the line. Typing dd deletes the entire line containing the cursor and places
the cursor at the start of the next line. To delete two lines, type
2dd
To delete from the cursor position to the end of
the line, type
D (uppercase)
Replacing
Characters:
To replace one character with another:
- Move the cursor to the character to be replaced.
- Type r
- Type the replacement character.
The new character will appear, and you will still
be in command mode.
Replacing
Words:
To replace one word with another, move to the
start of the incorrect word and type
cw
The last letter of the word to be replaced will
turn into a $. User is now in insert mode and may type the replacement. The new
text does not need to be the same length as the original. Press <Esc> to
get back to command mode. To replace three words, type
3cw
Replacing
Lines:
To change text from the cursor position to the
end of the line:
- Type C (uppercase).
- Type the replacement text.
- Press <Esc>.
Inserting
Text:
To insert text in a line:
- Position the cursor where the new text should go.
- Type i
- Enter the new text.
The text is inserted BEFORE the cursor.
4. Press
<Esc> to get back to command mode.
Appending
Text:
To add text to the end of a line:
- Position the cursor on the last letter of the line.
- Type a
- Enter the new text.
This adds text AFTER the cursor.
4. Press
<Esc> to get back to command mode.
Opening
a Blank Line:
To insert a blank line below the current line,
type
o (lowercase)
To insert a blank line above the current line,
type
O (uppercase)
Joining
Lines:
To join two lines together:
- Put the cursor on the first line to be joined.
- Type J
To join three lines together:
- Put the cursor on the first line to be joined.
- Type 3J
Undoing:
To undo your most recent edit, type
u
To undo all the edits on a single line, type
U (uppercase)
Undoing all edits on a single line only works as
long as the cursor stays on that line. Once anyone moves the cursor off a line,
no one cannot use U to restore the line.
Moving Around in a File:
There are shortcuts to move more quickly though a file. All these work in
command mode. Key Movement
--- --------
w forward word by word
b backward word by word
$ to end of line
0 (zero) to beginning of line
H to top line of screen
M to middle line of screen
L to last line of screen
G to last line of file
1G to first line of file
<Control>f scroll forward one screen
<Control>b scroll backward one screen
<Control>d scroll down one-half screen
<Control>u scroll up one-half screen
Moving
by Searching:
To move quickly by searching for text, while in command mode:- Type / (slash).
- Enter the text to search for.
- Press <Return>.
To repeat the search in a forward direction, type
nTo repeat the search in a backward direction, type
N
Closing and Saving a File:
With vi, you edit a copy of the file, rather than the original file. Changes
are made to the original only when you save your edits.To save the file and quit vi, type
ZZ
The vi editor editor is built on an earler Unix text editor called ex. ex
commands can be used within vi. ex commands begin with a : (colon) and end with
a <Return>. The command is displayed on the status line as you type. Some
ex commands are useful when saving and closing files.To save the edits you have made, but leave vi running and your file open:
- Press <Esc>.
- Type :w
- Press <Return>.
- Press <Esc>.
- Type :q!
- Press <Return>.
Command Summary:
STARTING vivi filename edit a file named "filename"
vi newfile create a new file named "newfile"
Entering Text:i insert text left of cursor
a append text right of cursor
Moving the Cursor: h left one space
j down one line
k up one line
l right one space
Basic Editing: x delete character
nx delete n characters
X delete character before cursor
dw delete word
ndw delete n words
dd delete line
ndd delete n lines
D delete characters from cursor to end of line
r replace character under cursor
cw replace a word
ncw replace n words
C change text from cursor to end of line
o insert blank line below cursor
(ready for insertion)
O insert blank line above cursor
(ready for insertion)
J join succeeding line to current cursor line
nJ join n succeeding lines to current cursor line
u undo last change
U restore current line
Moving Around in a File: w forward word by word
b backward word by word
$ to end of line
0 (zero) to beginning of line
H to top line of screen
M to middle line of screen
L to last line of screen
G to last line of file
1G to first line of file
<Control>f scroll forward one screen
<Control>b scroll backward one screen
<Control>d scroll down one-half screen
<Control>u scroll up one-half screen
n repeat last search in same direction
N repeat last search in opposite direction
Closing and Saving a File:ZZ save file and then quit
:w save file
:q! discard changes and quit file
Using g++ compiler for c++ program in Linux:
To write c++ program in Linux user must open a new file using vi editor with extension .cpp otherwise the file will not be recognized as c++ program files. After writing the c++ program the file must be saved in the current or any other directory. The next step is to compile the file for generate the output. For this purpose g++ compiler can be used for compiling the source code. The command to be used for compilation is as follows:
g++ -o <outputfilename> <sourcefilename>
./<outputfilename>
Let us consider if the source file name be sample.cpp, and if want the output file name be sample.out then the command will be as follows:
g++ -o sample.out sample.cpp
after entering the command if no errors are shown then the file is error free and is compiled successfully. At the same time the corresponding output file will be generated in the name of sample.out. To execute the file the command will be:
./sample.out
The user will see the output.
No comments:
Post a Comment