@vtudeveloper - in Unix Mod 1
@vtudeveloper - in Unix Mod 1
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.
These programs access the kernel through a set of functions called system calls.
Functions of kernel:
▪ It manages the system's memory, schedules processes, decides their priorities
and so on.
▪ Process control
▪ Resources management.
▪ The kernel has to do a lot of this work even if no user program is running.
▪ The kernel is also called as the operating system - a programs gateway to the
computer's resources.
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:
▪ The process is the name given to the file when it is executed as a program (Process
is program under execution).
▪ We can say process is an “time image” of an executable file.
▪ We also treat process as a living organism which have parents, children and are born
and die.
➢ 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
Device Independence
Utilities
Services
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.
UNIX offers facility to add and remove many applications as and when required.
Tools include general purpose tools, text manipulation tools, compilers, interpreters,
networked applications and system administration tools.
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.
13. Services: It also includes the support utilities for system administration & control System
Administrator is responsible in maintaining system resources like Disk, security access.
➢ 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
➢ Exceptions
There are some commands that don't accept any arguments.
There are also some commands that may or may not be specified with arguments.
The ls command can run without arguments (ls), with only options (ls -l) and also with only
filenames like: (ls chap01 chap02).
Examples:
o $ pwd # pwd prints the current working directory /root
o $ who # who lists currently logged in users
o mahesh tty7 2013-01-30 09:08
o mahesh pts/1 2013-01-30 10:20 (:0)
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
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
▪ 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$
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$
100
7
111
➢ 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.
All file attributes such as file type, permissions, links, owner, group owner etc are kept in a
separate area of the hard disk, not directly accessible to humans, but only to kernel.
The UNIX has divided files into three categories:
o Ordinary file – also called as regular file. It contains only data as a stream of
characters.
o Directory file – it contains files and other sub-directories.
o Device file – all devices and peripherals are represented by files
➢ 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
b) Binary file – it contains both printable and unprintable characters that cover
entire ASCII range.
Examples: Most Unix commands, executable files, pictures, sound and
video files are binary.
Directory File –
▪ a directory contains no data but keeps some details of the files and
subdirectories that it contains.
▪ A directory file contains an entry for every file and subdirectories that it
houses.
▪ If you have 20 files in a directory, there will be 20 entries in the directory.
▪ Each entry has two components-
a) The filename
b) A unique identification number for the file or directory (called as inode
number).
Device File –
▪ All devices & peripherals are represented by files to read or to write a device.
▪ Installing software from CD-ROM, printing files and backing up data files to
tape.
▪ All of these activities are performed by reading or writing the file representing
the device.
▪ Advantage of device file is that some of the commands used to access an
ordinary file also work with device file.
▪ Device filenames are generally found in a single directory structure, /dev.
▪ 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.
SOCKET file – A socket file is special file that is used for network communication. A Unix
socket (or Inter-process communication socket) is a special file which allows for advanced
inter-process communication. A Unix Socket is used in a client-server application framework.
➢ 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.
▪ /dev: This contains references to all the CPU peripheral hardware, which are
represented as files with special properties. These are device drivers.
▪ /etc: This contains system configuration files.
▪ /home: This contains home directories of the common users.
▪ /lib: This contains library files, including files for all kinds of programs needed
by system and users.
▪ /sbin: This contains programs for use by the system and the system
administrator.
▪ /tmp: This contains temporary space for use by the system. This space is
cleaned upon reboot. So, don't use this for saving any work.
▪ /usr: This contains programs, libraries, documentation etc. for all user-related
programs.
▪ /var: This contains storage for all variable files and temporary files created by
users, such as:
→ log files
→ mail queue or
→ print spooler area
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:
bash-4.1$ pwd
/home/student
Like HOME it displays the absolute path.
1. cd: Changing The Current Directory:
▪ User can move around the UNIX file system using cd (change directory) command.
▪ When used with the argument, it changes the current directory to the directory
specified as argument.
▪ Example:
bash-4.1$ cd pcd
bash-4.1$ pwd
/home/student/pcd
▪ Here we are using the relative pathname of pcd directory. The same can be done
with the absolute pathname also. Example:
bash-4.1$ cd /home/student/unix
bash-4.1$ pwd
/home/student/unix
bash-4.1$ pwd
/home/student
bash-4.1$ cd .
bash-4.1$ pwd
/home/student
bash-4.1$ cd ..
bash-4.1$ pwd
/home
bash-4.1$ cd /bin
bash-4.1$ pwd
/bin
▪ cd can also be used without arguments:
bash-4.1$ cd pcd
bash-4.1$ pwd
/home/student/pcd
bash-4.1$ cd
/home/student
▪ The order of specifying arguments is important. You cannot delete parent directories
before deletion of subdirectories
▪ System refuses to delete a directory due to following reasons:
i. User doesn‘t have permission to delete directory. (i.e. write protected
directory).
ii. The directory doesn‘t exist in system.
iii. The directory is your present working directory.
▪ The dot (.) and double dots (..) notations in relative pathnames:
- User can move from working directory /home/kumar/progs/cprogs to home
directory /home/kumar using cd command like:
$pwd
/home/kumar/progs/cprogs
$cd /home/kumar
$pwd
/home/kumar
▪ 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
Dept of CSE, Vemana I.T Page 33 of 39
UNIX Programming(18CS56) Module-1 Introduction, Unix files
▪ To view the contents of the file named new we will also use the cat command as we
did in the first example:
$cat new
WELCOME TO UNIX // contents of F1
WELCOME TO PERL // contents of F2
▪ appending the contents ( >> ):
cat F3 new >> new1
cat new1
WELCOME TO SHELL // contents of F3
WELCOME TO UNIX // contents of F1
WELCOME TO PERL // contents of F2
▪ cat Options:
1) Displaying Non-printing Characters (-v):
- By default, without any option, this command displays only printing ASCII
characters of the file.
- -v option can be used to display even non-printing ASCII characters of the file.
2) Numbering Lines (-n):
- -n option can be used to number the lines of the file.
- This option helps the programmer in debugging programs.
3 F1
$ wc -c F1
15 F1
▪ 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