Document

advertisement
CIS 90 - Lesson 8
Lesson Module Status
• Slides –
• Properties • Flash cards –
• No-stress quiz –
• Web calendar summary –
• Web book pages –
• Commands –
• Lab –
• Supplies –
• Class PC's –
• Hide script –
CIS 90 - Lesson 8
Quiz
Please close your books, turn off your
monitor, take out a blank piece of paper
and answer the following questions:
• With a umask of 002 what permissions would a
newly created file have?
• What is the numeric permission equivalent of
rwxr_xr__ ?
• Does: chmod o+w filename give write
permission to the owner or to other users?
CIS 90 - Lesson 8
Input/Output Processing
Objectives
Agenda
• Identify the three open files an
executing program is given when
started.
• Be able to redirect input from files
and output to files
• Define the terms pipe, filter, and tee
• Use pipes and tees to combine
multiple commands
• Know how to use the following
useful UNIX commands:
o find
o grep
o wc
o sort
o spell
• Quiz
• Questions from last week
• File descriptors
• Pipelines
• Commands
• Tasks using pipelines
 = hands on exercise for topic
CIS 90 - Lesson 8
Housekeeping
CIS 90 - Lesson 8
Previous material and assignment
1. Questions?
2. Lab 6 due today
3. Five posts due today
4. Review calendar up to next test
CIS 90 - Lesson 8
Permissions - Review
r
read
w
write
x
execute
user
[rsimms@opus
total 40
-rw-r--r-- 1
drwxr-xr-x 2
drwxr-xr-x 2
-rw-r--r-- 1
-rw-r--r-- 1
[rsimms@opus
r
read
w
write
x
execute
r
read
group
w
write
others
cis90]$ ls -l examples/
rsimms users 237 Oct 20 07:15
rsimms users 4096 Oct 20 07:16
rsimms users 4096 Oct 20 07:34
rsimms users 779 Oct 20 07:15
rsimms users 151 Oct 20 07:16
cis90]$
ant
birds
dogs
nursery
twister
x
execute
CIS 90 - Lesson 8
Permissions - Review
r
read
w
x
r
write
execute
read
user
w
x
r
write
execute
read
group
w
x
write
execute
others
rwx
Binary
Convert
Decimal
_ _ _
000
0+0+0
0
_ _ x
001
0+0+1
1
_ w _
010
0+2+0
2
_ w x
011
0+2+1
3
r _ _
100
4+0+0
4
r _ x
101
4+0+1
5
r w _
110
4+2+0
6
r w x
111
4+2+1
7
4's column
2's column
1's column
CIS 90 - Lesson 8
Permissions - Practice
r
read
w
x
write
execute
user
rw_
r
read
w
x
write
execute
group
What is this
permission in
binary?
r
read
w
x
write
execute
others
CIS 90 - Lesson 8
Permissions - Practice
r
read
w
x
write
execute
user
r
read
w
x
write
execute
group
r
read
w
x
write
execute
others
Binary number
rw_ = 1 1 0
4's column
2's column
1's column
Now, what is
this permission
in decimal?
CIS 90 - Lesson 8
Permissions - Practice
r
read
w
x
write
execute
user
r
read
w
x
write
execute
group
r
read
w
x
write
execute
others
Binary number
Decimal
number
rw_ = 1 1 0=6
4's column
2's column
1's column
CIS 90 - Lesson 8
Directory Read Permission
This table shows the
permissions needed by
different commands to
operate.
fileName1 inodeNum
fileName2 indoeNum
.
.
rwx
rwx
Permission
File
Directory
Read (4)
cat, more, file,
head, tail, cp
ls
Write (2)
vi, saving mail
cp, mv, rm, ln
Execute (1)
$ command
cd, ls -l, find
/
Removing directory r permission – can't list files (ls) in it
CIS 90 - Lesson 8
Directory Read Permission
r
read
w
x
write
execute
r
read
user
[simmsben@opus ~]$ ls
drwxrwxr-x 4 simmsben
[simmsben@opus ~]$ ls
examples/:
total 40
-rw-r--r-- 1 simmsben
drwxr-xr-x 2 simmsben
drwxr-xr-x 2 simmsben
-rw-r--r-- 1 simmsben
-rw-r--r-- 1 simmsben
w
x
write
execute
group
-ld examples/
cis90 4096 Oct 20 08:27 examples/
-lR examples/
cis90 237 Oct 20 08:27
cis90 4096 Oct 20 08:27
cis90 4096 Oct 20 08:27
cis90 779 Oct 20 08:27
cis90 151 Oct 20 08:27
ant
birds
dogs
nursery
twister
r
read
w
x
write
execute
others
[roddyduk@opus ~]$ ls -ld ../simmsben/examples/
drwxrwxr-x 4 simmsben cis90 4096 Oct 20 08:27 ../simmsben/exa
[roddyduk@opus ~]$ ls -lR ../simmsben/examples/
../simmsben/examples/:
total 40
-rw-r--r-- 1 simmsben cis90 237 Oct 20 08:27 ant
drwxr-xr-x 2 simmsben cis90 4096 Oct 20 08:27 birds
drwxr-xr-x 2 simmsben cis90 4096 Oct 20 08:27 dogs
-rw-r--r-- 1 simmsben cis90 779 Oct 20 08:27 nursery
-rw-r--r-- 1 simmsben cis90 151 Oct 20 08:27 twister
examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
../simmsben/examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
examples/dogs:
total 24
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 benji
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 duke
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 homer
[simmsben@opus ~]$
../simmsben/examples/dogs:
total 24
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 benji
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 duke
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 homer
[roddyduk@opus ~]$
Benji's dogs/ directory has group r and x permissions, duke can ls –l it
CIS 90 - Lesson 8
Directory Read Permission
r
read
w
x
write
execute
r
read
user
w
x
write
execute
group
Benji removes r permission
on his dogs directory
[simmsben@opus ~]$ chmod g-r examples/dogs/
[simmsben@opus ~]$ ls -ld examples/
drwxrwxr-x 4 simmsben cis90 4096 Oct 20 08:27
[simmsben@opus ~]$ ls -lR examples/
examples/:
total 40
-rw-r--r-- 1 simmsben cis90 237 Oct 20 08:27
drwxr-xr-x 2 simmsben cis90 4096 Oct 20 08:27
drwx--xr-x 2 simmsben cis90 4096 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 779 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 151 Oct 20 08:27
examples/
ant
birds
dogs
nursery
twister
examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
examples/dogs:
total 24
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 benji
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 duke
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 homer
[simmsben@opus ~]$
r
read
w
x
write
execute
others
Now Duke can't see what is
in that directory
[roddyduk@opus simmsben]$ ls -ld
drwxrwxr-x 4 simmsben cis90 4096
[roddyduk@opus simmsben]$ ls -lR
examples/:
total 40
-rw-r--r-- 1 simmsben cis90 237
drwxr-xr-x 2 simmsben cis90 4096
drwx--xr-x 2 simmsben cis90 4096
-rw-r--r-- 1 simmsben cis90 779
-rw-r--r-- 1 simmsben cis90 151
examples/
Oct 20 08:27 examples/
examples/
Oct
Oct
Oct
Oct
Oct
20
20
20
20
20
08:27
08:27
08:27
08:27
08:27
ant
birds
dogs
nursery
twister
examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
ls: examples/dogs: Permission denied
[roddyduk@opus simmsben]$
Removing directory r permission – can't ls (list) it's files.
CIS 90 - Lesson 8
Directory Read Permission
r
read
w
x
write
execute
r
read
user
w
x
write
execute
group
Benji removes r permission
on his dogs directory
[simmsben@opus ~]$ chmod g-r examples/dogs/
[simmsben@opus ~]$ ls -ld examples/
drwxrwxr-x 4 simmsben cis90 4096 Oct 20 08:27
[simmsben@opus ~]$ ls -lR examples/
examples/:
total 40
-rw-r--r-- 1 simmsben cis90 237 Oct 20 08:27
drwxr-xr-x 2 simmsben cis90 4096 Oct 20 08:27
drwx--xr-x 2 simmsben cis90 4096 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 779 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 151 Oct 20 08:27
examples/
ant
birds
dogs
nursery
twister
examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
examples/dogs:
total 24
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 benji
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 duke
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 homer
[simmsben@opus ~]$
r
read
w
x
write
execute
others
While Duke can't see what is in
that directory he can cd into it!
[roddyduk@opus simmsben]$
[roddyduk@opus examples]$
[roddyduk@opus birds]$ ls
abby nibbie
[roddyduk@opus birds]$ cd
[roddyduk@opus examples]$
[roddyduk@opus dogs]$ ls
ls: .: Permission denied
[roddyduk@opus dogs]$ cat
woof woof woof woof
[roddyduk@opus dogs]$
cd examples/
cd birds
..
cd dogs
homer
Like walking into a pitch black
room where you can't see
anything but you can still do
things
Removing directory r permission – can cd into it, read it's files, just can't list them
CIS 90 - Lesson 8
Directory Write Permission
This table shows the
permissions needed by
different commands to
operate.
fileName1 inodeNum
fileName2 indoeNum
.
.
rwx
rwx
Permission
File
Directory
Read (4)
cat, more, file,
head, tail, cp
ls
Write (2)
vi, saving mail
cp, mv, rm, ln
Execute (1)
$ command
cd, ls -l, find
Removing directory
/ w permission
• can't cp files to it,
• can't remove files,
• can't move files out,
• can't add links
CIS 90 - Lesson 8
Directory Write Permission
r
read
w
x
write
execute
user
r
read
w
x
write
execute
group
r
read
w
x
write
execute
others
[simmsben@opus ~]$ cd examples/dogs/
[simmsben@opus dogs]$ cp duke duke.bak
[simmsben@opus dogs]$ mv homer Homer
[simmsben@opus dogs]$ rm duke
[simmsben@opus dogs]$ ln benji mydog
[simmsben@opus dogs]$ ls -li
total 32
104704 -rw-r--r-- 2 simmsben cis90 20 Oct 20 08:27 benji
104743 -rw-r--r-- 1 simmsben cis90 20 Oct 20 09:24 duke.bak
104684 -rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 Homer
104704 -rw-r--r-- 2 simmsben cis90 20 Oct 20 08:27 mydog
[simmsben@opus ~]$ chmod u-w examples/dogs/
[simmsben@opus ~]$ cd examples/dogs/
[simmsben@opus dogs]$ cp duke.bak /tmp
[simmsben@opus dogs]$ cp duke.bak duke
cp: cannot create regular file `duke': Permission denied
[simmsben@opus dogs]$ mv duke.bak duke
mv: cannot move `duke.bak' to `duke': Permission denied
[simmsben@opus dogs]$ rm duke.bak
rm: cannot remove `duke.bak': Permission denied
[simmsben@opus dogs]$ ln duke.bak /tmp/mydog
ln: creating hard link `/tmp/mydog' to `duke.bak': Invalid cross-device link
[simmsben@opus dogs]$ ln Homer herdog
ln: creating hard link `herdog' to `Homer': Permission denied
Why?
Because filenames are
stored in a directory. cp,
mv, rm and ln commands
need to change filenames,
therefore they need write
access to the directory
Removing directory w permission
• can't cp files into it, can't remove files, can't move files out, can't add links
• can cp files out
CIS 90 - Lesson 8
Directory Execute Permission
This table shows the
permissions needed by
different commands to
operate.
fileName1 inodeNum
fileName2 indoeNum
.
.
rwx
rwx
Permission
File
Directory
Read (4)
cat, more, file,
head, tail, cp
ls
Write (2)
vi, saving mail
cp, mv, rm, ln
Execute (1)
$ command
cd, ls -l, find
/
Removing directory x permission – can't see inode
information (ls –l), or cd into it
CIS 90 - Lesson 8
Directory Write Permission
r
read
w
x
write
execute
r
read
user
w
x
write
execute
group
Benji removes x permission
on his dogs directory
[simmsben@opus ~]$ chmod g-x examples/dogs/
[simmsben@opus ~]$ ls -ld examples/
drwxrwxr-x 4 simmsben cis90 4096 Oct 20 08:27
[simmsben@opus ~]$ ls -lR examples/
examples/:
total 40
-rw-r--r-- 1 simmsben cis90 237 Oct 20 08:27
drwxr-xr-x 2 simmsben cis90 4096 Oct 20 08:27
drwxr--r-x 2 simmsben cis90 4096 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 779 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 151 Oct 20 08:27
examples/
ant
birds
dogs
nursery
twister
r
read
w
x
write
execute
others
Now Duke can only see the
file names there
[roddyduk@opus ~]$ ls -ld ../simmsben/examples/
drwxrwxr-x 4 simmsben cis90 4096 Oct 20 08:27 ../simmsben/exa
[roddyduk@opus ~]$ ls -lR ../simmsben/examples/
../simmsben/examples/:
total 40
-rw-r--r-- 1 simmsben cis90 237 Oct 20 08:27 ant
drwxr-xr-x 2 simmsben cis90 4096 Oct 20 08:27 birds
drwxr--r-x 2 simmsben cis90 4096 Oct 20 08:27 dogs
-rw-r--r-- 1 simmsben cis90 779 Oct 20 08:27 nursery
-rw-r--r-- 1 simmsben cis90 151 Oct 20 08:27 twister
examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
../simmsben/examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
examples/dogs:
total 24
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 benji
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 duke
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 homer
[simmsben@opus ~]$
../simmsben/examples/dogs:
total 0
?--------- ? ? ? ?
?--------- ? ? ? ?
?--------- ? ? ? ?
[roddyduk@opus ~]$
? benji
? duke
? homer
Removing directory x permission – can see files (ls) but no inode information (ls –l)
CIS 90 - Lesson 8
Directory Execute Permission
r
read
w
x
write
execute
r
read
user
w
x
write
execute
group
Benji removes x permission
on his dogs directory
[simmsben@opus ~]$ chmod g-x examples/dogs/
[simmsben@opus ~]$ ls -ld examples/
drwxrwxr-x 4 simmsben cis90 4096 Oct 20 08:27
[simmsben@opus ~]$ ls -lR examples/
examples/:
total 40
-rw-r--r-- 1 simmsben cis90 237 Oct 20 08:27
drwxr-xr-x 2 simmsben cis90 4096 Oct 20 08:27
drwxr--r-x 2 simmsben cis90 4096 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 779 Oct 20 08:27
-rw-r--r-- 1 simmsben cis90 151 Oct 20 08:27
examples/
ant
birds
dogs
nursery
twister
examples/birds:
total 16
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 abby
-rw-r--r-- 1 simmsben cis90 24 Oct 20 08:27 nibbie
r
read
w
x
write
execute
others
Now Duke can only see the
file names there
[roddyduk@opus ~]$ cd ../simmsben
[roddyduk@opus simmsben]$ cd examples/
[roddyduk@opus examples]$ cd birds
[roddyduk@opus birds]$ cd ..
[roddyduk@opus examples]$ cd dogs/
-bash: cd: dogs/: Permission denied
[roddyduk@opus examples]$
[roddyduk@opus examples]$ cat dogs/duke
cat: dogs/duke: Permission denied
[roddyduk@opus examples]$
but not read them
examples/dogs:
total 24
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 benji
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 duke
-rw-r--r-- 1 simmsben cis90 20 Oct 20 08:27 homer
[simmsben@opus ~]$
Removing directory x permission – can't cd into it, or access files inside it.
CIS 90 - Lesson 8
The Sticky Bit
A closer look
at the /tmp
directory
[root@opus
[root@opus
[root@opus
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwx-----drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
dr-xr-xr-x
drwxr-x--drwxr-xr-x
drwxrwxrwx
drwxrwxrwt
drwxrwxrwt
drwxr-xr-x
drwxr-xr-x
/]#
/]#
/]#
2
3
11
98
16
14
2
2
2
2
2
123
21
2
2
2
8
14
26
chmod 777 temp777
chmod 1777 temp777S
ls -ld *
root root
4096 Jun
root root
4096 Jun
root root
3660 Sep
root root
12288 Oct
root root
4096 Jun
root root
4096 Jun
root root
16384 Jun
root root
4096 Jun
root root
0 Sep
root root
4096 Oct
root root
4096 Oct
root root
0 Sep
root root
4096 Sep
root root
12288 Jun
root root
4096 Oct
root root
4096 Oct
root root
4096 Oct
root root
4096 Jun
root root
4096 Jun
17
17
16
21
20
17
16
17
10
10
10
10
17
17
22
22
22
16
17
16:25
15:00
12:59
04:02
11:07
16:22
08:35
15:10
21:48
2006
2006
14:48
17:25
16:25
14:04
13:59
13:52
15:38
22:16
bin
boot
dev
etc
home
lib
lost+found
media
misc
mnt
opt
proc
root
sbin
temp777
temp777S
tmp
usr
var
CIS 90 - Lesson 8
A closer look at the
/tmp directory
Sticky Bit
[root@opus /]# ls -ld t* bin etc
drwxr-xr-x 2 root root 4096 Jun
drwxr-xr-x 98 root root 12288 Oct
drwxrwxrwx 2 root root 4096 Oct
drwxrwxrwt 2 root root 4096 Oct
drwxrwxrwt 8 root root 4096 Oct
[root@opus /]#
17
21
22
22
22
16:25
04:02
14:21
13:59
13:52
bin
etc
temp777
temp777S
tmp
The other directories in / are set to 755 permission. The /tmp is 777 so
anyone can view, create and remove files there
[roddyduk@opus
[roddyduk@opus
[roddyduk@opus
[roddyduk@opus
[roddyduk@opus
simmsric]$ cd /temp777
temp777]$ touch duke
temp777]$ echo hi > benji
temp777]$ rm benji
temp777]$
Without the sticky bit
set, one user can
delete files belonging
to another.
[simmsben@opus
[simmsben@opus
[simmsben@opus
[simmsben@opus
[simmsben@opus
sticky bit not set
simmsric]$ cd /temp777
temp777]$ touch benji
temp777]$ echo hi > duke
temp777]$ rm duke
temp777]$
CIS 90 - Lesson 8
A closer looke at the
/tmp directory
Sticky Bit
[root@opus /]# ls -ld t* bin etc
drwxr-xr-x 2 root root 4096 Jun
drwxr-xr-x 98 root root 12288 Oct
drwxrwxrwx 2 root root 4096 Oct
drwxrwxrwt 2 root root 4096 Oct
drwxrwxrwt 8 root root 4096 Oct
[root@opus /]#
17
21
22
22
22
16:25
04:02
14:21
13:59
13:52
bin
etc
temp777
temp777S
tmp
The other directories in / are set to 755 permission. The /tmp is 777 so
anyone can view, create and remove files there
[roddyduk@opus temp777S]$ touch duke
[roddyduk@opus temp777S]$ echo hi > benji
[roddyduk@opus temp777S]$ rm benji
rm: cannot remove `benji': Operation not permitted
sticky bit set
[roddyduk@opus temp777S]$ rm duke
[roddyduk@opus temp777S]$
[simmsben@opus temp777S]$ touch benji
With the sticky bit set, a
user can delete there own
files but not those belonging
to another.
[simmsben@opus temp777S]$
[simmsben@opus temp777S]$
rm: cannot remove `duke':
[simmsben@opus temp777S]$
[simmsben@opus temp777S]$
echo hi > duke
rm duke
Operation not permitted
rm benji
-rw-rw-r-- 1 simmsben cis90 3 Oct 22 14:27 benji
-rw-rw-r-- 1 roddyduk cis90 3 Oct 22 14:26 duke
CIS 90 - Lesson 8
Hard and Soft Links Forum Posts
CIS 90 - Lesson 8
Groups
CIS 90 - Lesson 8
Groups
[roddyduk@opus ~]$ id roddyduk
uid=1156(roddyduk) gid=103(cis90) groups=103(cis90),100(users)
context=user_u:system_r:unconfined_t
[roddyduk@opus ~]$ groups roddyduk
roddyduk : cis90 users
Excerpt from /etc/passwd
roddyduk:x:1156:103:Duke Roddy:/home/cis90/roddyduk:/bin/bash
simmsben:x:1160:103:Benji Simms:/home/cis90/simmsben:/bin/bash
guest191:x:1161:191:CIS 191 guest account:/home/cis191/guest191:/bin/bash
woolahen:x:1162:103:Henry Woolard:/home/cis90/woolahen:/bin/bash
stanlcha:x:1163:103:Charles Stanley:/home/cis90/stanlcha:/bin/bash
bolasale:x:1164:103:Alexa Bolas:/home/cis90/bolasale:/bin/bash
seatocol:x:1165:103:Collin Seaton:/home/cis90/seatocol:/bin/bash
minvirob:x:1166:103:Robert Minvielle:/home/cis90/minvirob:/bin/bash
wrigholi:x:1167:103:Oliver Wright:/home/cis90/wrigholi:/bin/bash
Excerpt from /etc/group
users:x:100:guest90,jimg,mainart,solommat,villeill,cresszer,simmsmar,roddyduk,simms
ben,woolahen,stanlcha,bolasale,seatocol,minvirob,wrigholi,dymesdia,simmsric,milesfr
e,cafielar,sterlchr,barrecol,ruizdav,gantden,deakifre,bellhil,sinopjam,chaffjan,cla
rkjef,lemiejoh,simmsjon,bobisjoe,rivasjul,smithkay,farramar,talpamar,lemusosc,weave
pat,roneyric,kriewron,smithstu,scalenoa,childtim,husmalei,blackwil,doddkev,lyonsrob
/
,ybarrser,valdemar,elliokat,jessuwes,luisjus,perezave,thrascat,meyerjas,bergelyl,hu
tmabry,gardnnic,mohanchi,whitfbob,wichemic,crivejoh
cis90:x:103:jimg,guest,rsimms
cis191:x:191:jimg,rsimms
CIS 90 - Lesson 8
Groups
Every user is a member of a primary group (shown in
/etc/passwd) and multiple secondary groups (shown in
/etc/group)
[roddyduk@opus ~]$ groups roddyduk
roddyduk : cis90 users
primary
secondary
[roddyduk@opus ~]$ id
uid=1156(roddyduk) gid=103(cis90) groups=100(users),103(cis90)
context=user_u:system_r:unconfined_t
[roddyduk@opus ~]$ touch mydogs
/
[roddyduk@opus ~]$ ls -l mydogs
-rw-rw-r-- 1 roddyduk cis90 0 Oct 20 14:25 mydogs
New files are created using primary group
CIS 90 - Lesson 8
File
Descriptors
CIS 90 - Lesson 8
Input and Output
File Descriptors
Every process is given three open files
upon its execution. These open files are
inherited from the shell
stdin
Standard Input (0)
defaults to the user's keyboard
stdout
Standard Output (1)
defaults to the user's terminal
stderr
Standard Error (2)
/
defaults to the user's terminal
This is file redirection
in a nutshell
CIS 90 - Lesson 8
An instruction for you to handle arrives on your screen
Lets visualize a program
being loaded into memory
and run as a process by the
kernel
The day in a life of a process
CIS 90 - Lesson 8
The program is loaded as a process. It can see the in and out trays. It also
gets any additional options and arguments passed in from the command line
Your job: Take the words from
your in tray, sort them, and place
them in your out tray
Note: You work so hard and fast you never notice the little
trap doors underneath your work trays. Things just magically
appear from somewhere into your in tray and somehow
disappear to who-knows-where from your out trays.
CIS 90 - Lesson 8
You complete your job and are unloaded from memory
Duke
Benji
Star
Homer
<end>
Benji
Duke
Homer
Star
<end>
Note: You grab stuff as fast as you can from your in tray until
it is empty. Your job is to sort them, and placed the sorted
results in the proper out tray. You have no idea where the
work came from or where your results are being sent. All you
can see are the in and out trays.
CIS 90 - Lesson 8
Lets do another example, this time there is a problem
Your job: Take the words from
your in tray, sort them, and place
them in your out tray
Note: You grabs stuff as fast as you can from your in tray until
it is empty. Your job is to sort them, and placed the sorted
results in the proper out tray. You have no idea where the
work came from or where your results are being sent. All you
can see are the in and out trays.
CIS 90 - Lesson 8
Note: You grabs stuff as fast as you can from your in tray until it is empty. Your job is to
sort them, and placed the sorted results in the proper out tray. Unfortunately you are not
cleared to view what is in you in tray. So you place a "open failed – permission denied"
report in the out tray for mistakes
Top
Secret
Uh oh, its top secret
and we can't see the
words inside
Failed! Did not have
permission to view words
CIS 90 - Lesson 8
Ok, lets make the
visualization a little
more realistic
stdout (1)
stdin (0)
stderr (2)
The actual in and out trays have different names as wells
as numbers … stdin (0) stdout (1) and stderr (2).
CIS 90 - Lesson 8
Input and Output
stdout (1)
File Descriptors
Now lets start to show the
connections as "pipes". More
on this later.
stdin (0)
stderr (2)
CIS 90 - Lesson 8
Input and Output
File Descriptors
stdout
Lets replace the little worker with a
box where we can load programs
into to run as a process
input (if
necessary)
is read from
stdin
stdin
stderr
normal
output is
written to
stdout
problems
are
written to
stderr
CIS 90 - Lesson 8
Input and Output
Standard Output (1)
defaults to the user's terminal
File Descriptors
stdout
Finally, lets show the defaults
devices the pipes are attached to.
0
1
2
stdin
Standard Input (0)
defaults to the user's keyboard
stderr
Standard Error (2)
defaults to the user's terminal
CIS 90 - Lesson 8
Input and Output
[simmsben@opus ~]$ sort
star
benji
duke
homer
D
ctrl
benji
duke
homer
star
[simmsben@opus ~]$
File Descriptors
stdout
Options: NA
Args: NA
0
sort
benji
duke
homer
star
1
2
stdin
star
benji
duke
homer
Now lets do an example of the sort
program with no arguments or options
stderr
CIS 90 - Lesson 8
File
Redirection
CIS 90 - Lesson 8
Standard Output (1)
defaults to the user's terminal
stdout
Life would be boring if stdin was always
attached to the keyboard, and stdout and
stderr to the terminal !!
0
1
2
stdin
Standard Input (0)
defaults to the user's keyboard
stderr
Standard Error (2)
defaults to the user's terminal
CIS 90 - Lesson 8
Input and Output
File Redirection
Lets look at the
sort example again
/home/cis90/roddyduk $ sort
duke
benji
star
ctrl
D
homer
benji
duke
End of File
homer
star
/home/cis90/roddyduk $
/
CIS 90 - Lesson 8
Words are read in
from stdin (attached
to keyboard), sorted,
then written to stdout
(attached to
terminal)
Read from stdin
Written to stdout
Input and Output
File Redirection
/home/cis90/roddyduk $ sort
duke
benji
star
D
ctrl
homer
benji
"End of File"
duke
homer
star
/home/cis90/roddyduk $
/
The sort program is loaded. bash assigns stdin to
the keyboard and stdout to the terminal.
CIS 90 - Lesson 2
Example program to process: sort command
/home/cis90/roddyduk $ sort
duke
benji
star
homer
D
ctrl
benji
duke
homer
star
/home/cis90/roddyduk $
/dev/pts/0
stdout
Options: NA
Args: NA
0
sort
benji
duke
homer
star
1
2
/dev/pts/0
duke
benji
star
homer
stdin
Note: It is bash that
sets up the default
input and output
devices. The program
is never even aware of
what is at the end of
the pipes.
stderr
CIS 90 - Lesson 8
But what if we could
tell bash to change
the devices at the
end of the pipes? We
can …
Input and Output
File Redirection
The input and output of a program can be
redirected from and to other files:
0< filename
Input will now come from filename rather than the keyboard.
1> filename
Output will now go to filename instead of the terminal.
2> filename
Error messages will now go to filename
instead of the terminal.
/
>> filename
Output will now be appended to filename.
CIS 90 - Lesson 8
The redirection is specified on the
command line using the syntax
specified below …
Input and Output
File Redirection
The input and output of a program can be
redirected from and to other files:
0< filename
X
Input will now come from filename rather than the keyboard.
1> filename
X
Output will now go to filename instead of the terminal.
2> filename
Error messages will now go to filename instead of the terminal.
>> filename
/
Output will now be appended to filename.
The 0 in 0< is not necessary, just use < to redirect stdin
The 1 in 1> is not necessary, just use > to redirect stdout
The 2 in 2> is necessary, always use 2> to redirect stderr
CIS 90 - Lesson 8
Input and Output
Lets try redirecting
stdout …
File Redirection
Normal output (stdout) is redirected to the file dogsinorder
[simmsben@opus ~]$ sort > dogsinorder
duke
benji
star
D
ctrl
homer
[simmsben@opus ~]$ cat dogsinorder
benji
duke
homer
star
/
[simmsben@opus ~]$
CIS 90 - Lesson 2
Example program to process: sort command
$ sort > dogsinorder
duke
benji
star
ctrl
homer
$
stdout
D
Options: NA
Args: NA
0
sort
dogsinorder
$ cat dogsinorder
benji
duke
homer
star
1
2
/dev/pts/0
duke
benji
star
homer
stdin
stderr
Note: sort doesn't know about keyboard (/dev/pts/0) or
dogsinorder file. It just reads from stdin and writes to stdout.
CIS 90 - Lesson 8
Input and Output
Lets try redirecting stdin
and stdout …
File Redirection
[roddyduk@opus ~]$ cat names
duke
output is redirected to the
benji
input is redirected
file dogsinorder
from the file names
star
homer
[roddyduk@opus ~]$ sort < names > dogsinorder
[roddyduk@opus ~]$ cat dogsinorder
benji
duke
Note: The bash shell handles parsing
homer
and handling the redirection. The sort
star
command never is even aware that
[roddyduk@opus ~]$
the redirection was done.
/
CIS 90 - Lesson 2
Example program to process: sort command
$ sort < names > dogsinorder
stdout
Options: NA
Args: NA
$ cat names
duke
benji
star
homer
0
$ cat dogsinorder
benji
duke
homer
star
1
2
stdin
names
sort
dogsinorder
stderr
Note: sort doesn't know about names or dogsinorder files. It just
reads from stdin and writes to stdout.
CIS 90 - Lesson 8
Input and Output
File Redirection
Now lets try something different.
The difference on the command
line is very subtle. The names file
is now an argument passed to
sort from the command line.
output is redirected to the
file dogsinorder
[roddyduk@opus ~]$ sort names > dogsinorder
[roddyduk@opus ~]$ cat dogsinorder
benji
duke
The sort program is now aware of the
homer
names file. It is the sort programs
star
responsibility now to open the names
[roddyduk@opus ~]$
file and read it. The is done the sort
/
process code making requests to the
kernel to read data from the file on
the hard drive.
CIS 90 - Lesson 2
Example program to process: sort command
$ sort names > dogsinorder
stdout
Note: sort does know
about names file but
doesn't know about
dogsinorder file. It just
reads file names and
writes to stdout.
Options: NA
Args: names
0
sort
dogsinorder
$ cat dogsinorder
benji
duke
homer
star
1
2
read
names
stdin
file contents are read using
the kernel
stderr
CIS 90 - Lesson 8
Input and Output
File Redirection
OK, another little twist, lets pass
in an option as well this time
specifying an option
(for reverse order)
output is redirected to the
file dogsinorder
[roddyduk@opus ~]$ sort -r names > dogsinorder
[roddyduk@opus ~]$ cat dogsinorder
star
homer
This –r option dos the sort in
duke
reverse order
benji
[roddyduk@opus ~]$
/
CIS 90 - Lesson 2
Example program to process: sort command
$ sort -r names > dogsinorder
stdout
Note: sort does
know about names
file but doesn't
know about
dogsinorder file. It
just reads names
file and writes to
stdout. It does see
the option and
modifies how it
sorts.
Options: -r
Args: names
0
sort
dogsinorder
$ cat dogsinorder
star
homer
duke
benji
1
2
read
names
stdin
file contents are read using
the kernel
stderr
CIS 90 - Lesson 8
Input and Output
File Redirection
/dev/pts/0
[roddyduk@opus
duke
benji
star
homer
[roddyduk@opus
[roddyduk@opus
/dev/pts/0
[roddyduk@opus
[roddyduk@opus
~]$ cat names
Note, everything in
UNIX is a file so we can
even redirect to another
terminal
~]$
~]$ tty
~]$ sort names > /dev/pts/1
~]$
/dev/pts/1
[roddyduk@opus ~]$ tty
/dev/pts/1
[roddyduk@opus ~]$ benji
duke
homer
/
star
CIS 90 - Lesson 8
Input and Output
File Redirection
Another example …
[roddyduk@opus
[roddyduk@opus
Hello World
[roddyduk@opus
[roddyduk@opus
Hello World
Hello Universe
[roddyduk@opus
[roddyduk@opus
Oops
[roddyduk@opus
[roddyduk@opus
[roddyduk@opus
~]$ echo "Hello World" > message
~]$ cat message
~]$ echo "Hello Universe" >> message
~]$ cat message
~]$ echo "Oops" > message
~]$ cat message
~]$ > message
~]$ cat message
~]$
>> appends
to the end of
the file
> overwrites
anything already
in the file
CIS 90 - Lesson 2
Example program to process: echo command
$ echo "Hello World" > message
stdout
Note: In this
example echo does
not use stdin. It
gets its input from
the command line
and writes to
stdout which is
redirected to the file
message.
stdin
Options: NA
Args: "Hello World"
0
echo
message
$ cat cat message
Hello World
1
2
stderr
CIS 90 - Lesson 8
Input and Output
Another example …
File Redirection
[roddyduk@opus ~]$ ls -lR > snapshot
ls: ./Hidden: Permission denied
[roddyduk@opus ~]$ head -10 snapshot
.:
total 296
-rw-rw-r-- 1 roddyduk cis90
51 Sep
-rw-r--r-- 21 guest90 cis90 10576 Jul
drwxr-x--- 2 roddyduk cis90
4096 Oct
drwx--x--- 4 roddyduk cis90
4096 Oct
-rw------- 1 roddyduk cis90
484 Sep
drwxrwxr-x 2 roddyduk cis90
4096 Oct
-rw-rw-r-- 1 roddyduk cis90
22 Oct
drwx------ 2 roddyduk cis90
4096 Oct
[roddyduk@opus ~]$
[roddyduk@opus ~]$ ls -lR > snapshot 2>
[roddyduk@opus ~]$ cat errors
ls: ./Hidden: Permission denied
[roddyduk@opus ~]$
Note: errors are written
to stderr, which defaults
to the terminal
24
20
8
8
24
8
20
16
17:13
2001
09:05
09:00
18:13
09:05
10:51
09:17
errors
1993
bigfile
bin
class
dead.letter
docs
dogsinorder
edits
> redirects
stdout to file
named snapshot
2> redirects stderr to
file named errors
CIS 90 - Lesson 2
Example program to process: ls command
$ ls -lR > snapshot 2> errors
Note: In this
example ls does not
use stdin. It gets
its input from the
command line and
the OS (kernel) and
writes to stdout
(redirected to
message) and
stderr (redirected
to errors).
stdout
Options: -lR
Args: NA
0
ls
snapshot
[roddyduk@opus ~]$ head -10 snapshot
.:
total 296
-rw-rw-r-- 1 roddyduk cis90
51 Sep
-rw-r--r-- 21 guest90 cis90 10576 Jul
drwxr-x--- 2 roddyduk cis90
4096 Oct
drwx--x--- 4 roddyduk cis90
4096 Oct
-rw------- 1 roddyduk cis90
484 Sep
drwxrwxr-x 2 roddyduk cis90
4096 Oct
-rw-rw-r-- 1 roddyduk cis90
22 Oct
drwx------ 2 roddyduk cis90
4096 Oct
1
2
24
20
8
8
24
8
20
16
17:13
2001
09:05
09:00
18:13
09:05
10:51
09:17
1993
bigfile
bin
class
dead.letter
docs
dogsinorder
edits
read
stdin
directory contents are read
using the kernel
stderr
errors
$ cat errors
ls: ./Hidden: Permission denied
CIS 90 - Lesson 8
Input and Output
File Redirection
Another example … using all three
Note: bc reads from stdin
[roddyduk@opus ~]$ echo 2+2 > math
which is attached to math
[roddyduk@opus ~]$ bc < math
4
[roddyduk@opus ~]$ echo 4/0 >> math
dividing by zero always
[roddyduk@opus ~]$ cat math
results in an error
2+2
4/0
[roddyduk@opus ~]$ bc < math
4
Runtime error (func=(main), adr=5): Divide by zero
input from math (via
[roddyduk@opus ~]$ bc < math > answers 2> errors
stdin), normal
[roddyduk@opus ~]$ cat answers
output to answers
4
(via stdout) and
[roddyduk@opus ~]$ cat errors
error output to errors
Runtime error (func=(main), adr=5): Divide by zero
(using stderr)
[roddyduk@opus ~]$
CIS 90 - Lesson 2
Example program to process: bc command
$ bc < math > answers 2> errors
Note: Nothing
passed in from the
command line to
bc. Input comes
from math file,
output to answers
file and errors to
errors file
stdout
Options: NA
Args: NA
answers
4
0
bc
1
2
stdin
math
2+2
4/0
stderr
errors
Runtime error (func=(main), adr=5): Divide by zero
CIS 90 - Lesson 8
Input and Output
File Redirection
Introducing the bit bucket
[roddyduk@opus ~]$ find . -name sonnet6
find: ./Hidden: Permission denied
./poems/Shakespeare/sonnet6
[roddyduk@opus ~]$ find /home/cis90 -name sonnet6
find: /home/cis90/guest/.ssh: Permission denied
find: /home/cis90/guest/Hidden: Permission denied
/home/cis90/guest/Poems/Shakespeare/sonnet6
find: /home/cis90/guest/.gnupg: Permission denied
find: /home/cis90/guest/.gnome2: Permission denied
find: /home/cis90/guest/.gnome2_private: Permission denied
find: /home/cis90/guest/.gconf: Permission denied
find: /home/cis90/guest/.gconfd: Permission denied
find: /home/cis90/roddyduk/Hidden: Permission denied
<snipped>
find: /home/cis90/wichemic/class: Permission denied
find: /home/cis90/crivejoh/Hidden: Permission denied
/home/cis90/crivejoh/poems/Shakespeare/sonnet6
[roddyduk@opus ~]$
How annoying is
this?
CIS 90 - Lesson 8
Input and Output
File Redirection
Introducing the bit bucket
bit bucket
[roddyduk@opus ~]$ find /home/cis90 -name sonnet6 2> /dev/null
/home/cis90/guest/Poems/Shakespeare/sonnet6
/home/cis90/roddyduk/poems/Shakespeare/sonnet6
/home/cis90/stanlcha/poems/Shakespeare/sonnet6
/home/cis90/seatocol/poems/Shakespeare/sonnet6
/home/cis90/wrigholi/poems/Shakespeare/sonnet6
/home/cis90/dymesdia/poems/Shakespeare/sonnet6
/home/cis90/lyonsrob/poems/Shakespeare/sonnet6
/home/cis90/ybarrser/poems/Shakespeare/sonnet6
/home/cis90/ybarrser/poems/Sonnets/sonnet6
Much better!
/home/cis90/valdemar/poems/Shakespeare/sonnet6
/home/cis90/elliokat/poems/Shakespeare/sonnet6
/home/cis90/jessuwes/poems/Shakespeare/sonnet6
/home/cis90/luisjus/poems/Shakespeare/sonnet6
/home/cis90/meyerjas/poems/Shakespeare/sonnet6
/home/cis90/bergelyl/sonnet6
/home/cis90/bergelyl/poems/Shakespeare/sonnet6
/home/cis90/gardnnic/poems/Shakespeare/sonnet6
/home/cis90/mohanchi/poems/Shakespeare/sonnet6
/home/cis90/whitfbob/poems/Shakespeare/sonnet6
/home/cis90/crivejoh/poems/Shakespeare/sonnet6
[roddyduk@opus ~]$
CIS 90 - Lesson 8
Pipelines
CIS 90 - Lesson 8
Input and Output
Pipelines
Commands may be chained together in such a
way that the stdout of one command is "piped"
into the stdin of a second process.
Filters
A program that both reads from stdin and writes to stdout.
Tees
A filter program that reads stdin and writes it to stdout and the file
specified as the argument.
For example, the following command sends a sorted list of the
current users logged on to the system to the screen, and saves an
unsorted list to the file users. /
who | tee users | sort
Note, redirection sends output to another file. Pipes
send output to another process
CIS 90 - Lesson 8
Input and Output
Pipelines
Lets count the lines in letter
[roddyduk@opus ~]$ cat letter | wc -l
28
[roddyduk@opus ~]$
CIS 90 - Lesson 2
Example program to process: cat and wc commands
stdout
28
$ cat letter | wc -l
Options: -l
Args: NA
0
wc
1
2
stdout
stdin
Options: NA
Args: letter
0
cat
1
2
read
letter
stdin
file contents are read
using the kernel
stderr
stderr
CIS 90 - Lesson 8
Input and Output
Pipelines
[roddyduk@opus
roddyduk pts/0
roddyduk pts/1
rsimms
pts/2
bolasale pts/4
[roddyduk@opus
[roddyduk@opus
bolasale pts/4
roddyduk pts/0
roddyduk pts/1
rsimms
pts/2
[roddyduk@opus
[roddyduk@opus
4 users
[roddyduk@opus
bolasale pts/4
roddyduk pts/0
roddyduk pts/1
rsimms
pts/2
[roddyduk@opus
~]$ who
~]$
~]$
~]$
~]$
I would like to save a sorted list of users and a
count of how many users are logged on
2008-10-19 18:36 (dsl-63-249-103-107.cruzio.com)
2008-10-19 18:27 (dsl-63-249-103-107.cruzio.com)
2008-10-20 17:33 (dsl-63-249-103-107.cruzio.com)
2008-10-21 10:43 (dsl-63-249-97-17.cruzio.com)
who > tempfile
sort tempfile
2008-10-21 10:43 (dsl-63-249-97-17.cruzio.com)
2008-10-19 18:36 (dsl-63-249-103-107.cruzio.com)
2008-10-19 18:27 (dsl-63-249-103-107.cruzio.com)
2008-10-20 17:33 (dsl-63-249-103-107.cruzio.com)
sort tempfile > users
wc -l users
~]$ cat users
2008-10-21
2008-10-19
2008-10-19
2008-10-20
~]$
10:43
18:36
18:27
17:33
(dsl-63-249-97-17.cruzio.com)
(dsl-63-249-103-107.cruzio.com)
(dsl-63-249-103-107.cruzio.com)
(dsl-63-249-103-107.cruzio.com)
Method I – use temporary files
CIS 90 - Lesson 8
Input and Output
Pipelines
I would like to save a sorted list of users and a
count of how many users are logged on
[roddyduk@opus ~]$ who | sort | tee users | wc -l
4
[roddyduk@opus ~]$ cat users
bolasale pts/4
2008-10-21 10:43 (dsl-63-249-97-17.cruzio.com)
roddyduk pts/0
2008-10-19 18:36 (dsl-63-249-103-107.cruzio.com)
roddyduk pts/1
2008-10-19 18:27 (dsl-63-249-103-107.cruzio.com)
rsimms
pts/2
2008-10-20 17:33 (dsl-63-249-103-107.cruzio.com)
[roddyduk@opus ~]$
Method II – uses pipes
CIS 90 - Lesson 8
Input and Output
Pipelines
[roddyduk@opus
roddyduk pts/0
roddyduk pts/1
rsimms
pts/2
bolasale pts/4
[roddyduk@opus
bolasale pts/4
roddyduk pts/0
roddyduk pts/1
rsimms
pts/2
[roddyduk@opus
4
[roddyduk@opus
4
[roddyduk@opus
bolasale pts/4
roddyduk pts/0
roddyduk pts/1
rsimms
pts/2
[roddyduk@opus
~]$ who
who is logged in now
2008-10-19 18:36 (dsl-63-249-103-107.cruzio.com)
2008-10-19 18:27 (dsl-63-249-103-107.cruzio.com)
2008-10-20 17:33 (dsl-63-249-103-107.cruzio.com)
2008-10-21 10:43 (dsl-63-249-97-17.cruzio.com)
~]$ who | sort
lets sort them
2008-10-21 10:43 (dsl-63-249-97-17.cruzio.com)
2008-10-19 18:36 (dsl-63-249-103-107.cruzio.com)
2008-10-19 18:27 (dsl-63-249-103-107.cruzio.com)
2008-10-20 17:33 (dsl-63-249-103-107.cruzio.com)
~]$ who | sort | wc -l lets sort them and count them
~]$ who | sort | tee users | wc -l lets sort them, save the sorted
~]$ cat users
2008-10-21
2008-10-19
2008-10-19
2008-10-20
~]$
names in users, then count them
10:43
18:36
18:27
17:33
(dsl-63-249-97-17.cruzio.com)
(dsl-63-249-103-107.cruzio.com)
(dsl-63-249-103-107.cruzio.com)
(dsl-63-249-103-107.cruzio.com)
CIS 90 - Lesson 8
Miscellaneous
Commands
CIS 90 - Lesson 8
Input and Output
Miscellaneous Commands
•
•
•
•
•
find – Find file or content of a file
grep – "Global Regular Expression Print"
sort - sort
spell – spelling correction
wc – word count
/
CIS 90 - Lesson 8
find command
find command by itself lists all files from the directory specified and
down into any sub-directories.
[roddyduk@opus poems]$ find
.
./Blake
./Blake/tiger
./Blake/jerusalem
./Shakespeare
./Shakespeare/sonnet1
./Shakespeare/sonnet2
./Shakespeare/sonnet3
./Shakespeare/sonnet4
./Shakespeare/sonnet5
./Shakespeare/sonnet7
./Shakespeare/sonnet9
./Shakespeare/sonnet10
./Shakespeare/sonnet15
./Shakespeare/sonnet17
./Shakespeare/sonnet26
./Shakespeare/sonnet35
./Shakespeare/sonnet11
./Shakespeare/sonnet6
./Yeats
./Yeats/whitebirds
./Yeats/mooncat
./Yeats/old
./Anon
./Anon/ant
./Anon/nursery
./Anon/twister
find command issued
in the poems directory
note: reduced font size
so it will fit on this slide
[roddyduk@opus poems]$
CIS 90 - Lesson 8
find command
How many files are on Opus?
start in / (the top of the file tree)
[roddyduk@opus ~]$ find / 2> /dev/null
154033
[roddyduk@opus ~]$
| wc -l
count the number of lines
throw away permission errors
CIS 90 - Lesson 8
find command
Find files with names starting with sonnet in current home directory
[roddyduk@opus ~]$ find -name "sonnet*"
find: ./Hidden: Permission denied
./poems/Shakespeare/sonnet1
./poems/Shakespeare/sonnet2
./poems/Shakespeare/sonnet3
./poems/Shakespeare/sonnet4
./poems/Shakespeare/sonnet5
./poems/Shakespeare/sonnet7
./poems/Shakespeare/sonnet9
./poems/Shakespeare/sonnet10
./poems/Shakespeare/sonnet15
./poems/Shakespeare/sonnet17
./poems/Shakespeare/sonnet26
./poems/Shakespeare/sonnet35
./poems/Shakespeare/sonnet11
./poems/Shakespeare/sonnet6
[roddyduk@opus ~]$
CIS 90 - Lesson 8
find command
Find sonnet6 files starting in parent directory (/home/cis90)
[roddyduk@opus ~]$ find .. -name "sonnet6" 2> /dev/null
../guest/Poems/Shakespeare/sonnet6
../roddyduk/poems/Shakespeare/sonnet6
../stanlcha/poems/Shakespeare/sonnet6
../seatocol/poems/Shakespeare/sonnet6
../wrigholi/poems/Shakespeare/sonnet6
../dymesdia/poems/Shakespeare/sonnet6
../lyonsrob/poems/Shakespeare/sonnet6
../ybarrser/poems/Shakespeare/sonnet6
../ybarrser/poems/Sonnets/sonnet6
../valdemar/poems/Shakespeare/sonnet6
../elliokat/poems/Shakespeare/sonnet6
../jessuwes/poems/Shakespeare/sonnet6
../luisjus/poems/Shakespeare/sonnet6
../meyerjas/poems/Shakespeare/sonnet6
../bergelyl/sonnet6
../bergelyl/poems/Shakespeare/sonnet6
../gardnnic/poems/Shakespeare/sonnet6
../mohanchi/poems/Shakespeare/sonnet6
../whitfbob/poems/Shakespeare/sonnet6
../crivejoh/poems/Shakespeare/sonnet6
[roddyduk@opus ~]$
CIS 90 - Lesson 8
find command
Find all directories in my home directory and down
[roddyduk@opus ~]$ find . -type d
.
./.mozilla
./.mozilla/extensions
./.mozilla/plugins
./bin
./Hidden
find: ./Hidden: Permission denied
./poems
./poems/Blake
./poems/Shakespeare
./poems/Yeats
./poems/Anon
./olddir
./newdir
./edits
./docs
./etc
./class
./class/labs
./class/exams
./misc
[roddyduk@opus ~]$
CIS 90 - Lesson 8
find command
Find all directories starting in my home directory that start with a
capital B, S, Y or A.
[roddyduk@opus ~]$ find . -type d -name "[BSYA]*"
find: ./Hidden: Permission denied
./poems/Blake
./poems/Shakespeare
./poems/Yeats
./poems/Anon
[roddyduk@opus ~]$
Find all files starting in my home directory that contain town
[roddyduk@opus ~]$ find .
find: ./Hidden: Permission
[roddyduk@opus ~]$ find .
find: ./Hidden: Permission
./edits/small_town
./edits/better_town
[roddyduk@opus ~]$
-name "\*town\*"
denied
-name "*town*"
denied
CIS 90 - Lesson 8
find command
Find ordinary files containing the word bones in the /home directory
$ find /home -mount -type f -exec grep -l "bones" {} \; 2> /dev/null
/home/cis90/simmsben/stash
$
-mount = do not list mount points to other file
systems
- type f = ordinary files. Other types are l
(symbolic link), d directory
CIS 90 - Lesson 8
grep command
Find the wor'd love in Shakespeare's sonnets
[roddyduk@opus poems]$ grep love Shakespeare/son*
Shakespeare/sonnet10:For shame deny that thou bear'st love to any,
Shakespeare/sonnet10:Shall hate be fairer lodg'd then gentle love?
Shakespeare/sonnet10:
Make thee another self for love of me,
Shakespeare/sonnet15:
And all in war with Time for love of you,
Shakespeare/sonnet26:Lord of my love, to whom in vassalage
Shakespeare/sonnet26:
Then may I dare to boast how I do love thee,
Shakespeare/sonnet3:Of his self-love, to stop posterity?
Shakespeare/sonnet3:Calls back the lovely April of her prime,
Shakespeare/sonnet4:Unthrifty loveliness, why dost thou spend
Shakespeare/sonnet5:The lovely gaze where every eye doth dwell
Shakespeare/sonnet9:
No love toward others in that bosom sits
[roddyduk@opus poems]$
CIS 90 - Lesson 8
grep command
find lines with love and hate
[roddyduk@opus poems]$ grep love Shakespeare/son* | grep hate
Shakespeare/sonnet10:Shall hate be fairer lodg'd then gentle love?
[roddyduk@opus poems]$
CIS 90 - Lesson 8
grep command
Find simmsben in /etc/passwd
[roddyduk@opus poems]$ grep simmsben /etc/passwd
simmsben:x:1160:103:Benji Simms:/home/cis90/simmsben:/bin/bash
[roddyduk@opus poems]$
Now show what line it is on
[roddyduk@opus poems]$ grep -n simmsben /etc/passwd
53:simmsben:x:1160:103:Benji Simms:/home/cis90/simmsben:/bin/bash
[roddyduk@opus poems]$
CIS 90 - Lesson 8
grep command
Find the word love in Shakespeare's sonnets
[roddyduk@opus poems]$ grep love Shakespeare/son*
Shakespeare/sonnet10:For shame deny that thou bear'st love to any,
Shakespeare/sonnet10:Shall hate be fairer lodg'd then gentle love?
Shakespeare/sonnet10:
Make thee another self for love of me,
Shakespeare/sonnet15:
And all in war with Time for love of you,
Shakespeare/sonnet26:Lord of my love, to whom in vassalage
Shakespeare/sonnet26:
Then may I dare to boast how I do love thee,
Shakespeare/sonnet3:Of his self-love, to stop posterity?
Shakespeare/sonnet3:Calls back the lovely April of her prime,
Shakespeare/sonnet4:Unthrifty loveliness, why dost thou spend
Shakespeare/sonnet5:The lovely gaze where every eye doth dwell
Shakespeare/sonnet9:
No love toward others in that bosom sits
[roddyduk@opus poems]$
Looking for love in all the wrong places?
CIS 90 - Lesson 8
Pipeline
Tasks
CIS 90 - Lesson 8
Class Exercise
Pipeline Tasks
• Count how many files are on the system
• Count how many users are logged onto the
system
• Sort and count the misspelled words in a
series of documents
• Search for a particular string in the output
of a command
CIS 90 - Lesson 8
Wrap up
CIS 191 - Lesson 8
New commands:
find
grep
sort
spell
tee
wc
find files or content
look for text strings
perform sorts
spell checking
save output to a file
count lines or words in a file
CIS 90 - Lesson 8
Next Class
Assignment: Check Calendar Page on web site to
see what is due next week.
Quiz questions for next class:
• What is the numeric version of rw-rw-r-– that you could
use on a chmod command?
• What command could you use to get an approximate count
of all the files on Opus and ignore the permission errors?
• For the command: sort dognames > dogsinorder
Is sort getting its input from:
a) stdin
b) the command line
c) opening and reading the file dognames
CIS 90 - Lesson 8
Backup
CIS 90 - Lesson 8
Example program to process: bc command
/home/cis90/simmsben $ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software
Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+2
4
0
bc
stdout
1
2
/dev/pts/1
2+2
stdin
Inputs: stdin
Outputs: stdout
stderr
/dev/pts/1
bc 1.06
Copyright 19911994, 1997,
1998, 2000 Free
Software
Foundation, Inc.
This is free
software with
ABSOLUTELY NO
WARRANTY.
For details type
`warranty'.
4
CIS 90 - Lesson 2
Example program to process: ls command
/home/cis90/simmsben/Poems $ ls
ant Blake nursery Shakespeare
/home/cis90/simmsben/Poems $
Inputs: Command
line & Operating
System
/dev/pts/1
twister
Yeats
stdout
Options: NA
Args: NA
Outputs: stdout
0
ls
ant Blake
nursery
Shakespeare
twister
Yeats
1
2
read
$PWD
stdin
current directory contents
are read using the kernel
stderr
CIS 90 - Lesson 8
File Permissions
/home/cis90: drwxr-x--exercise
/home/cis90/simmsben: drwxr-xr-x
/home/cis90/simmsben/Directory1: drwxr-x--x
file1: -rw-rw-r-owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
__read
__read
__read
__execute
__execute
__execute
file2: -rwxr-xr-x
owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
__read
__read
__read
__execute
__execute
__execute
file3: -r-xr-xr-owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
__read
__read
__read
__execute
__execute
__execute
__read
__read
__read
__execute
__execute
__execute
/home/cis90/simmsben/Directory2: drwxrwxr-x
/
file1: -rwxr-xr-x
owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
CIS 90 - Lesson 8
File Permissions
/home/cis90: drwxr-x--/home/cis90/simmsben: drwxr-xr-x
/home/cis90/simmsben/Directory1: drwxr-x—x
file1: -rw-rw-r-owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
__read
__read
__read
__execute
__execute
__execute
file2: -rwxr-xr-x
owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
__read
__read
__read
__execute
__execute
__execute
file3: -r-xr-xr-owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
__read
__read
__read
__execute
__execute
__execute
__read
__read
__read
__execute
__execute
__execute
/home/cis90/simmsben/Directory2: drwxrwxr-x
/
file1: -rwxr-xr-x
owner
group
other
__modify
__modify
__modify
__delete
__delete
__delete
CIS 90 - Lesson 2
banner Good Work | mail –s "Pat on the back " $logname
stdout
28
Options: -l
Args: NA
0
wc
1
2
stdout
stdin
Options: NA
Args: letter
0
cat
1
2
read
letter
stdin
file contents are read
using the kernel
stderr
stderr
Download