You are on page 1of 46

UNIX Day 1

Intermediate Level

Learning approach
The following are strongly suggested for a better learning and understanding of this course:
Noting down the key concepts in the class, explained by the educator Analyze all the examples / code snippets provided Study and understand the self study topics Completion and submission of all the assignments, on time Completion of the self review questions in the lab guide Study and understand all the artifacts including the reference materials / elearning / supplementary materials specified Completion of the project (if applicable for this course) on time inclusive of individual and group activities Taking part in the self assessment activities Participation in the doubt clearing sessions

Course Objectives
To introduce the Unix Operating System To develop an ability to use Unix commands To develop an ability to use Unix utilities To introduce the security features in Unix To introduce the important features of vi editor To develop an ability to write programs in shell

Course Pre-requisites
For better understanding of this module, you should be conversant with Operating System Concepts and Programming Basics

Unix in Industry
Organizations use different implementations of Unix for creating and deploying applications as Unix is a very popular Operating System Many implementations use databases which are set-up on Unix Servers Many web based projects are deployed on Unix Web Servers like Apache are commonly deployed on Unix platforms With the advent of Mono, Unix can also cater to DotNet applications Hence Unix is considered to be very important in the industry In Infosys, there are many projects which use Unix in its various flavors and require good understanding of Unix for its team
5

C-Map

Expectations
At the end of the Unix module, the candidate should be conversant with the following
Unix architecture Usage of various types of Unix commands Security features in Unix Unix utilities vi editor Shell programming

References
Sumitadha Das, Unix Concepts and Applications ,3 rd Edition,TMH Yashavant P. Kanetkar, Unix Shell Programming,BPB Brain W. Kernighan and Rob Pike, The UNIX Programming Environment Stephen Prata, Advanced UNIX - A Programmers Guide. Sumitadha Das,, Your Unix: the ultimate guide.,TMH Stephen G. Kochan and Patrick H. Wood, Exploring the Unix System James R. Groff, Paul N. Weinberg , Understanding Unix - A conceptual guide Maurice J. Bach, The Design of the Unix Operating System
8

References
Introduction to Unix Shell Programming
http://kshop/kshop/showdoc.asp?DocNo=8011

Unix Regular Expressions


http://kshop/kshop/showdoc.asp?DocNo=95641 http://kshop/kshop/showdoc.asp?DocNo=72231

Unix Command Tips


http://kshop/kshop/showdoc.asp?DocNo=63432

Unix Utilities and Programming Tool


http://kshop/kshop/showdoc.asp?DocNo=162572

UNIX-Shell Programming
http://kshop/kshop/showdoc.asp?DocNo=162573
9

Session Plan Day 1


Introduction to Unix Unix System Architecture Basic Unix Commands File and directory related commands Shell meta characters Standard files Redirection and Pipe

10

Introduction to Unix
What is Unix?
Unix is a multitasking, multi-user, time-sharing operating system

Evolution of Unix
In 1965 AT&T along with a group of other companies started working to create a time-sharing system, MULTICS In 1969 AT&T has dropped the idea of MULTICS project. Ken Thompson has written first version of UNICS on a PDP-7 machine. UNICS stands for Uniplexed Information and Computing Services. UNICS is changed to UNIX.

11

Introduction to Unix
Flavors of Unix
AIX (Advanced IBM Unix) HP-UX ( Hewlett Packard Unix) MINIX (Minimal Unix) XENIX SCO UNIX SOLARIS LINUX, etc

12

Features Of Unix
Portability
In 1973, Unix kernel is written in C, hence it can be installed on machines from microcomputers to mainframe computers

Security
Proper username and password are required to work on Unix Access permissions are given to prevent unauthorized access of files

13

Features Of Unix
Redirection Tools
Used to redirected data between files

Pipes
Work with many commands at the same time

Background Processing
Jobs/Tasks can be executed in the background with minimal interaction from the user

Stable and Reliable


Less prone to crashes

14

Features Of Unix
Communication
Unix has commands through which communication between different users connected to the system is possible

Hierarchical File System


Store everything in a tree structure, so easy to manage everything

Shells
Unix has different types of shells like Bourne Shell(sh), Bourn Again Shell(bash), Korn Shell(ksh) etc

15

System Architecture
Major components of Unix are :
Kernel
Acts like a resource manager Allocates resources among its users in an optimal manner

Shell
Provides a processing environment for the user programs and acts like a command translator

Utilities
Programs which are used for development purposes

User Applications
Programs written by the user
16

System Architecture
User
SHELL
OTHER APPLICATIONS

User
SHELL
UNIX

COMMANDS
HARDWARE

DATABASE
PACKAGES

KERNEL

COMPILERS
SHELL

SHELL

User

User

17

UNIX File system


UNIX has multiple file systems A file system
is a formatted partition of the disk Is a group of files having relevant information

There is always one compulsory file system known as the root

18

File System in UNIX


Boot block
Occupies the beginning of the root file system

Super block
Has the state of the file system-its size, status of the free space on the file system, how many files it can store etc

Inode block
It follows the super block-Gives the internal representation of the file

Data Block
Contains data.(Size of blocks can vary from 512 bytes to 4K)

19

Inode pointer structure


Data Block Direct 0

Direct 1

Data Block

. . .

Data Block

Direct 9 Single Indirect Double Indirect Data Block Triple Indirect Array of 13 pointers Index Block Index Block Index Block Data Block

Data Block

Index Block

Index Block

Index Block

20

Hierarchy of Unix File System


/ (root)

home bin sh

dev ls tty lp

etc

tmp

lib

usr

passwd shadow

Note: Different variants of UNIX differ slightly in the file system structure. But some of the basic folders are still the same.

21

Hierarchy of Unix File System


The Absolute Pathname. The complete pathname starting from root(/). Example: /home/training/vaniv

The Relative Pathname. The pathname relative to current working directory. Example: training/vaniv.
Note : .. . : Parent Directory : Current Working Directory
22

Can you answer these questions?


Name any 3 flavors of Unix Explain the properties of Unix OS Explain the components of Unix OS Describe the volume structure of Unix Assume
block size of 512 bytes & every address requires 4 bytes The inode structure contains ten direct pointers, one single indirect, one double indirect & one triple indirect pointer What could be the possible maximum file size for this system?
23

Unix Command Syntax


Unix Command Syntax
command [options] [arguments]

Notes: 1. Option is generally preceded by (hyphen) symbol 2. More than one option can be used together

24

Types of Commands
Internal Commands
These are shell built-ins, not stored as separate files The shell does not generate a process for these commands and executes them directly. Examples: cd, echo

External Commands
These are stored as files having an independent existence The shell creates a process for these commands Examples: cat, ls

Shell Scripts
The shell executes these scripts by spawning another shell The child shell becomes the parent of the commands that feature in the script The child shell executes the commands in the script

25

Categories of Commands/Utilities
General Commands

File and Directory related Commands


Text Processing Commands

Process and Memory related Commands


Communication Commands

Compression Utilities
Find Utility
26

General UNIX Commands


Command
clear date cal

Description
Clears the terminal screen Displays system date and time Displays calendar Displays the details of the users currently logged in Tells about the type of the command Used to create alias of an existing command Prints current working directory
27

Syntax
$ clear $ date [+Format] $ cal [arguments]

who
type alias pwd

$ who [arguments]
$ type cmd_name $ alias [alias_name=cmd_name] $ pwd

Self Study: General UNIX Commands

28

man command
A screenshot of the man command.
Syntax: man <command name>
[ ] indicates optional content

Type the letter q to quit 29

File and Directory related Commands (1/7)


Command Description Creates an empty file if file doesnt exist Changes file timestamps if the file exists Create file and save the content in file Display file(s) content Append the content in file Displays the type of file file perusal filter for page-wise display Syntax

touch

$ touch file1 [file2 ] $ cat > file_name $ cat file_name $ cat >> file_name $ file file_name/dir_name $ more file_name

cat file more

30

File and Directory related Commands (2/7)


Command mkdir cd Description Used to create directories change directory Syntax $ mkdir dir_name $ mkdir p d1/d2/d3 $ cd dir_name

rmdir

removes directory if it is empty

$ rmdir dir_name

31

File and Directory related Commands (3/7)


Command ls Variations List including hidden files List in long format List recursively List with inode values Description List files and directories of target directory Syntax $ ls [options] dir/file_name

Syntax $ ls a dir/file_name $ ls l dir/file_name $ ls R dir/file_name $ ls i dir/file_name

32

File and Directory related Commands (4/7)


Command cp Variations Create copy of file1 with name file2 in current directory Create copy of source file(s) with same name in target directory If dir2 is not present: Create dir2 in current directory Copy content of dir1 in dir2 if dir2 is present: Copy sub-tree structure rooted at dir1 in dir2
33

Description Copy source file to target location

Syntax $ cp source target

Syntax $ cp file1 file2 $ cp file1 [file2] dir_name $ cp r dir1 dir2

$ cp r dir1 dir2

File and Directory related Commands (5/7)


Command Description Rename source file/dir name to target name Move source file/dir to target dir Syntax

mv

$ mv source target

Variations

Syntax $ mv file1 [file2] dir_name

Rename source file name with target name $ mv file1 file2 Move source file(s) in target directory If dir2 is not present: Rename dir1 with name dir2 if dir2 is present: Move sub-tree structure rooted at dir1 in dir2

$ mv dir1 dir2
$ mv dir1 dir2

34

File and Directory related Commands (6/7)


Command rm Variations Remove all source files Remove dir1 recursively Remove interactively Description Remove file(s)/directories Syntax $ rm file_name/dir_name Syntax $ rm file1 [file2 ] $ rm r dir1 $ rm i file_name

35

File and Directory related Commands (7/7)


Command Description Makes hard links between files ln Makes soft(or symbolic) links between files Syntax $ ln source target $ ln s source target

S No. Hard Link 1 Inode number is same for the original and link file Link will remain valid even if we are deleting the original file Cannot be established for directories For every hard link created, value of the link field is incremented by 1 for original as well as link file
36

Soft link Inode number is different for the original and link file Link will not remain valid if we are deleting the original file Can be established for directories The link field value would be 1 for link file and no change in link field value of original file

2
3 4

Self Study: File and Directory related Commands

37

The Shell Meta Characters.


? Used to match any one character in file name * - Used to match zero or more characters in file name [xyz] Used to match any one character either x, y or z. [!xyz] Used to match any one character which is not x, y or z. [x-z] Used to match any one character which is within the range of x to z.
38

Concept of stdin, stdout and stderr

stdout stdin A Computer program running stderr

Keyboard

stdin

Another Computer program running

stdout Monitor

stderr

Operating System

39

Standard Files
Standard Input (0)
This file is opened by shell to accept data

Standard Output (1)


This file is opened by shell to redirect data

Standard Error (2)


This file is opened by shell for writing error messages

40

Building Block Primitives


Redirection of I/O:
Input can be supplied from either from keyboard or a file to a process Output of a process can be redirected to either display screen or a file

Pipes
Output of one program/command is piped as input to another program/command.

41

Special Files
/dev/tty
It is used to display on the terminal

/dev/null
This file is used to suppress the output from being displayed

Used for suppressing error messages from being


displayed on the screen in programs

42

Self Study: Standard Files, Redirection and Pipe

43

Can you answer these questions?


Give two ways to find out whether UNIX is file or directory What is difference between file and type command? How does touch command differ from cat command? Give the situations in which mv command will work as rename How does cp command differ from mv command? How does rm command differ from rmdir command? What is difference between hard link and soft link? What is difference between redirection and pipe?
44

Summary
Features of Unix Unix System Architecture General Unix commands File and directory related commands Shell meta characters Standard files Redirection and Pipe
45

Thank You

46

You might also like