Chapter 2 Introduction To UNIX Concepts
Chapter 2 Introduction To UNIX Concepts
Introduction To Unix
Concepts
By-RAHUL SONAWANE CA - CCTP-5 OPERATING SYSTEMS
0
Chapter 2 Introduction to UNIX concepts.
1. Introduction to Unix Operating System.
2. Features of Unix.
3. Architecture of the Unix Operating System.
4. Introduction to File System and Process Environment.
5. Working with Unix
• The login prompt.
• General features of Unix commands/ command structure.
• Command arguments and options.
• Understanding of some basic commands such as echo, printf, ls, who, date, passwd,
cal, Combining commands.
1. Introduction
• UNIX is an operating system.
• An operating system is a set of programs that act as a link between the computer and the user.
• The operating system (OS) manages the resources of a computer.
• Examples of computer resources are: CPU, RAM, disk memory, printers, displays, keyboard,
etc.
2. Features of Unix
1) Multiuser
• UNIX is a multiprogramming system.
• Multiple users can access the system by connecting to points known as terminals.
• Several users can run multiple programs simultaneously on one system.
CA - CCTP-5-Operating Systems 1
2) Multitasking
• UNIX is a multitasking system.
• A single user can also run multiple tasks concurrently.
• For example:
At the same time, a user can
→ edit a file
→ print another file one on the printer
→ send email to a friend and
→ browse www
• 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.
3) Pattern Matching
• UNIX has very sophisticated pattern matching features.
• Regular Expressions are a feature of UNIX.
• Regular Expressions describe a pattern to match, a sequence of characters, not words, within a
line of text.
4) Portable
• UNIX can be installed on many hardware platforms.
• Unix operating system is written in C language; hence it is more portable than other operating
systems.
5) UNIX Toolkit
• 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
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.
CA - CCTP-5-Operating Systems 2
3. Unix Architecture (Components)
• The UNIX operating system (OS) consists of a kernel layer, a shell layer, an application layer
& file.
Kernel
• The kernel is the heart of the operating system.
• It interacts with the machine’s hardware.
• It is a collection of routines written in C.
• It is loaded into memory when the system is booted.
• Main responsibilities:
1) Memory management
2) Process management (using commands: kill, ps, nohup)
3) File management (using commands: rm, cat, ls, rmdir, mkdir)
• To access the hardware, user programs use the services of the kernel via system calls.
System Call
• A system call is a request for the operating system to do something on behalf of the user's
program.
• The system calls are functions used in the kernel itself.
• To the programmer, the system call appears as a normal C function call.
• UNIX system calls are used to manage the file system and control processes.
Example: read(), open(), close(), fork(), exec(), exit()
• There can be only one kernel running on the system.
Shell
• The shell interacts with the user.
• The shell is a command line interpreter (CLI).
• Main responsibilities:
1) interprets the commands the user types in and
CA - CCTP-5-Operating Systems 3
2) dispatches the command to the kernel for execution
• There can be several shells in action, one for each user who’s logged in.
• There are multiple shells that are used by the UNIX OS.
• For example: Bourne shell (sh), the C shell (csh), the Korn shell (ksh) and Bourne Again shell
(bash).
Application
• This layer includes the commands, word processors, graphic programs and database
management programs.
File
• A file is an array of bytes that stores information.
• All the data of Unix is organized into files.
• All files are then organized into directories.
• These directories are further organized into a tree-like structure called the filesystem.
CA - CCTP-5-Operating Systems 4
• From a user perspective in a Unix system, everything is treated as a file. Even such devices
such as printers and disk drives.
• How can this be, you ask? Since all data is essentially a stream of bytes, each device can be
viewed logically as a file.
• In Unix, there are three basic types of files −
• Ordinary Files − An ordinary file is a file on the system that contains data, text, or program
instructions. In this tutorial, you look at working with ordinary files.
• Directories − Directories store both special and ordinary files. For users familiar with
Windows or Mac OS, Unix directories are equivalent to folders.
• Special Files − Some special files provide access to hardware such as hard drives, CD-ROM
drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts
and enable you to access a single file using different names.
• Examples of files:
• a document (report, essay etc.)
• the text of a program written in some high-level programming language
• instructions comprehensible directly to the machine and incomprehensible to a casual user,
for example, a collection of binary digits (an executable or binary file);
• a directory, containing information about its contents, which may be a mixture of other
directories (subdirectories) and ordinary files.
CA - CCTP-5-Operating Systems 5
• Whenever a command is issued in Unix/Linux, it creates/starts a new process. For example,
pwd when issued which is used to list the current directory location the user is in, a process
starts.
• Through a 5 digit ID number Unix/Linux keeps an account of the processes, this number
is call process ID or PID. Each process in the system has a unique PID.
• Used up pid’s can be used in again for a newer process since all the possible combinations
are used.
• At any point of time, no two processes with the same pid exist in the system because it is
the pid that Unix uses to track each process.
• Whenever you issue a command in Unix, it creates, or starts, a new process. When you tried
out the ls command to list the directory contents, you started a process. A process, in simple
terms, is an instance of a running program.
• The operating system tracks processes through a five-digit ID number known as the pid or the
process ID. Each process in the system has a unique pid.
• Pids eventually repeat because all the possible numbers are used up and the next pid rolls or
starts over. At any point of time, no two processes with the same pid exist in the system because
it is the pid that Unix uses to track each process.
5.2 Options
• An option is preceded by a minus sign (-) to distinguish it from filenames.
CA - CCTP-5-Operating Systems 6
• Example:
$ ls –l // -l option list all the attributes of the file note
• There must not be any whitespaces between – and l.
• Options are also arguments, but given a special name because they are predetermined.
• Options can be normally combined with only one – sign.
thus $ ls –l –a –t is same as $ ls –lat
• Because UNIX was developed by people who had their own ideas as to what options should
look like, there will be variations in the options.
• Some commands use + as an option prefix instead of -.
• 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.
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.
• For Example:
The ls command can run
→ without arguments (ls)
→ with only options (ls –l)
→ with only filenames (ls f1 f2), or
→ using a combination of both (ls –l f1 f2).
• There are some commands compulsorily take options (cut).
• There are some commands which can take an expression as an argument, or a set of
instructions as argument. Ex: grep, sed
CA - CCTP-5-Operating Systems 7
"\b‟ Back Space
"\f‟ Form Feed
"\n‟ New Line
"\r‟ Carriage Return
"\t‟ Horizontal Tab
"\v‟ Vertical Tab
"\\‟ Backslash
2. printf
• This command can be used to display a message on the terminal.
• This command can be used with following escape characters:
"\a" Audible Alert (Bell)
"\b‟ Back Space
"\f‟ Form Feed
"\n‟ New Line
"\r‟ Carriage Return
"\t‟ Horizontal Tab
"\v‟ Vertical Tab
"\\‟ Backslash
• Example:
$ printf "Welcome to UNIX \n"
Welcome to UNIX
• Similar to C language, this command can be used with following format specifiers:
%d Decimal integer
%f Floating point number
%s String
%o Octal integer (base 8)
%x Hexadecimal integer (base 16)
• Syntax:
printf("format-string", variable-list);
where format-string contains one or more format-specifiers variable-list contains names of
variables
• Example:
$ printf "My current shell is %s\n" $SHELL
My current shell is /bin/ksh
3. ls
• ls command can be used to obtain a list of all filenames in the current directory.
• -l option can be used to obtain a detailed list of attributes of all files in the current directory.
• Example:
$ ls -l
Type & Perm Link Owner Group Size Date & Time File Name
-rwxr-xr-- 1 kumar metal 195 may 10 13:45 chap01
drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir
4. who
• This command can be used to display the details of all the users logged-in into the unix system
at the same time.
CA - CCTP-5-Operating Systems 8
• Example:
$ who
Kumar tty0 Oct 8 14:10
5. date
• This command can be used to display the current date and time.
• Example:
$ date
Mon Sep 4 16:40:02 IST 2021
• This command can also be used with suitable format specifiers as arguments.
• Following are some format specifiers:
d – day of month (1 - 31)
m - Month (01-12)
y – last two digits of the year.
H– hour (00-24)
M – minute (00-59)
S – second (00-59)
D – date in the format mm/dd/yy
T – time in the format hh:mm:ss
• Syntax:
$ date +%format_specifier
• Example:
$ date +"%d-%m-%y"
04-09-21
$ date +%m // displays month number
09
6. passwd
• This command can be used to change user password.
• All Unix systems require passwords to help ensure that your files and data remain secure from
hackers.
CA - CCTP-5-Operating Systems 9
• Following are the steps to change your password –
1) To start, type password at the command prompt as shown below.
2) Enter your old password, the one you're currently using.
3) Type in your new password.
4) You must verify the password by typing it again.
$ passwd
Changing password for
kumar (current) Unix
password: ***** New
Unix password: *****
Retype new Unix
password: *****
passwd: all authentication tokens updated successfully
$
7. cal
• This command can be used to display the calendar of the current month.
• Syntax:
cal [ [ month] year ]
• Example:
$ cal
September 2017
Su Mo Tu We Th Fr Sa
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
• This command can also be used to display the calendar of any specific month or a complete
year.
• Example:
$ cal 9 2017
September 2017
Su Mo Tu We Th Fr Sa
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
• You can't hold the calendar of a year in a single screen page; it scrolls off rapidly to end of
the year.
• To pause at each screen page, a pipe "|" can be connected to more pager:
• Example:
$cal 2017 | more
CA - CCTP-5-Operating Systems 10
• A command can often be entered in more than one way.
• Shell allows the following type of command usage:
1) Combining Commands.
2) A command line can overflow or Be split into multiple lines.
3) Entering a command before previous command has finished.
1) Combining Commands
• UNIX allows you to specify more than one command in the single command line.
• Example:
$ wc sample.txt ; ls –l sample.txt 2 3 //Two commands separated by ; (semicolon).
16 sample.txt
CA - CCTP-5-Operating Systems 11