Download CS21Lecture3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Addition wikipedia , lookup

Transcript
Introduction to Unix – CS 21
Lecture 3
Lecture Overview




Lab review
More on files and listings
Disk usage and compressing files
Permissions and ownership
How Did Lab Go?


What was missing?
What did I want you to get out of it?


1. Basic differences between Windows and
Unix
2. Difference between a good design and a
bad design

A, b, x don’t really make good descriptions
Lab Continued






3. Getting used to navigating the system
4. rmdir can only remove empty directories
5. A lot of environment variables are
automatically set, but not every one will be
used every time
6. Difference between an environment
variable and a local variable
7. Get used to reading man pages
8. Think about the future
What ls Output Means
Properties Of Files

All files have these properties


Permissions
Links






For directories, this lists the number of subdirectories
Owner
Owner group
Size
Timestamp
Name
Disk Space And Quota

Every file takes some amount of space,
even an empty file


Space for the name, and information about
the file
Your account has limited space


quota can be used to check available space
Du shows how much space you are using
and where it is located

“du –s” summarizes and just gives you the total
The du Command
Saving Disk Space

Compressing files


Zip
gzip


bzip2


GNU version of zip
Different algorithm
Decompressing files

unzip, gunzip, bunzip2
Running zip, gzip, And bzip2
A Special Type of File: A
Symbolic Link


Not an actual file, but a pointer to
another file
Acts as a shortcut



Can act as a shortcut to a directory
Provides a quick link to any file
Often used so files can be changed without
disturbing other programs
Graphical Representation
/home/csmajs/user
/home/csmajs/user/cs21
/home/csmajs/user/cs21/link
/home/csmajs/user2/otherFile
Creating Symbolic Links With
ln

ln –s TARGET NAME



-s signifies a symbolic link
Without the –s, a hard link is created
Difference between a hard link and a
symbolic link?

A hard link’s target must exist

A symbolic link can point to nothing (broken
link)
Overview Of Permissions
-rwxrwxrwx
File Type
rwx
rwx
rwx
Owner
Group
World
Owner, Group, The World

Owner


Group


The creator of the file
A set of users grouped together
The world

Every other account not in the group
Read, Write, Execute

Read


Write


File can be read, but not modified
Permission is granted to modify the file
Execute


Run directly as if the file is a program
All programs should be executable (/bin)
What Permissions Mean On A
Directory

Read


Write


Users can get a listing of that directory
Users can create and remove files in that
directory
Execute

Users can examine files in that directory
Changing Permissions With
chmod
chmod SETTINGS FILENAME
u = user
+ (add)
r = read
g = group
- (remove)
w = write
o = other
= (set)
x = execute
a = all
chmod a-x testFile
More Examples

Set read and write access for all?


Add executable access for others?


chmod a=rw FILE
chmod o+x FILE
Remove all access for owner?

chmod u-rwx FILE
Advanced chmod Usage


Most Unix hackers don’t use this form
They prefer the more direct approach


Set permissions for owner, group, and
others all with one number
Unfortunately, this approach requires a
little bit of information
How Decimal Numbers Work

1234



10 distinct values


1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1
3
2
1
0
1 * 10 + 2 * 10 + 3 * 10 + 4 * 10
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Why is this system so comfortable?
Binary Numbers

Only two distinct values



0 and 1
On and off
Binary numbers work the exact same
way as decimal numbers
Conversion

1010
3




1
1*2+0*2+1*2+0*2
8 + 0 + 2 + 0 = 10
111

2
2
1
0
1*2+1*2+1*2
4+2+1=7
0
Powers Of Two
x
2
0
2
1
2
2
2
23
4
2
5
2
6
2
Decimal Value
1
2
4
8
16
32
64
Octal Numbers

Eight distinct numbers


0, 1, 2, 3, 4, 5, 6, 7
Binary numbers can be split into groups
of three and represented by an octal
number exactly
Conversion Table
Binary Number
Octal/Decimal Number
000
0
001
1
010
2
011
3
100
4
101
5
110
6
111
7
Relationship Between Binary
Numbers And Permission







Each permission can be represented by a
binary number
Each slot is either on or off
--- = 000
rwx = 111
rw- = 110
r-x = 101
Each group of three corresponds exactly to
an octal number
Conversion Examples

Read, write, and execute permission for
everybody?


Read and execute permission for everyone?


rwx rwx rwx = 111 111 111 = 777
r-x r-x r-x
= 101 101 101 = 555
Read permission for owner, write permission
for group, and execute permission for
everyone else?

r-- -w- --x
= 100 010 001 = 421
More Examples

All permission for owner, read and
execute for everyone else?


rwx r-x r-x = 111 101 101 = 755
Read, write for owner, and read only for
everyone else?

rw- r-- r-- = 110 100 100 = 644
Back To chmod

The advanced way to use chmod is to
use octal numbers and set all
permissions at once
chgrp And chown

chgrp will change the group setting of a
file to another that you are a member
of


You probably don’t belong to more than
one group
chown will change the owner of a file

Only the owner may chown a file

Once chown’ed, the new owner is the only one
who can grant permission back
Using umask To Set Default
Permissions



Every time you create a file, the
permissions are set to a default
umask will set this default
Unfortunately, it does it the exact
opposite way than you would think
How umask Works




If you want default permissions to be
770, you set your umask with the
opposite: 007
Permissions = 770 = 111 111 000
umask 007
= 000 000 111
Unix tries to be helpful and clears the
executable flags, though
Examples

Read, write, execute permission for
owner, nothing for anyone else?



Permissions = 700 = 111 000 000
umask 077
= 000 111 111
All permissions for owner and read,
write for group?


Permissions = 760 = 111 110 000
umask 017
= 000 001 111
More Examples

Read and write permissions for
everybody?



Permissions = 666 = 110 110 110
umask 111
= 001 001 001
No permissions for anybody?


Permissions = 000 = 000 000 000
umask 777
= 111 111 111
A Couple Simple Commands

echo


Simply prints out whatever it is given
Example:
Using echo To Make A Simple
File
Timestamps

touch command


Without any flags, it will make the
modification time of that file the current
time
Can be used to create a new file, but the
file will be empty (0 byte size)
Final Notes For Today


We have covered up through chapter 5
in the book, so you will be expected to
have read these chapters
Next time we will delve deeper into
creating files and moving them around
in the file system