Top Banner
Unix programming Term:2008-2009 III B.Tech II semester Unit-I PPT Slides Text Books : (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming in unix environment by Stevens 1
46
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: UNIX unit-1

Unix programming

Term:2008-2009

III B.Tech II semester

Unit-I PPT Slides

Text Books: (1)unix the ultimate guide

by Sumitabha Das

(2)Advanced programming in unix environment by Stevens

1

Page 2: UNIX unit-1

INDEX

UNIT-I PPT SLIDES

Srl. No. Module as per Session planner Lecture No. PPT Slide No.

1. Introduction to Unix file system L1 1-8

2. vi editor L2 9-15

3. file handling utilities-cp, mv,ln L3 16-19

4. Rm, unlink, mkdir, rmdir, find L4 20-24

5. security by file permissions L5 25-30

6. process utilities L6 31-33

7 .disk utilities L7 34-38

8. ulimit , unmask L8 39-40

9. networking commands L9 4146

2

Page 3: UNIX unit-1

What is UNIX?

• A computer operating system. It is designed to be used by many people at the same time (multi-user).

• Runs on a variety of processors• It provides a number of facilities:

– management of hardware resources– directory and file system– loading / execution / suspension of programs

3

Page 4: UNIX unit-1

1.3. Why Use UNIX?

• multi-tasking / multi-user

• networking capability

• graphical (with command line)

• easy to program

• portable (PCs, mainframes, super-computers)

continued4

Page 5: UNIX unit-1

• File: s a container for storing information.

• A file is of 3 types.

• Ordinary file: It contains data as a stream of characters. It is of 2 types.

• Text file: contains printable characters.

• Binary file: contains both printable & non printable characters.

• Directory file: contains no data but it maintains some details of the files & subdirectories that it contains.

5

Page 6: UNIX unit-1

• Every directory entry contains 2 components.

• 1.file name.

• 2.a unique identification number for the file or directory.

• Device file: It represents the device or peripheral.

6

Page 7: UNIX unit-1

The UNIX File System

• A simplified UNIX directory/file system:

/

bin dev lib

User

usr tmp. . .. . .

User2

User1 User3

7

Page 8: UNIX unit-1

• /Bin: contains executable files for most of the unix commands.

• /Dev: contain files that control various input & output devices.

• /Lib: contains all the library functions in binary form.

• /Usr: contains several directories each associated with a particular user.

• /Tmp: contain the temporary files created by unix or by any user.

• /Etc: contains configuration files of the system.

8

Page 9: UNIX unit-1

vi Editor

• vi is a full screen text editor. It was created by Bill Joy.

• Bram Moolenaor improved it and called it vim (vi improved).

• Invoking vi:

• $Vi file name

9

Page 10: UNIX unit-1

Modes of operation• Vi has 3 mode of operation.

• 1.Command mode: In this mode all the keys pressed by the user are interpreted as commands. It may perform some actions like move cursor, save, delete text, quit vi, etc.2.Input/Insert mode: used for inserting text.– start by typing i; finish with ESC

10

Page 11: UNIX unit-1

Modes of operation

• Ex mode or last line mode:

• Used for giving commands at command line.

• The bottom line of vi is called the command line.

11

Page 12: UNIX unit-1

Basic Cursor Movements

h move cursor one place to left

j down one

k up one

l right one

w move forward one word

b back one word

12

Page 13: UNIX unit-1

Finishing a vi Session

• Get to command mode (press ESCs)

ZZ save changes to the file and quit(no RETURN)

:q! quit without saving(press RETURN)

:wq! Saves the file & quit.

13

Page 14: UNIX unit-1

Inserting Text

• Move to insertion point

• Switch to input mode: i

• Start typing; BACKSPACE or DELETE

for deletion

• ESC finish; back in command mode

No RURN

14

Page 15: UNIX unit-1

Deletion

• Must be in command mode.

x Delete character that cursor is on.

dd Delete current line.

D Delete from cursor position to end of line

u Undo last command

15

Page 16: UNIX unit-1

File handling utilities • Cp (Copying Files)

– To create an exact copy of a file you can use the “cp” command. The format of this command is:

cp [-option] source destinationEg:Cp file1 file2Here file1 is copied to file2.Eg:Cp file1 file2 dirFile1 file2 are copied to dir.

16

Page 17: UNIX unit-1

• Copying FilesCp turns to interactive when –i option is used &

destination file also exists. $cp -i file1 file2overwrite file2 (yes/no)?

Y at this prompt overwrites the file.

17

Page 18: UNIX unit-1

• mv (Moving and Renaming Files)Used to rename the files/directories. $mv test sample

Here test is renamed as sample.

18

Page 19: UNIX unit-1

• ln (link):

• Used to create links (both soft & hard links).

• It creates the alias & increase the link count by one.

• $ln file1 file2

• ln won’t work if the destination file also exists.

19

Page 20: UNIX unit-1

• Rm (Deleting Files and Directories)– To delete or remove a file, you use the “rm”

command. For example,

$rm my. listing

will delete “my.listing”.

With –i option removes the files interactively.

$rm –i file1

With –r option recursively removes directories.

$rm –r dir1

20

Page 21: UNIX unit-1

• mkdir: used to create one or more directories.

• $mkdir book

Creates the directory named book.

• $mkdir dbs doc dmc

Creates three directories.

21

Page 22: UNIX unit-1

• rmdir (remove directories ):

• Removes empty directories.

• $rmdir book

removes directory named book if it is empty.

• $rmdir dbs doc dmc

Removes 3 directories.

22

Page 23: UNIX unit-1

• find: It recursively examines a directory tree to look for matching some criteria and then takes some action on the selected files.

Syntax:

• find path_list selection_criteria action

• To locate all files named a. out use

$find / -name a. out –print

• ‘/’ indicates search should start from root directory.

23

Page 24: UNIX unit-1

• To locate all c files in current directory

$find . -name “*.c” –print

• To find all files begin with an uppercase letter use

$find . –name ‘[A-Z]*’ –print

Find operators:

Find uses 3 operators

!,-a ,-o

24

Page 25: UNIX unit-1

Security by file permissionsUnix follows a 3-tiered file protection system. owner’s permission other’s permission

- r w x r - x r - -

Type of file group’s permissions

Each group represents a category. There are 3 categories-owner ,group ,others.

25

Page 26: UNIX unit-1

Security by file permissions• Each category contains

read ,write ,execute permissions .

• rwx->presence of all permissions.

• r-x->absence of write permission

• r-- -> absence of write ,execute permission

• Chmod: changing file permission

chmod sets a file’s permissions (read, write and execute) for all three categories of users (owner, group and others)

26

Page 27: UNIX unit-1

• Syntax: chmod category operation permission file(s)

The command contains three components:

• category of a user (owner, group or others)

• operation to be performed (assign or remove a permission)

• Permission type (read, write or execute)

27

Page 28: UNIX unit-1

• Abbreviations used by chmod:

Category operation

u-user +-assign permission

g-group --remove permission

o-others =-assigns absolute permission

a-all

permissions

r-read permission

w-write permission

x-execute permission28

Page 29: UNIX unit-1

• Absolute assignment:

Absolute assignment by chmod is done with the = operator. Unlike the + or – operator s, it assigns only those permissions that are specified along with it and removes other permissions.

If u want to assign only read permission to all three categories and remove all other permissions from the file small use

chmod g-wx,o-x small

Or simply use = operator in any of the following ways. 29

Page 30: UNIX unit-1

chmod ugo=r small

chmod a=r small

Chmod =r small

The octal notation:

Chmod also takes a numeric argument that describes both the category and the permission. The notation uses octal numbers. Each permission is assigned a number like

read permission-4, write permission-2, execute permission-1

30

Page 31: UNIX unit-1

Process utilities

• Ps (process status):

• Display some process attributes.

• $ps

PID TTY TIME CMD

1078 pts/2 0:00 bash

• Ps presents a snapshot of the process table.

31

Page 32: UNIX unit-1

Process utilities

• Ps with –f option displays a fuller listing that includes the PPID.

• Ps with –u option followed by user-id displays the processes owned by the user-id.

• Ps with –e option displays the system processes.

32

Page 33: UNIX unit-1

• Who: know the users

• Displays the users currently logged in the system.

• $who

• Whoami: Show you the owner of this account

• $whoami

• W: Tell you who is logging in and doing what!

• $w33

Page 34: UNIX unit-1

• Finger: Displays the information about the users.

• $finger user

Find out the personal information of a user

• $finger name

Try to find the person’s info. by his/her name

• finger email-address

Try to find the person’s info across the network

34

Page 35: UNIX unit-1

Disk utilities

• Du: disk usage

• Du estimate the file space usage on the disk.

• It produces a list containing the usage of each subdirectory of its argument and finally produces a summary.

• $du /home/usr1

35

Page 36: UNIX unit-1

Disk utilities

• Df: displays the amount of free space available on the disk. The output displays for each file system separately.

• $df

• Mount:

• Used to mount the file systems.

• Takes 2 arguments-device name ,mount point.

36

Page 37: UNIX unit-1

Disk utilities• Mount uses an option to specify the type

of file system.

• To mount a file system on the /oracle directory on Linux system use

$mount –t ext2 /dev/hda3 /oracle

$mount –t iso9660 /dev/cdrom /mnt /cdrom

$mount –t vfat /dev/hda1 /msdos

$mount –t msdos /dev/fd0 /floppy

37

Page 38: UNIX unit-1

Disk utilities

• Umount: unmounting file systems

• Unmounting is achieved with the umount command. which requires either file system name or the mount point as argument.

• $umount /oracle

• $umount /dev/hda3

• Unmounting a file system is not possible if the file is opened.

38

Page 39: UNIX unit-1

• ulimit: user limit• It contains a value which signifies the

largest file that can be created by the user in the file system.

• When used by itself it displays the current setting.

• $ulimit unlimitedUser can also set the ulimit value by using $ulimit 10

39

Page 40: UNIX unit-1

unmask:

When u create files and directories, the default permissions that are assigned to them depend on the system’s default setting. Actually this default is transformed

By subtracting the user mask from it to remove one or more permissions. This value is evaluated by umask without arguments.

$umask

02240

Page 41: UNIX unit-1

Networking commands• ftp: file transfer protocol

• ftp is used to transfer files. It can be used

with host name.

$ftp Saturn

Connected to Saturn

220 Saturn ftp server

Name (Saturn: summit ): Henry

Password: ******

41

Page 42: UNIX unit-1

• To quit ftp use close and then bye or quit.• ftp>close 221 good bye• ftp>bye• Transferring files: Files can be of 2 types.• Uploading( put & mput):• To upload ur web pages & graphic files to

website.The put command sends a single file to the

remote machine.42

Page 43: UNIX unit-1

• ftp>binary 200 type set to I• ftp>put penguin. gif To copy multiple files use mput.• ftp>mput t*.sql• Downloading files: get & mget• To download the files from remote

machine use get & mget.• ftp>get ls-lR.gz• ftp>_

43

Page 44: UNIX unit-1

Networking commands

telnet: Remote login

If u have an account on the host in a local network (or on internet ),u can use this with the host name or the ip address as argument.

$telnet Saturn

Trying to 192.168.0.1…

Connected to Saturn

44

Page 45: UNIX unit-1

• Login:----• Password:-----• U can quit telnet by using exit command.• telnet prompt:When telnet used without Ip address the

system displays a telnet> prompt . U can invoke a login session from here with open.

telnet> open 192.168.0.8Trying to 192.168.0.8…Connected to 192.168.0.8

45

Page 46: UNIX unit-1

Networking commands• rlogin: remote login without password• rlogin is the Berkley's implementation of the

remote login facility.• U can log on to ur own identical remote account

without using either the user name or password.• $rlogin Jupiter• Last login :….• rlogin is terminated with ctrl+d or exit or logout.

46