UNIX Module 1 notes
UNIX Module 1 notes
Programming(BCS5151C)
MODULE-1
Module 1: Introduction: Unix Components/Architecture. Features of Unix. The UNIX Environment
and UNIX Structure, Posix and Single Unix specification. General features of Unix commands/
command structure. Command arguments and options. Basic Unix commands such as echo, printf,
ls, who, date,passwd, cal, Combining commands. Meaning of Internal and external commands. The
type command: knowing the type of a command and locating it. The root login. Becoming the super
user: su command.
Unix files: Naming files. Basic file types/categories. Organization of files. Hidden files. Standard
directories. Parent child relationship. The home directory and the HOME variable. Reaching required
files- the PATH variable, manipulating the PATH, Relative and absolute pathnames. Directory
commands – pwd, cd, mkdir, rmdir commands. The dot (.) and double dots (..) notations to represent
present and parent directories and their usage in relative pathnames. File related commands – cat,
mv, rm, cp, wc and od commands.
Text Book:
Unix Concepts and Applications, Sumitabha Das, Tata McGraw Hill , 4th Edition
History of Unix OS
Prior to Unix, many operating systems ran collections or “batches” of operations one at a
time.
This single-user “batch-processing” approach did not take advantage of the potential
processing power and speed of computers.
History of Unix OS
The Unix OS was developed (based on Multics & CTSS operating systems) by Ken
Thompson at the AT&T Bell Laboratories in 1969. He wanted to create an multi-user
operating system to run “space travel” game.
Ken‟s philosophy was to create an operating system with commands or “utilities” that
would do one thing well (i.e. UNIX). Pipes could be used combine commands...
The first versions of UNIX were written in “machine-dependent” program (such as PDP-7).
Ken Thompson approach Dennis Ritchie developer of C program), and in 1973 they
compiled UNIX in C programming language to make operating system “portable” to other
computers systems.
The entire UNIX system is supported by a handful of essentially simple and abstract
concepts.
The UNIX architecture has three important agencies:
1. Division of labor: Kernel and shell
2. The file and process
3. The system calls
a) The Kernel
The core of the operating system - a collection of routines mostly written in C.
It is loaded into memory when the system is booted and communicates directly
with the hardware.
User programs (the applications) that need to access the hardware use the services
of the kernel, which performs the job on the user's behalf.
b) The Shell
Computers don't have any capability of translating commands into action.
That requires a command interpreter, also called as the shell.
Shell is actually interface between the user and the kernel.
Most of the time, there's only one kernel running on the system, there could be
several shells running – one for each user logged in.
The shell accepts commands from user, if require rebuilds a user command, and
finally communicates with the kernel to see that the command is executed.
Example: $ echo VTU Belagavi
#Shell rebuilds echo command by removing multiple spaces
VTU Belagavi
Types of shell - There are 3 types of Shell:
1.) Bourne shell ($) ---- > Bash ($)
2.) C (%) ----- > tesh (%)
3.) korn ($)
The Process:
Features Of UNIX
UNIX is an operating system, so it has all the features an operating system is expected to have:
A Multiuser System
A Multitasking System
The building-block approach
The UNIX toolkit
Pattern Matching
Programming Facility
Documentation
Portable
Networking
Organization of File System
1. A Multiuser System:
UNIX is a multiprogramming system; it permits multiple programs to run and
compete for the attention of the CPU.
This can happen in two ways:
Multiple users can run separate jobs
A single user can also run multiple jobs
2. A Multitasking System:
A single user can also run multiple tasks concurrently.
UNIX is a multitasking system.
It is usual for a user to edit a file, print another one on the printer, send email to a
friend and browse www - all without leaving any of applications.
The kernel is designed to handle a user's multiple needs.
In a multitasking environment, a user sees one job running in the foreground; the
rest run in the background.
User can switch jobs between background and foreground, suspend, or even
terminate them.
5. Pattern Matching:
UNIX features very sophisticated pattern matching features.
Example: The * (zero or more occurrences of characters) is a special character used
by system to indicate that it can match a number of filenames.
Ex: ls chap* , ls chap+ , ls chap?
6. Programming Facility:
The UNIX shell is also a programming language it was designed for programmer,
not for end user.
It has all the necessary ingredients, like control structures, loops and variables, that
establish powerful programming language.
These features are used to design shell scripts – programs that can also invoke UNIX
commands.
Many of the system's functions can be controlled and automated by using these shell
scripts.
7. Documentation:
The principal on-line help facility available is the man command, which remains the
most important references for commands and their configuration files.
Apart from the man documentation, there's a vast ocean of UNIX resources
available on the Internet.
8. Portable: The programs that are written in C are easily moved from one hardware to
other hardware. It needs only standard compile.
9. Networking: Networking allows the users at one location to log into systems at other
sites. Once access is gained to a remote system user operate just as though they were on
their own system. TCP/IP protocol is used for communication
10. Organized File System: UNIX has a very organized file & directory system that allows
users to organize and maintain files.
11. Device Independence: UNIX treats input / output devices like ordinary files.
12. Utilities: UNIX provides rich library packages. A user can develop programs / products in
an efficient manner.
Unix Environments
1. Personal environment - Linux, Apple‟s System X (Unix kernel).
Allows single user to use Unix on personal computer.
Locating Commands
The UNIX is command-based system - i.e., things happens because the user enters
commands in.
UNIX commands are seldom more than four characters long.
All UNIX commands are single words like – cat, ls, pwd, date, mkdir, rmdir, cd, grep etc.
The command names are all in lowercase.
Example: $ LS
bash: LS: command not found
All UNIX commands are files containing programs, mainly written in C.
All UNIX commands (files) are stored in directories (folders).
If you want to know the location of executable program (or command), use type command
-
Example: $ type date
date is /bin/date
When you execute date command, the shell locates this file in the /bin directory and makes
arrangements to execute it.
The PATH
The sequence of directories that the shell searches to look for a command is
specified in its own PATH variable.
Example: $ echo $PATH
/bin: /usr/bin: /usr/local/bin: /usr/ccs/bin: /usr/local/java/bin:.
2. Internal commands:
When the shell executes command(file) from its own set of built-in commands
that are not stored as separate files in /bin directory, it is called internal
command.
Internal commands are something which is built into the shell.
For the shell built in commands, the execution speed is really high.
It is because no process needs to be spawned for executing it.
For example, when using the "cd" command, no process is created.
The current directory simply gets changed on executing it.
If the command exists both as an internal and external one, shell executes internal
command only.
Internal commands will have top priority compared to external command of same
name.
Example: echo, exit, kill, cd
$ type echo # echo is an internal command echo is shell built-in
Command Structure
To understand power of UNIX, user must know syntax of important UNIX commands.
The general syntax of UNIX command is – command arguments.
Example: cat note
command argument
Commands and arguments have to be separated by spaces or tabs to enable the system to
interpret them as words.
UNIX arguments range from the simple to the complex.
Arguments may consist of options, expressions, instructions and filenames etc.
General Command Format
Options
Options are special type of arguments mostly used with a minus (-) sign.
An option is normally preceded by a minus (-) sign to distinguish it from filenames or other
arguments:
command –optionname
Example: $ ls -l note # -l option list all the attributes of the file note
-rwxrwxrwx 1 mahesh mahesh 811 Jan 27 12:20 note
Options can normally be combined with only one (-) sign, i.e., instead of using $ ls -l -a -t -
d you might as well use $ ls -latd.
Example: $ ls -z note # Message from ls, not from shell ls:
illegal option –z
Filename Arguments
Many UNIX commands use a filename as argument so the command can take input from
the file.
If a command uses a filename as argument, it will generally be its last argument.
It's also quite common to see many commands working with multiple filenames as
arguments.
The command with its arguments and options is known as the command line.
This line can be considered complete only after the user has hit [Enter].
The complete line is then fed to the shell as its input for interpretation and execution.
Examples:
o $ ls -lat chap01 chap02 chap03 # Multiple filenames as arguments
o $ rm chap01 chap02
o $ cp chap01 chap01.bak
1. Combining Commands
UNIX allows you to specify more than one command in the single command line.
Example: $ ( wc note; ls -l note ) #Two commands combined here using ; &
parenthesis
2 3 16 note
-rw-rw-r-- 1 mahesh mahesh 16 Jan 30 09:35 note
$ ls | wc #Two commands combined here using filter
115 166 1227
To make cal pause using pager using the “ | “ symbol to connect them.
Example: $cal 2017 | more
date: DISPLAYING THE SYSTEM DATE
One can display the current date with the date command, which shows the date
and time to the nearest second:
Example: $ date
Mon Sep 4 16:40:02 IST 2017
The command can also be used with suitable format specifiers as arguments.
There are many other format specifiers, and the useful ones are listed below:
d – The day of month (1 - 31)
y – The last two digits of the year.
H, M and S – The hour, minute and second, respectively.
D – The date in the format mm/dd/yy
T – The time in the format hh:mm:ss
bash-4.1$ who am i
student pts/0 2020-09-07 09:53 (:0.0)
bash-4.1$ ls
1.c ccittu.c~ Documents Music Pictures sample.odt te.cpp
text
array.c~ Desktop Downloads pcd Public s.doc Templates
text1.c
bash-4.1$
Type Command
All UNIX commands are files containing programs, mainly written in C.
All UNIX commands(files) are stored in directories(folders).
If you want to know the location of executable program (or command), use type
Command.
Example:
$ type date
date is /bin/date
When you execute date command, the shell locates this file in the /bin directory and makes
arrangements to execute it.
The PATH:
The sequence of directories that the shell searches to look for a command is specified
in its own PATH variable.
Example:
$ echo $PATH
/bin: /usr/bin: /usr/local/bin: /usr/ccs/bin: /usr/local/java/bin:
Root login
The UNIX system provides a special login name for the exclusive use of the administrator,
it is called root.
This account doesn't need to be separately created but comes with every system.
Its password is generally set at the time of installation of the system and has to be used on
logging in:
o Becoming the super at login time
Login: root
Password: ********* [Enter]
#-
The prompt of root is #Once you login as a root you are placed in root‟s home
directory. Depending on the system, this directory could be / or /root.
Chapter -2 of Module1
UNIX Files
The File
The file is the container for storing information.
Neither a file's size nor its name is stored in file.
File Types
The UNIX has divided files into three categories:
1. Ordinary file – also called as regular file. It contains only data as a stream of
characters.
2. Directory file – it contains files and other sub-directories.
3. Device file – all devices and peripherals are represented by files.
Ordinary File – ordinary file itself can be divided into two types
a) Text File – it contains only printable characters, and you can often view the
contents and make sense out of them.
Example: C, C++, Java files are text files
Character Special (Device) File: Character device file is a physical device, that
reads or writes one character at a time.
Ex: Terminal
Block Special (Device) File – Block device file is a physical device, that reads or writes one
block at a time. Ex: Disk
Symbolic Link (soft) file – It is a logical file that defines the location of another file.
FIFO file – A first-in first-out is also known as a PIPE used in inter-processor communication.
Organization of files
Unix file system is a logical method of organizing and storing large amounts of information
in a way that makes it easy to manage.
A file is a smallest unit in which the information is stored.
Unix file system has several important features.
All data in Unix is organized into files. All files are organized into directories.
These directories are organized into a tree-like structure called the file system.
Files in Unix System are organized into multi-level hierarchy structure known as a directory
tree.
At the very top of the file system is a directory called “root” which is represented by a “/”.
All other files are “descendants” of root.
Hidden Files
UNIX allows users to have files which are not listed, by default, by the ls command.
These are called hidden files and are distinguishable from other files by the fact that their
filenames begin with a dot (.).
Such a file is .profile which is executed every time you log in to the system.
Hidden files are listed by adding the -a option to the ls command.
For example:
Type: ls –a
They are commonly used for storing user preferences or preserving the state of a utility, and
are frequently created implicitly by using various utilities.
The feature of UNIX file system is that there is a top, which serves as the reference point for
all files.
This top is called root and is represented by a / (Front slash).
The root is actually a directory.
The root directory (/) has a number of subdirectories under it.
The subdirectories in turn have more subdirectories and other files under them.
Every file apart from root, must have a parent, and it should be possible to trace the ultimate
parentage of a file to root.
In parent-child relationship, the parent is always a directory.
• This variable specifies the locations in which the shell should look for commands.
• Example:
$ echo $PATH
/bin: /usr/bin:
• When you specify a command like date, the system will locate the associated file from a list
of directories specified in the PATH variable and then executes it.
• Whenever you enter any UNIX command, you are actually specifying the name of an
executable file located somewhere on the system.
• The system goes through the following steps in order to determine which program to
execute:
Built in commands (such as cd and history) are executed within the shell.
If an absolute path name (such as /bin/ls) or a relative path name (such as
./myprog), the system executes the program from the specified directory.
Otherwise, the PATH variable is used.
Relative Path:
This method is compact and easy when ascending the directory hierarchy. The
command: cd .. Translates to this ―change your current directory to parent of
current directory‖.
The relative paths can also be used as:
$pwd
/home/student/cs/3cs
$cd ../..
$pwd
/home
Directory Command
Any time user can know the current working directory using pwd command.
Example:
Here, the content of the second file is shown immediately after the first file. So, this
command concatenates two files; hence its name (cat).
This command can also be used to create a new file.
Syntax: cat > FILENAME
Example:
$ cat > F3
WELCOME TO SHELL // contents of F3
[ctrl-d] // Terminates F3
$ cat F3
WELCOME TO SHELL // contents of F3
Using cat to redirect the contents of two files to another file :The following
command redirects the contents of the files named F1 and F2 to the file named all:
$cat F1 F2 > new
od Options:
i. byte (-b):
o -b option can be used to display octal value of each printable character.
o Each line displays 16 bytes of data in octal, preceded by the offset of
the first byte in the line.
o Example:
$ od –b chap01
offset <-------------- 16 bytes of data in octal ------------------->
0000000 141 142 143 144 040 145 146 147 148 040 040 040
040 040 040 012
0000020 141 142 143 144 040 145 146 147 148 040 040 040
040 040 040 012
ii. character (-c):
o -c option can be used to display the printable characters and its
corresponding octal value.
o Example:
$ od –bc chap01
0000000 141 142 143 144 040 145 146 147 148 040 040 040 040 040 040 012
a b c d e f g f \n
0000020 141 142 143 144 040 145 146 147 148 040 040 040 040 040 040 012
a b c d e f g f \n