SysProg-Tutor 02 Introduction to Unix Operating System

Post on 25-May-2015

799 views 1 download

Tags:

Transcript of SysProg-Tutor 02 Introduction to Unix Operating System

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Introduction to UNIX Operating System

Tutor Session II:

Wongyos Keardsri (P’Bank)Department of Computer EngineeringFaculty of Engineering, Chulalongkorn UniversityBangkok, Thailand

Mobile Phone: 089-5993490E-mail: wongyos@gmail.com, MSN: bankberrer@hotmail.comTwitter: @wongyos

2

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Tutor Outline

History of UNIXUnix Structure

UNIX ShellUNIX File System

Getting StartedLogging in / ExitingEnvironment Variables

Unix Command Line Structure

System Resource Commands

Directory Navigation and ControlFile Maintenance CommandsDisplay Commands

Special Unix FeaturesText ProcessingOther Useful Commandsvi Editor

3

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

History of UNIX

Unix (officially trademarked as UNIX®) is a computer operating systemIn 1965, Bell Laboratories joins with MIT and General Electric developed the new operating system named MulticsIn 1969, Some of the Bell Labs programmersdesigned and implemented the first version of the Unix File System on a PDP-7Given the name UNIX by Brian Kernighan

4

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

History of UNIX

UNIX developer team (Bell Labs programmers)Ken ThompsonDennis RitchieRudd CanadayDoug McIlroy

Languages written in UNIXBCPL (Multics)B (Unix) Unix version 1.0C (Unix) After Unix version 2.0

(Cont)

5

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Unix Structure

Unix featuresMulti-user and time-sharingMulti-taskingMulti-level (Hierarchical file system)Multi-processorSimple user interfaceSimple utilities

Free Unix-like operating systemsLinuxBSD

6

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Unix Structure

Several hundred utility programsUser commandsThe Unix system kernel and The shellsSystem callsC library functionsDevice & network interfaceFile formatGames and demosSystem maintenance

(Cont)

7

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

System Calls

Kernel - controls hardware- handles input/output- manages file system- manages memory- schedules processes

Shell - accepts and executes user commands [command interpreter]

Hardware

Programs=Commands

Unix System Structure

(Cont)

Unix Structure

8

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

UNIX shellBourne shell (sh) -- Written by Steve Bourne, while at Bell Labs. First distributed with Version 7 Unix.Bourne-Again shell (bash) -- Written as part of the GNU project to provide a superset of Bourne Shell functionality. Korn shell (ksh) -- Written by David Korn, while at Bell Labs. Z shell (zsh) -- considered as the most complete shell C shell (csh) -- Written by Bill Joy, while at the University of California, Berkeley. First distributed with BSD, circa 1979.

(Cont)

Unix Structure

9

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

(Cont)

Unix Structure

Unix File System

/

bin etc lib usr homedev tmp

sh date ls passwd group libc.so

bin

tty nullbank u50skp

bin manlocal

Root

hello.c cpcuHierarchical file system

10

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Unix Structure

Directory: etc, usr, home, bank, cpcuFile: libc.so, hello.cPath: /home/bank/cpcuTwo special directory entries

. the current directory

.. the parent of the current directory

(Cont)

11

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Getting Started

Logging inAfter connecting with a Unix system, a user is prompted for a login username, then a password.Unix is case sensitive should be typed exactly as issued

Example

login: rootpassword:

12

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Getting Started

Exiting^D indicates end of data stream; can log a

user off.^C interruptlogout leave the systemexit leave the shell

Terminal Typevt100 Most computerssun Sun workstationxterms or xterm X-Terminal

(Cont)

13

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Getting Started

Environment VariablesDISPLAY The graphical display to use, e.g.nyssa:0.0EDITOR The path to your default editor, e.g.

/usr/bin/viGROUP Your login group, e.g. staffHOME Path to your home directory, e.g.

/home/frankHOST The hostname of your system, e.g. nyssaLOGNAME The name you login with, e.g. frankPATH Paths to be searched for commands,

e.g. /usr/bin:/usr/ucb:/usr/local/bin

(Cont)

14

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Getting Started

Environment Variables (Cont)PS1 The primary prompt string, Bourne

shell only (defaults $)PS2 The secondary prompt string, Bourne

shell only (defaults >)SHELL The login shell you’re using, e.g.

/usr/bin/cshTERM Your terminal type, e.g. xtermUSER Your username, e.g. frank

(Cont)

15

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Getting Started

Set a global environment variableC shell:% setenv NAME value

Bourne shell:$ NAME=value; export NAME

List global environmental variables with the envor printenv commands.Unset with the unsetenv (C shell) or unset(Bourne shell) commands.

(Cont)

16

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Set a local shell variableC shell:

% set name=valueBourne shell:

$ name=valueDisplay environment variables

echo $NAME

Getting Started(Cont)

Change the shell$ /bin/sh$ /bin/csh$ /bin/ksh$ /usr/pkg/bin/bash

17

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

C Shell History Substitutionhistory recall previous commands!! repeat last command!n repeat command number n!-n repeat command n from last!str repeat command that started with

string str!?str? repeat command with str anywhere on

the line!?str?% select the first argument that had str in it

Getting Started(Cont)

18

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Control Key^S pause display^Q restart display^C cancel operation^U cancel line^D signal end of file^V treat following control character as

normal character

Getting Started(Cont)

19

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Unix Command Line Structure

Unix Command Line Structure

Unix manual / Help$ man command_name$ man -k keyword$ man -k “key words”

$ command [options] [arguments]

20

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

System Resource Commandsdate report the current date and timecal report the calendardf report the summary of disk blocks and inodes

free and in usedu report amount of disk space in usehostname/uname display or set (super-user only)

the name of the current machinekill send a signal to the process with the process

id number (pid#) or job control number (%n). The default signal is to kill the process.

Unix Command Line Structure(Cont)

21

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

System Resource Commands (Cont)passwd set or change your passwordps show status of active processesscript saves everything that appears on the

screen to file until exit is executedstty set or display terminal control optionswhereis report the binary, source, and man

page locations for the command named

which reports the path to the command or the shell alias in use

Unix Command Line Structure(Cont)

22

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

System Resource Commands (Cont)who or w report who is logged in and what

processes are runningwhoami report current user namefinger report who is logged inifconfig report network interfacessu switch userwrite send a message to another user

Unix Command Line Structure(Cont)

23

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Directory Navigation and Controlcd [directory]change directoryls [options] [directory or file]list directory contents or file permissionsmkdir [options] directorymake a directorypwdprint working (current) directoryrmdir [options] directoryremove a directory

Unix Command Line Structure(Cont)

24

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

List file and directory contents

drwxr-xr-x 3 root wheel 512 Jul 4 23:48 2110101drwxr-xr-x 4 root wheel 512 Aug 28 2005 2110313drwxr-xr-x 3 root wheel 512 Aug 28 2005 Baka-rw-r--r-- 1 root wheel 905 Jul 9 02:09 file-rw-r--r-- 1 root wheel 105 Jul 9 00:05 hello.java

Permission mode

Number of link

Owner FilenameSize Modified/Createddate

Group

Type field

Unix Command Line Structure(Cont)

25

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

PermissionType field (first character)Access permissions (characters 2-10):

• First 3 : user/owner• Second 3: group• Last 3: others

Permission mode• d directory• r read permission• w write permission• x execute permission• - no permission

Unix Command Line Structure(Cont)

drwxr-xr-x

26

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Permission number7 111 rwx6 110 rw-5 101 r-x4 100 r--3 011 -wx2 010 -w-1 001 --x0 000 ---

Unix Command Line Structure(Cont)

27

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

File Maintenance Commandschgrp [options] group filechange the group of the filechmod [options] filechange file or directory access permissionschown [options] owner filechange the ownership of a file; can only be done by the superusercp [options] file1 file2copy file1 into file2; file2 shouldn't already exist. This command creates or overwrites file2.

Unix Command Line Structure(Cont)

28

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

File Maintenance Commands (Cont)mv [options] file1 file2move file1 into file2rm [options] fileremove (delete) a file or directory (-r recursively deletes the directory and its contents) (-i prompts before removing files)

Unix Command Line Structure(Cont)

29

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Display Commandscat [options] fileconcatenate (list) a fileecho [text string]echo the text string to stdouthead [-number] filedisplay the first 10 (or number of) lines of a filemore (or less or pg ) [options] filepage through a text filetail [options] filedisplay the last few lines (or parts) of a file

Unix Command Line Structure(Cont)

30

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Special Unix Features

I/O redirection and pipingOutput redirection to a fileInput redirection from a filePipingOutput of one command becomes the input of a subsequent command

There are 3 standard file descriptors:stdin 0 Standard input to the programstdout 1 Standard output from the programstderr 2 Standard error output from the program

31

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

File Redirection> redirect standard output to file

command > outputfile>> append standard output to file

command >> outputfile< input redirection from file

command < inputfile| pipe output to another command

command1 | command2

Special Unix Features(Cont)

32

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

File Redirection (csh)>& file redirect stdout and stderr to file>>& file append stdout and stderr to file|& command pipe stdout and stderr to

command

To redirect stdout and stderr to separate files:% (command > outfile) >& errfile

Special Unix Features(Cont)

33

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

File Redirection (sh)2> file redirect stderr to file> file 2>&1 direct both stdout and stderr to file>> file 2>&1 append both stdout and stderr to file2>&1|command pipe stdout and stderr to command

To redirect stdout and stderr to two separate files:$command > outfile 2> errfile

To discard stderr:$ command 2> /dev/null

(/dev/null is a black hole for bits)

Special Unix Features(Cont)

34

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Wild Cards? match any single character* match any string of zero or more characters[abc] match anyone of the enclosed characters[a-s] match any character in the range a through s[!def] (sh) match any characters not one of[^def] (csh) the enclosed characters

Special Unix Features(Cont)

35

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Control Over Command ExecutionBackground (non-interactive)command &

Sequentialcommand1 ; command2

Subprocess (run as an atomic unit)(command)

Special Unix Features(Cont)

36

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Control Over Command Execution (Cont)Conditional (program exit status determines success or failure)

• OR command1 || command2

– (run command2 if command1 fails)• AND command1 && command2

– (run command2 if command1 succeeds)

Special Unix Features(Cont)

37

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Text Processing

FilterA filter (in Unix) is a program that reads some input,performs a simple translation on it, and writes some output

Examples of Unix filtersgrepsortwcawkvised

38

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Text Processing Commandsawk [options] filescan for patterns in a file and process the resultsgrep [options] 'search string' filesearch the argument (in this case probably a file) for all occurrences of the search string, and list them.sed [options] filestream editor for editing files from a script or from the command line

Text Processing(Cont)

39

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Text Processing

Regular expressionsRegular expressions come in three different forms

• Anchor tie the pattern to a location on the line• Character sets match a single character at a single position• Modifiers specify how many times to repeat the previous

expression

Regular expressions can be combined to form longer regular expressions

(Cont)

40

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Regular expressions (Cont). match any single character except newline* match zero or more instances of single

expression preceding it[abc] match any of the characters enclosed[a-d] match any character in enclosed range[^abc]match any character NOT in the enclosed set^exp regular expression must start at the beginning

of the line

Text Processing(Cont)

41

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Regular expressions (Cont)exp$ regular expression must end at the beginning

of the line\ treat the next character literally

Text Processing(Cont)

42

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

Working With Filescmp [options] file1 file2compare two files and list where differences occur (text or binary files)cut [options] [file(s)]cut specified field(s)/character(s) from lines in file(s)diff [options] file1 file2compare the two files and display the differences (text files only)file [options] fileclassify the file type

43

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

Working With Files (Cont)find directory [options] [actions]find files matching a type or patternln [options] source_filetarget link the source_file to the targetpaste [options] filepaste field(s) onto the lines in filesort [options] filesort the lines of the file according to the options chosen

(Cont)

44

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

Working With Files (Cont)strings [options] filereport any sequence of 4 or more printable characters ending in <NL> or <NULL>. Usually used to search binary files for ASCII strings.tee [options] filecopy stdout to one or more filestouch [options] [date] filecreate an empty file, or update the access time of an existing file

(Cont)

45

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

Working With Files (Cont)tr [options] string1 string2translate the characters in string1 from stdin into those in string2 in stdoutuniq [options] fileremove repeated lines in a filewc [options] [file(s)]display word (or character or line) count for file(s)

(Cont)

46

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

File Archiving, Compression and Conversioncompress/uncompress/zcat [options] file[.Z]compress or uncompress a file. Compressed files are stored with a .Z ending.gzip/gunzip/zcat [options] file[.gz]compress or uncompress a file. Compressed files are stored with a .gz ending

(Cont)

47

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

File Archiving, Compression and Conversiontar key[options] [file(s)]tape archiver--refer to man pages for details on creating, listing, and retrieving from archive files. Tar files can be stored on tape or disk.

(Cont)

48

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

Remote Connectionsfinger [options] user[@hostname]report information about users on local and remote machinesftp [options] hosttransfer file(s) using file transfer protocolrcp [options] hostnameremotely copy files from this machine to another machine

(Cont)

49

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Other Useful Commands

Remote Connectionsrlogin [options] hostnamelogin remotely to another machinersh [options] hostnameremote shell to run on another machinetelnet [host [port]]communicate with another host using telnet protocol

(Cont)

50

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

UNIX Editorsviemacspico

Vi is a modal editor (2 modes)Insert mode <i> key

• Text insertion

Command mode <escape> key• All commands

51

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Cursor Movement Commands(n) indicates a number, and is optional(n)h left (n) space(s)(n)j down (n) space(s)(n)k up (n) space(s)(n)l right (n) space(s)^F forward one screen^B back one screen^D down half screen^U up half screen

vi Quick Reference Guide

52

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Cursor Movement Commands (Cont)H beginning of top line of screenM beginning of middle line of screenL beginning of last line of screenG beginning of last line of file(n)G move to beginning of line (n)0 (zero) beginning of line$ end of line(n)w forward (n) word(s)

vi Quick Reference Guide (Cont)

53

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Cursor Movement Commands (Cont)(n)b back (n) word(s)e end of word

Inserting Texti insert text before the cursora append text after the cursor (does not

overwrite other text)I insert text at the beginning of the lineA append text to the end of the liner replace the character under the cursor with

the next character typed

vi Quick Reference Guide (Cont)

54

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Inserting Text (Cont)R Overwrite characters until the end of the line

(or until escape is pressed to change command)

o (alpha o) open new line after the current line to type text

O (alpha O) open new line before the current line to type text

vi Quick Reference Guide (Cont)

55

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Deleting Textdd deletes current line(n)dd deletes (n) line(s)(n)dw deletes (n) word(s)D deletes from cursor to end of linex deletes current character(n)x deletes (n) character(s)X deletes previous character

vi Quick Reference Guide (Cont)

56

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Change Commands(n)cc changes (n) characters on line(s) until end of

the line (or until escape is pressed)cw changes characters of word until end of the

word (or until escape is pressed)(n)cw changes characters of the next (n) wordsc$ changes text to the end of the linect(x) changes text to the letter (x)

vi Quick Reference Guide (Cont)

57

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Change Commands (Cont)C changes remaining text on the current line

(until stopped by escape key)~ changes the case of the current characterJ joins the current line and the next lineu undo the last command just done on this line. repeats last changes substitutes text for current characterS substitutes text for current line

vi Quick Reference Guide (Cont)

58

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

Change Commands (Cont):s substitutes new word(s) for old:< line nos effected> s/old/new/g& repeats last substitution (:s) command.(n)yy yanks (n) lines to buffery(n)w yanks (n) words to bufferp puts yanked or deleted text after cursorP puts yanked or deleted text before cursor

vi Quick Reference Guide (Cont)

59

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

File Manipulation:w(file) writes changes to file (default is

current file):wq writes changes to current file and quits

edit session:w!(file) overwrites file (default is current file):q quits edit session w/no changes made:q! quits edit session and discards changes:n edits next file in argument list

vi Quick Reference Guide (Cont)

60

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

Vi Editor

File Manipulation (Cont):f(name) changes name of current file to (name):r(file) reads contents of file into current edit

at the current cursor position (insert a file)

:!(command) shell escape:r!(command) inserts result of shell command at

cursor positionZZ write changes to current file and exit

vi Quick Reference Guide (Cont)

61

Chulalongkorn University

2110313 Operating Systems and System Programs (1/2010)

Tutor Session - 2

End

Question ?

… Answer