CSc 352: Basic Unix

Reading

Chapter 1:

Upto “Background
|obs” (page 17)
2
What is Unix?

Uníx ís an operatíng system

síts between the hardware and the
user/appíícatíons

provídes hígh-íeveí abstractíons (e.g., fííes) and
servíces (e.g., muítíprogrammíng)

Línux:

a “Uníx-ííke” operatíng system: user-íeveí
ínterface very símííar to Uníx

code base ís dífferent from orígínaí Uníx code
3
Layers of a Unix system
4
hardware
sheíí
Uníx operatíng system kerneí
users appíícatíons sheíí
comman
ds
system
caíís
The file system

A file ís basícaííy a
sequence of bytes

Coííectíons of fííes
are grouped ínto
directories (≈ foíders)

A dírectory ís ítseíf a
fííe
fííe system has a
híerarchícaí structure
(í.e., ííke a tree)
o
the root ís referred to
as “/”
5
.
dd
cc bb
/
ff
ee
!"erything is a file#

In Uníx, everythíng íooks ííke a fííe:

documents stored on dísk

dírectoríes

ínter-process communícatíon
– network connectíons
– devíces (prínters, graphícs cards, ínteractíve termínaís,
.)

They are accessed ín a uníform way:

consístent API (e.g., read, wríte, open, cíose,
.)

consístent namíng scheme (e.g.,
/home/debray, /dev/cdrom)
6
Referring to files: $%sol&te
'aths

An absolute path
specífíes how to get to
a fííe startíng at the
fííe system root

ííst the dírectoríes on
the path from the root
(“/”), separated by “/”
7
dd cc bb
/
ff
ee
gg
Referring to files: $%sol&te
'aths

An absolute path
specífíes how to get to
a fííe startíng at the
fííe system root

ííst the dírectoríes on
the path from the root
(“/”), separated by “/”
8
dd cc bb
/
ff
ee
gg
absoíute path:
(dd(ee(gg
Referring to )iles: Relati"e
'aths

Typícaííy we have a
notíon of a “current
directory”

A relative path specífíes
how to get to a fííe
startíng from the
current dírectory

´**´ means “move up one
íeveí”

´*´ means current
dírectory

ííst the dírectoríes on the
path separated by “/”
9
dd cc bb
/
ff
ee
gg
Referring to files: Relati"e
'aths

Typícaííy we have a
notíon of a “current
directory”

A relative path specífíes
how to get to a fííe
startíng from the
current dírectory

´**´ means “move up one
íeveí”

´*´ means current
dírectory

ííst the dírectoríes on the
path separated by “/”
10
dd cc bb
/
ff
ee
gg
Exampíe:
ff reíatíve to ee
ís: **(ff
Referring to files: Relati"e
'aths

Typícaííy we have a
notíon of a “current
directory”

A relative path specífíes
how to get to a fííe
startíng from the
current dírectory

´**´ means “move up one
íeveí”

´*´ means current
dírectory

ííst the dírectoríes on the
path separated by “/”
11
dd cc bb
/
ff
ee
gg
Exampíe:
cc reíatíve to ee
ís: **(**(cc
+ome directories

Each user has a “home dírectory”

specífíed when the account ís created

gíven ín the fííe (etc(,ass-d

When you íog ín, your current dírectory ís
your home dírectory

can then start a shell and íssue commands

Notatíonaí shorthand:

one´s own home dírectory: .

some other user /oe´s home dírectory: ./oe
12
A sheíí ís |ust
an ínterpreter
for Uníx
commands
0n,&t and o&t,&t

Data are read from and wrítten to í/o
streams

There are three predefíned streams:
stdin : “standard ínput” − usuaííy, keyboard ínput
stdo&t : “standard output” − usuaííy, the screen
stderr : “standard error” − for error messages (usuaííy,
the screen)
Other streams can be created usíng system caíís
(e.g., to read or wríte a specífíc fííe)
13
'rocesses

Programs are executed vía processes

a process ís the unít of executíon

consísts of:
• the code that ís executed
• the data thís code manípuíates

Dífferent processes execute concurrentíy

each process has íts own address space, stdín,
stdout, etc.

theír executíon ís managed by the operatíng
system

Common tasks are carríed out usíng a set
of system-províded programs caííed
commands

the sheíí ís aíso a system-províded program
14
Unix Commands

Each command performs |varíatíons of| a
síngíe task
– “optíons” can be used to modífy what a command does
– dífferent commands can be “gíued together” to perform
more compíex tasks

Syntax:
command options arguments
Examples:
15
Command 1,tions $rg&ments
pwd
cd /home/debray
ís -a -í
ís -aí /usr/íocaí
Options can
(usually) be
combined
together:
these are
equivalent
Unix Commands

Each command performs |varíatíons of| a
síngíe task
– “optíons” can be used to modífy what a command does
– dífferent commands can be “gíued together” to perform
more compíex tasks

Syntax:
command options arguments
Examples:
16
Command 1,tions $rg&ments
pwd
cd /home/debray
ís -a -í
ís -aí /usr/íocaí
Not always required:
may have default
values
defauíts
to
current
dírector
y
typícaí defauíts:
• ínput: stdín
• output: stdout
• dírectory: current
!xam,les of Unix commands 0

Fíguríng out one´s current dírectory: ,-d

Movíng to another dírectory: cd targetdir
Examples:
17
cd (
move to the root of the
fííe system
cd .
(aíso: |ust “cd” by ítseíf)
move to one´s home
dírectory
cd (&sr(local(src move to /usr/íocaí/src
cd **(** move up two íeveís
!xam,les of Unix commands 00

Command: ls 2 lists the contents of a
directory

Exampíes:
18
ls ííst the fííes ín the current dírectory
won’t show files whose names start
with .’
ls (&sr(%in ííst the fííes ín the dírectory /usr/bín
ls 3l gíve a “íong format” íístíng (provídes
addítíonaí ínfo about fííes)
ls 3a ííst aíí fííes ín the current dírectory,
íncíudíng those that start wíth ´.´
ls 3al (&sr(local gíve a “íong format” íístíng of aíí the
fííes (íncí. those startíng wíth ´.´) ín
/usr/íocaí
Com%ining commands

The output of one command can be fed to
another command as ínput.
– Syntax: command
1
| command
2
Exampíe:
19
“pípe”
ls íísts the fííes ín a dírectory
more foo shows the fííe foo one screenfuí at a tíme
ls 4 more íísts the fííes ín a dírectory one screenfuí
at a tíme
+o- this -or5s:
• ls wrítes íts output to íts stdo&t
• more´s ínput stream defauíts to íts
stdin
• the pípe connects ls´s stdout to
more´s stdín
• the píped commands run “ín
paraííeí”
)inding o&t a%o&t commands 0
Fíguríng out whích command to use
a,ro,os !eyword
man 65 !eyword
“searches a set of database fííes contaíníng short
descríptíons of system commands for keywords”

Heípfuí, but not a panacea:

depends on appropríate choíce of keywords

may requíre tríaí and error

may return a íot of resuíts to síft through
• pípe through more
20
)inding o&t a%o&t commands 00
Fíguríng out how to use a command
man command
“díspíays the on-ííne manuaí pages”

Provídes ínformatíon about command
optíons, arguments, return vaíues, bugs,
etc.
21
!xam,le: man ls#
22
ítems wíthín square
brackets are optíonaí
!xam,le: man man#
23
!xam,le: man man#
24
we can specify
what !ind of
information we
want
Some other &sef&l commands

-c |file|
• word count: counts characters, words, and íínes ín the
ínput

gre, pattern |file|
• seíect íínes ín the ínput that match pattern

head "n |file|
• show the fírst n íínes of the ínput

tail "n |file|
• show the íast n íínes of the ínput
• c, file
1
file
2
• copy file
1
to file
2
• m" file
1
file
2
• move file
1
to file
2
25
!xam,le: 7eleting a file
Fíguríng out whích command to use:

a,ro,os delete
• produces many screenfuís of output that go by too
quíckíy

a,ro,os delete 4 more

many screenfuís of output, but shown one screenfuí
at a tíme

most of the commands shown aren´t reíevant
26
!xam,le: 7eleting a file.
(1)
Idea 1: fííter out írreíevant
stuff
man 65 delete 4
gre, file
27
a íot fewer
resuíts;
nothíng
reíevant
!xam,le: 7eleting a file8
(2)
Idea 2: try a dífferent
keyword
man 65 remo"e 4
gre, file
28
!xam,le: 7eleting a file8
(3)
Idea 2: try a dífferent
keyword
man 65 remo"e 4
gre, file
29
these are the oníy
commands that
refer to removíng
fííes
!xam,le: 7eleting a file8
(4)
Idea 2: try a dífferent
keyword
man 65 remo"e 4
gre, file
30
thís ís the oníy
user command
that refers to
removíng fííes
!xam,le: 7eleting a file8
(5)
Confírm that thís ís the appropríate command:
man rm#
31
strongíy
suggest
makíng thís
your defauít
Setting defa&lts for yo&r
commands

Create an “aíías” for your command

syntax dífferent for dífferent sheíís

bash: alias aliasName=“cmdName#
e$g$: alias rm%&rm "i#

see “man alias” for detaíís

To have thís aíías ín force whenever you
íog ín, add thís ííne to the fííe
-/.bashrc // assumíng your íogín sheíí
ís “bash”

To fínd out your íogín sheíí, run the
command
echo $0 32
'attern matching: gre,
33
'attern matching: gre,8
(1)
34
prínt the current
dírectory
show the contents of thís
fííe
prínt out the íínes that
match “natíon”
'attern matching: gre,8
(2)
35
“print all lines in the input
that match the string er”
'attern matching: gre,8
(3)
36
“print all lines in the input that
match the string er or re
print all lines in the input that
begin with the string er or re
'attern matching in the shell

We can aíso use patterns ín sheíí
commands, e.g.:
Example:

37
9
matches any stríng
: .
;
matches any one of the characters wíthín
braces
ls %9c
ííst fííes that begín wíth % and end
wíth c
ls
a:xy<;9
ííst fííes startíng wíth a foííowed by x,
y, or <
ls 9*,df
ííst fííes endíng wíth “*,df”
0(1 Redirection

Defauít ínput/output behavíor for
commands:

stdin: keyboard; stdo&t: screen; stderr:
screen

We can change thís usíng I/O redírectíon:
38
cmd <
file
redírect cmd´s stdín to read from file
cmd >
file
redírect cmd´s stdout to file
cmd >>
file
append cmd´s stdout to file
cmd &>
file
redírect cmd´s stdout and stderr to
file
cmd
1
|
cmd
2
redírect cmd
1
´s stdout to cmd
2
´s
stdín
=etting more information
a%o&t files

ís -í : provídes addítíonaí ínfo about fííes
39
=etting more information
a%o&t files8 (1)
40
file name last-modified time size group owner
no. of hard links
access permissions
file type

normaí fííe
d dírectory
l
(ell)
symboííc íínk
)ile access ,ermissions
41
access permissions for owner (u)
access permissions for group (g)
access permissions for others (o)
r read
- wríte
x execute
(executabíe fííe)
enter (dírectory)

no permíssíon
Changing file access
,ermissions
Command:
chmod who±what file
1
file
2
… file
n
Example:
42
C {r, w, x}
C {a, u, g, o}
chmod &3-
foo
remove wríte permíssíon for user on fííe foo
chmod g>rx
%ar
gíve read and execute permíssíon to group
for bar
chmod o3r-x
9*doc
remove aíí access permíssíons for “other
users” (í.e., not owner or group members)
for *.doc fííes
chmod a>r-
,9
gíve read and wríte permíssíon to everyone
for aíí fííes startíng wíth p
)oregro&nd and Bac5gro&nd
'rocesses

Muítípíe processes can run concurrentíy

at any poínt, there ís exactíy one process that
you can ínteract wíth through the keyboard
(“foreground process”)

remaíníng processes execute “ín the
background”

A process can be started ín the
background:
processName ?

The executíon of the current foreground
process can be paused vía ctrí-z

“%g” wííí then start ít executíng ín the
background

“fg” wííí bríng ít to the foreground
43

Sign up to vote on this title
UsefulNot useful