Chapter 3 Introduction to UNIX system Chapter 3: Introduction to UNIX 1

Download Report

Transcript Chapter 3 Introduction to UNIX system Chapter 3: Introduction to UNIX 1

Chapter 3
Introduction to UNIX system
Chapter 3: Introduction to UNIX
system
1
Facilities
http://www.comp.nus.edu.sg/facilities.html
Chapter 3
Facilities
2
Rules on computer usage
man policies
 sharing of computer account
 playing games during office hours
 misbehavior and eating in labs
 sending mass mails or chain mails
 running redundant or destructive processes
 sending or posting distasteful, deragatory or
prejudicial mails or messages
Chapter 3
Rules on computer usage
3
Logging in to and out of
NUSNET-III and UNIX machines
Follow the steps given in chapter 3 of
Preparatory Course for Programming.
Chapter 3
Logging in and out
4
Password
Your password is important -- guard it!
Change password regularly -- yppasswd
Rules for password -- man passwd
Use non-guessable password -- man
password
Chapter 3
Password
5
UNIX file system
/
home
...
stu98
...
...
root
bin
usr
...
...
stu99
...
garfield
Garfield’s home
directory
doc
Chapter 3
prog
my_file
UNIX file system
6
Basic UNIX commands
Chapter 3
DOS
dir
dir/w
dir/a
UNIX
ls –l
ls
ls –a
cd
cd
del
rd
md
rename
move
copy
type
rm
rmdir
mkdir
mv
mv
cp
cat
Purpose (in UNIX context)
List files in the current directory.
List files in the current directory.
List all files, including hidden files, in the
current directory.
Hidden files are files whose names begin
with a period (e.g.: ‘.plan’, ‘.profile’).
Change directory.
‘cd’ with no
arguments brings you right to your home
directory.
Remove file.
Remove directory.
Make new directory.
Rename file.
Move file.
Copy file.
Catenate file (display file’s content).
Basic UNIX commands
7
Command format
command <options> <arguments>
valid
invalid
who
who -u
who
who-u
who - u
lsFa
ls- F
ls -F-a
ls -F
ls -a
ls -Fa
ls -a -F
Chapter 3
Command format
8
The ‘man’ command
Help facility -- man





Chapter 3
man <command> (eg: man who)
man helpinfo
man rules, man policies
man printers, man printquota, man pusage
man mailgroups, man mailinglists
The ‘man’ command
9
Organising directory
pwd, ls, mkdir, rmdir, cd
pwd -- print working directory
garfield@decunx:~[xx]$ pwd
/home/stu99/garfield
Chapter 3
Organising directory
10
Organising directory
/
Full pathname:
reference from root
home
...
bin
usr
...
...
. . . stu98
/home/stu99/garfield
stu99
/
/usr
...
garfield
/home/stu99/garfield/prog
~/prog
(~ = home dir)
doc prog my_file
~tantc/prep/index.html
(= /home/staff/tantc/prep/index.html)
Chapter 3
Organising directory
...
11
Organising directory
/
Relative pathname:
reference from curr. dir.
home
(. = current dir; .. = parent dir) . . . stu98
my_file
../../stu98
...
./doc/hello
curr. dir.
garfield/my_file 
doc
././..
bin 
hello
Chapter 3
Organising directory
...
bin
usr
...
...
stu99
...
garfield
prog
my_file
12
Organising directory
ls -- list directory
garfield@decunx:~[xx]$ ls
c
doc
my_file
garfield@decunx:~[xx]$ ls -F
c/
doc/
my_file
garfield@decunx:~[xx]$ ls -l
drwx-----drwx------rw------Chapter 3
1 garfield cpe99
1 garfield cpe99
1 garfield cpe99
512 May 10 09:39 c
512 Jun 21 19:43 doc
142 May 5 14:20 my_file
Organising directory
13
Organising directory
mkdir -- make a new directory
garfield@decunx:~[xx]$ mkdir temp
rmdir -- remove a directory (must be empty)
garfield@decunx:~[xx]$ rmdir temp
Chapter 3
Organising directory
14
Organising directory
cd -- change directory
garfield@decunx:~[xx]$ cd doc
garfield@decunx:~/doc[xx]$ pwd
/home/stu99/garfield/doc
cd without argument -- return to home
directory
Chapter 3
Organising directory
15
File access permissions
garfield@decunx:~[xx]$ ls -l
drwx------
1 garfield cpe99
512 May 10 09:39 c
d rwx --- --d = directory
- = file
Chapter 3
u
g
o
Access:
r = read
w = write
x = execute/access
File access permissions
16
File access permissions
chmod -- change mode
garfield@decunx:~[xx]$ chmod g+rx .
garfield@decunx:~[xx]$ chmod g+rx c
garfield@decunx:~[xx]$ chmod g+r c/example1.c
garfield
Lower protection
code for these
directories and file
c
prog
...
example1.c
Chapter 3
File access permissions
17
File access permissions
Symbolic mode
chmod g+w <filename>
chmod o=rx <filename>
chmod g+w,o=rx <filename>
Octal mode
chmod 504 <filename>
chmod 761 <filename>
Chapter 3
File access permissions
r-x---r-rwxrw---x
18
File manipulation
cp, mv, rm, cat (pr, pg, more, less)
cp -- copy file
cp <source> <target>
cp file1 file2
(what if there exists a file called file2?
or there exists a directory called file2?)
cp c/prog1.c 
cp c/prog1.c .
cp ~tantc/quiz/answers ans
Chapter 3
File manipulation
19
File manipulation
mv -- move/rename file
mv <source> <target>
mv
mv
mv
mv
Chapter 3
file1 file2
c/prog1.c .
c/prog1.c ./prog2.c
c/prog1.c prog2.c
File manipulation
20
File manipulation
rm -- remove file
rm file1
rm file1 file2
rm -i file1
Chapter 3
File manipulation
21
File manipulation
cat -- catenate file; displays file’s content
pr, pg, more, less
cat file1 file2
pr file1
more file2
use cat for small files; use pg, more, less for big
files
Chapter 3
File manipulation
22
Printing files
man printers, man printquota, man pusage
lpr -- send job to printer
lpr -P<printerid> <filename>
garfield@decunx:~[xx]$ lpr -Ppsmr my_file
Chapter 3
Printing files
23
Printing files
lpq -- check printer queue
garfield@decunx:~[xx]$ lpq -Ppsmr
Rank
Owner
active tantc
1st
garfield
Job
822
823
Files
exercise1
my_file
Total Size
38284 bytes
142 bytes
lprm -- remove print job
garfield@decunx:~[xx]$ lprm –Ppsmr 823
Chapter 3
Printing files
24
Useful UNIX features
Meta-characters for filename matching
 * (wildcard): matches any string
 ?: matches a single character
 [set]: matches a character in the set
Assume files are part5, part6, part7, part10, part12, partA.
cat part[4-6]
rm part*
ls part?
Chapter 3
cat part5 part6
rm part10 part12 part5 part6 part7 partA
ls part5 part6 part7 partA
Useful UNIX features
25
Useful UNIX features
I/O redirection
 <: input redirection
 >: output redirection
 >>: append
date > file1
ls >> file1
mailx dkiong <file1
Chapter 3
redirect output of date into file1
append output of ls into file1
send file1 to dkiong
Useful UNIX features
26
Useful UNIX features
Pipe |
Output of command sent as input to the next
command in the piple
who | wc -l
sort list | head | tail -3
Chapter 3
count number of users logged in
extract 8th, 9th, 10th lines of list
Useful UNIX features
27
Using the pico editor
Follow the steps given in chapter 3 of
Preparatory Course for Programming.
Learn another more advanced editor: vi, vim,
joe, emacs.
Handy features for programming: search and
replace, cut-and-paste, auto indentation,
macros.
Chapter 3
Using the pico editor
28
Customising UNIX environment
Shell variables: system and user-defined.
System shell variables:
HOME
TERM
PS1
SHELL
LOGNAME
Chapter 3
home directory
terminal type
primary prompt string
shell type
username
Customising UNIX environment
29
Customising UNIX environment
$var represents the value stored in var:
$HOME
$TERM
$PS1
$SHELL
$LOGNAME
is '/home/stu99/garfield'
is 'vt100'
is '$'
is 'bash'
is 'garfield’
to see value in variable, use echo:
garfield@decunx:~[xx]$ echo $HOME
/home/stu99/garfield
Chapter 3
Customising UNIX environment
30
Customising UNIX environment
Changing a shell variable:
garfield@decunx:~[xx]$ PS1='Yes ! '
Yes! echo "what does PS1 contain? Ans: $PS1"
what does PS1 contain? Ans: Yes!
Yes!
Chapter 3
Customising UNIX environment
31
Customising UNIX environment
Creating/changing a user-defined variable:
garfield@decunx:~[xx]$
garfield@decunx:~[xx]$
bad
garfield@decunx:~[xx]$
garfield@decunx:~[xx]$
/home/stu99/garfield
Chapter 3
good="bad"
echo $good
xyz="$HOME"
echo $xyz
Customising UNIX environment
32
Customising UNIX environment
Alias: a synonym for command/statement.
garfield@decunx:~[xx]$ alias dir='ls'
garfield@decunx:~[xx]$ dir
c/
doc/
my_file
garfield@decunx:~[xx]$ alias bye='exit'
garfield@decunx:~[xx]$ bye
(log out…)
unalias to remove alias:
garfield@decunx:~[xx]$ unalias bye
Chapter 3
Customising UNIX environment
33
Customising UNIX environment
.profile: automatically executed when you log in
put aliases and other settings inside .profile to
make them ‘permanent’
to activate definitions in .profile, type:
garfield@decunx:~[xx]$ . .profile
Chapter 3
Customising UNIX environment
34
Homework
Try exercises behind chapter 3.
Chapter 3
Homework
35