SysProg-Tutor 02 Introduction to Unix Operating System
1. Tutor Session - 2
Chulalongkorn
Tutor Session II:
Introduction to UNIX
University
Operating System
Wongyos Keardsri (P’Bank)
Department of Computer Engineering
Faculty of Engineering, Chulalongkorn University
Bangkok, Thailand
Mobile Phone: 089-5993490
E-mail: wongyos@gmail.com, MSN: bankberrer@hotmail.com
Twitter: @wongyos
2110313 Operating Systems and System Programs (1/2010)
2. Tutor Session - 2
Tutor Outline
Chulalongkorn
University
History of UNIX Directory Navigation and
Control
Unix Structure
File Maintenance
UNIX Shell Commands
UNIX File System Display Commands
Getting Started Special Unix Features
Logging in / Exiting
Text Processing
Environment Variables
Other Useful Commands
Unix Command Line
Structure vi Editor
System Resource
Commands
2 2110313 Operating Systems and System Programs (1/2010)
3. Tutor Session - 2
History of UNIX
Chulalongkorn
University
Unix (officially trademarked as UNIX®) is a
computer operating system
In 1965, Bell Laboratories joins with MIT and
General Electric developed the new operating
system named Multics
In 1969, Some of the Bell Labs programmers
designed and implemented the first version of the
Unix File System on a PDP-7
Given the name UNIX by Brian Kernighan
3 2110313 Operating Systems and System Programs (1/2010)
4. Tutor Session - 2
History of UNIX
Chulalongkorn
University (Cont)
UNIX developer team (Bell Labs programmers)
Ken Thompson
Dennis Ritchie
Rudd Canaday
Doug McIlroy
Languages written in UNIX
BCPL (Multics)
B (Unix) Unix version 1.0
C (Unix) After Unix version 2.0
4 2110313 Operating Systems and System Programs (1/2010)
5. Tutor Session - 2
Unix Structure
Chulalongkorn
University
Unix features
Multi-user and time-sharing
Multi-tasking
Multi-level (Hierarchical file system)
Multi-processor
Simple user interface
Simple utilities
Free Unix-like operating systems
Linux
BSD
5 2110313 Operating Systems and System Programs (1/2010)
6. Tutor Session - 2
Unix Structure
Chulalongkorn
University (Cont)
Several hundred utility programs
User commands
The Unix system kernel and The shells
System calls
C library functions
Device & network interface
File format
Games and demos
System maintenance
6 2110313 Operating Systems and System Programs (1/2010)
7. Tutor Session - 2
Unix Structure
Chulalongkorn
University (Cont)
Unix System Structure
Kernel - controls hardware
- handles input/output
- manages file system
- manages memory
- schedules processes
Hardware
System Calls
Shell - accepts and executes user
Programs=Commands commands [command interpreter]
7 2110313 Operating Systems and System Programs (1/2010)
8. Tutor Session - 2
Unix Structure
Chulalongkorn
University (Cont)
UNIX shell
Bourne 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.
8 2110313 Operating Systems and System Programs (1/2010)
9. Tutor Session - 2
Unix Structure
Chulalongkorn
University (Cont)
Unix File System
/ Root
bin etc lib usr dev tmp home
sh date ls passwd group libc.so tty null
bank u50skp
bin local man
bin hello.c cpcu
Hierarchical file system
9 2110313 Operating Systems and System Programs (1/2010)
10. Tutor Session - 2
Unix Structure
Chulalongkorn
University (Cont)
Directory: etc, usr, home, bank, cpcu
File: libc.so, hello.c
Path: /home/bank/cpcu
Two special directory entries
. the current directory
.. the parent of the current directory
10 2110313 Operating Systems and System Programs (1/2010)
11. Tutor Session - 2
Getting Started
Chulalongkorn
University
Logging in
After 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: root
password:
11 2110313 Operating Systems and System Programs (1/2010)
12. Tutor Session - 2
Getting Started
Chulalongkorn
University (Cont)
Exiting
^D indicates end of data stream; can log a
user off.
^C interrupt
logout leave the system
exit leave the shell
Terminal Type
vt100 Most computers
sun Sun workstation
xterms or xterm X-Terminal
12 2110313 Operating Systems and System Programs (1/2010)
13. Tutor Session - 2
Getting Started
Chulalongkorn
University (Cont)
Environment Variables
DISPLAY The graphical display to use, e.g.nyssa:0.0
EDITOR The path to your default editor, e.g.
/usr/bin/vi
GROUP Your login group, e.g. staff
HOME Path to your home directory, e.g.
/home/frank
HOST The hostname of your system, e.g. nyssa
LOGNAME The name you login with, e.g. frank
PATH Paths to be searched for commands,
e.g. /usr/bin:/usr/ucb:/usr/local/bin
13 2110313 Operating Systems and System Programs (1/2010)
14. Tutor Session - 2
Getting Started
Chulalongkorn
University (Cont)
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/csh
TERM Your terminal type, e.g. xterm
USER Your username, e.g. frank
14 2110313 Operating Systems and System Programs (1/2010)
15. Tutor Session - 2
Getting Started
Chulalongkorn
University (Cont)
Set a global environment variable
C shell:
% setenv NAME value
Bourne shell:
$ NAME=value; export NAME
List global environmental variables with the env
or printenv commands.
Unset with the unsetenv (C shell) or unset
(Bourne shell) commands.
15 2110313 Operating Systems and System Programs (1/2010)
16. Tutor Session - 2
Getting Started
Chulalongkorn
University (Cont)
Set a local shell variable Change the shell
C shell: $ /bin/sh
$ /bin/csh
% set name=value $ /bin/ksh
$ /usr/pkg/bin/bash
Bourne shell:
$ name=value
Display environment variables
echo $NAME
16 2110313 Operating Systems and System Programs (1/2010)
17. Tutor Session - 2
Getting Started
Chulalongkorn
University (Cont)
C Shell History Substitution
history 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
17 2110313 Operating Systems and System Programs (1/2010)
18. Tutor Session - 2
Getting Started
Chulalongkorn
University (Cont)
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
18 2110313 Operating Systems and System Programs (1/2010)
19. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University
Unix Command Line Structure
$ command [options] [arguments]
Unix manual / Help
$ man command_name
$ man -k keyword
$ man -k “key words”
19 2110313 Operating Systems and System Programs (1/2010)
20. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
System Resource Commands
date report the current date and time
cal report the calendar
df report the summary of disk blocks and inodes
free and in use
du report amount of disk space in use
hostname/uname display or set (super-user only)
the name of the current machine
kill 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.
20 2110313 Operating Systems and System Programs (1/2010)
21. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
System Resource Commands (Cont)
passwd set or change your password
ps show status of active processes
script saves everything that appears on the
screen to file until exit is executed
stty set or display terminal control options
whereis 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
21 2110313 Operating Systems and System Programs (1/2010)
22. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
System Resource Commands (Cont)
who or w report who is logged in and what
processes are running
whoami report current user name
finger report who is logged in
ifconfig report network interfaces
su switch user
write send a message to another user
22 2110313 Operating Systems and System Programs (1/2010)
23. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
Directory Navigation and Control
cd [directory]
change directory
ls [options] [directory or file]
list directory contents or file permissions
mkdir [options] directory
make a directory
pwd
print working (current) directory
rmdir [options] directory
remove a directory
23 2110313 Operating Systems and System Programs (1/2010)
24. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
List file and directory contents
drwxr-xr-x 3 root wheel 512 Jul 4 23:48 2110101
drwxr-xr-x 4 root wheel 512 Aug 28 2005 2110313
drwxr-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 Owner Group Size Modified/Created Filename
date
Type field Number of link
24 2110313 Operating Systems and System Programs (1/2010)
25. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
Permission
Type 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 drwxr-xr-x
• x execute permission
• - no permission
25 2110313 Operating Systems and System Programs (1/2010)
26. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
Permission number
7 111 rwx
6 110 rw-
5 101 r-x
4 100 r--
3 011 -wx
2 010 -w-
1 001 --x
0 000 ---
26 2110313 Operating Systems and System Programs (1/2010)
27. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
File Maintenance Commands
chgrp [options] group file
change the group of the file
chmod [options] file
change file or directory access permissions
chown [options] owner file
change the ownership of a file; can only be done by the
superuser
cp [options] file1 file2
copy file1 into file2; file2 shouldn't already exist. This
command creates or overwrites file2.
27 2110313 Operating Systems and System Programs (1/2010)
28. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
File Maintenance Commands (Cont)
mv [options] file1 file2
move file1 into file2
rm [options] file
remove (delete) a file or directory (-r recursively
deletes the directory and its contents) (-i prompts
before removing files)
28 2110313 Operating Systems and System Programs (1/2010)
29. Tutor Session - 2
Unix Command Line Structure
Chulalongkorn
University (Cont)
Display Commands
cat [options] file
concatenate (list) a file
echo [text string]
echo the text string to stdout
head [-number] file
display the first 10 (or number of) lines of a file
more (or less or pg ) [options] file
page through a text file
tail [options] file
display the last few lines (or parts) of a file
29 2110313 Operating Systems and System Programs (1/2010)
30. Tutor Session - 2
Special Unix Features
Chulalongkorn
University
I/O redirection and piping
Output redirection to a file
Input redirection from a file
Piping
Output of one command becomes the input of a
subsequent command
There are 3 standard file descriptors:
stdin 0 Standard input to the program
stdout 1 Standard output from the program
stderr 2 Standard error output from the program
30 2110313 Operating Systems and System Programs (1/2010)
31. Tutor Session - 2
Special Unix Features
Chulalongkorn
University (Cont)
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
31 2110313 Operating Systems and System Programs (1/2010)
32. Tutor Session - 2
Special Unix Features
Chulalongkorn
University (Cont)
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
32 2110313 Operating Systems and System Programs (1/2010)
33. Tutor Session - 2
Special Unix Features
Chulalongkorn
University (Cont)
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 file
2>&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)
33 2110313 Operating Systems and System Programs (1/2010)
34. Tutor Session - 2
Special Unix Features
Chulalongkorn
University (Cont)
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
34 2110313 Operating Systems and System Programs (1/2010)
35. Tutor Session - 2
Special Unix Features
Chulalongkorn
University (Cont)
Control Over Command Execution
Background (non-interactive)
command &
Sequential
command1 ; command2
Subprocess (run as an atomic unit)
(command)
35 2110313 Operating Systems and System Programs (1/2010)
36. Tutor Session - 2
Special Unix Features
Chulalongkorn
University (Cont)
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)
36 2110313 Operating Systems and System Programs (1/2010)
37. Tutor Session - 2
Text Processing
Chulalongkorn
University
Filter
A filter (in Unix) is a program that reads some input,
performs a simple translation on it, and writes some
output
Examples of Unix filters
grep
sort
wc
awk
vi
sed
37 2110313 Operating Systems and System Programs (1/2010)
38. Tutor Session - 2
Text Processing
Chulalongkorn
University (Cont)
Text Processing Commands
awk [options] file
scan for patterns in a file and process the results
grep [options] 'search string' file
search the argument (in this case probably a file) for all
occurrences of the search string, and list them.
sed [options] file
stream editor for editing files from a script or from the
command line
38 2110313 Operating Systems and System Programs (1/2010)
39. Tutor Session - 2
Text Processing
Chulalongkorn
University (Cont)
Regular expressions
Regular 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
39 2110313 Operating Systems and System Programs (1/2010)
40. Tutor Session - 2
Text Processing
Chulalongkorn
University (Cont)
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
40 2110313 Operating Systems and System Programs (1/2010)
41. Tutor Session - 2
Text Processing
Chulalongkorn
University (Cont)
Regular expressions (Cont)
exp$ regular expression must end at the beginning
of the line
treat the next character literally
41 2110313 Operating Systems and System Programs (1/2010)
42. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University
Working With Files
cmp [options] file1 file2
compare 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 file2
compare the two files and display the differences (text
files only)
file [options] file
classify the file type
42 2110313 Operating Systems and System Programs (1/2010)
43. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University (Cont)
Working With Files (Cont)
find directory [options] [actions]
find files matching a type or pattern
ln [options] source_file
target link the source_file to the target
paste [options] file
paste field(s) onto the lines in file
sort [options] file
sort the lines of the file according to the options
chosen
43 2110313 Operating Systems and System Programs (1/2010)
44. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University (Cont)
Working With Files (Cont)
strings [options] file
report any sequence of 4 or more printable characters
ending in <NL> or <NULL>. Usually used to search
binary files for ASCII strings.
tee [options] file
copy stdout to one or more files
touch [options] [date] file
create an empty file, or update the access time of an
existing file
44 2110313 Operating Systems and System Programs (1/2010)
45. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University (Cont)
Working With Files (Cont)
tr [options] string1 string2
translate the characters in string1 from stdin into
those in string2 in stdout
uniq [options] file
remove repeated lines in a file
wc [options] [file(s)]
display word (or character or line) count for file(s)
45 2110313 Operating Systems and System Programs (1/2010)
46. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University (Cont)
File Archiving, Compression and Conversion
compress/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
46 2110313 Operating Systems and System Programs (1/2010)
47. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University (Cont)
File Archiving, Compression and Conversion
tar 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.
47 2110313 Operating Systems and System Programs (1/2010)
48. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University (Cont)
Remote Connections
finger [options] user[@hostname]
report information about users on local and remote
machines
ftp [options] host
transfer file(s) using file transfer protocol
rcp [options] hostname
remotely copy files from this machine to another
machine
48 2110313 Operating Systems and System Programs (1/2010)
49. Tutor Session - 2
Other Useful Commands
Chulalongkorn
University (Cont)
Remote Connections
rlogin [options] hostname
login remotely to another machine
rsh [options] hostname
remote shell to run on another machine
telnet [host [port]]
communicate with another host using telnet protocol
49 2110313 Operating Systems and System Programs (1/2010)
50. Tutor Session - 2
Vi Editor
Chulalongkorn
University
UNIX Editors
vi
emacs
pico
Vi is a modal editor (2 modes)
Insert mode <i> key
• Text insertion
Command mode <escape> key
• All commands
50 2110313 Operating Systems and System Programs (1/2010)
51. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide
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
51 2110313 Operating Systems and System Programs (1/2010)
52. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
Cursor Movement Commands (Cont)
H beginning of top line of screen
M beginning of middle line of screen
L beginning of last line of screen
G 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)
52 2110313 Operating Systems and System Programs (1/2010)
53. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
Cursor Movement Commands (Cont)
(n)b back (n) word(s)
e end of word
Inserting Text
i insert text before the cursor
a append text after the cursor (does not
overwrite other text)
I insert text at the beginning of the line
A append text to the end of the line
r replace the character under the cursor with
the next character typed
53 2110313 Operating Systems and System Programs (1/2010)
54. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
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
54 2110313 Operating Systems and System Programs (1/2010)
55. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
Deleting Text
dd deletes current line
(n)dd deletes (n) line(s)
(n)dw deletes (n) word(s)
D deletes from cursor to end of line
x deletes current character
(n)x deletes (n) character(s)
X deletes previous character
55 2110313 Operating Systems and System Programs (1/2010)
56. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
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) words
c$ changes text to the end of the line
ct(x) changes text to the letter (x)
56 2110313 Operating Systems and System Programs (1/2010)
57. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
Change Commands (Cont)
C changes remaining text on the current line
(until stopped by escape key)
~ changes the case of the current character
J joins the current line and the next line
u undo the last command just done on this line
. repeats last change
s substitutes text for current character
S substitutes text for current line
57 2110313 Operating Systems and System Programs (1/2010)
58. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
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 buffer
y(n)w yanks (n) words to buffer
p puts yanked or deleted text after cursor
P puts yanked or deleted text before cursor
58 2110313 Operating Systems and System Programs (1/2010)
59. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
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
59 2110313 Operating Systems and System Programs (1/2010)
60. Tutor Session - 2
Vi Editor
Chulalongkorn
University vi Quick Reference Guide (Cont)
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 position
ZZ write changes to current file and exit
60 2110313 Operating Systems and System Programs (1/2010)
61. Tutor Session - 2
End
Chulalongkorn
University
Question ?
… Answer
61 2110313 Operating Systems and System Programs (1/2010)