IT 244: Introduction to Linux/Unix
Class 9
Tips and Examples
Review
New Material
Microphone
Questions
Are there any questions before I begin?
Homework 5
I have posted homework 5 here.
It is due this coming Sunday at 11:59 PM.
Tips and Examples
Mistakes That Make Your Scripts Hard to Score
- I wrote the
Rules for Homework Scripts
to make the time I spend grading your scripts as efficient as
possible
- They may seem arbitrary but they make it easier for me to score
your scripts
- Whenever a student submits a script that violates the rules ...
- it causes me extra work
- That is why you lose points if you don't follow the rules
- Most scripts I have seen follow these rules
- But there are certain common mistakes that will cause you
to lose points
- The rule that causes me the most trouble when it is ignored is
this
Testing Your Class Exercises
A Subtle Pathname Problem
- Several semesters ago a student came to me with a problem that took
me a few minutes to unravel
- It all had to do with the absence of one character in a pathname
- The student was trying to go to his it244
directory using the command
cd ~/it244
- Once there he tried to create a file using the
touch command ...
- but he got a permission error
- After staring at his prompt for a few minutes I realized what was
happening
- The student thought he had entered
cd ~/it244
- Which would have put him in his it244 directory
- What he really typed was
cd ~it244
- This command took him to the home directory of the it244 account, /home/it244
- This account was created by Prof. Bo Sheng, who used to teach this course
- It is a dummy account that he uses for teaching and testing ...
- like my it244gh account
- The only account that can create a file in this directory is it244 ...
- Prof. Sheng's test account
- Here again a single character was the source of the problem
A Trick When Creating a Long Pathname
- I often make mistakes when typing
- So when I have to create a long pathname ...
- I often get it wrong
- To deal with this I came up with a way to use
ls ...
- to slowly build the correct pathname
- Let's say I'm in the directory holding test scripts for IT 244 ...
- and I want to copy the file five_lines.txt ...
- in my it244_files directory
- I know that the file is somewhere under my home directory ...
- So I'll start with
$ ls ~
bin drop_box it116 messages
bugs f24 it116_test public_html
code ghoffman it117 stdwrk
course_files html it117_test submitted
course_materials hw1_piazza_filename_create.py it244 tests_taken
course_scoring_documents hw99.sh it244_test tmp
demos issues mail wrk
- The directory I'm looking for is under the
course_files directory
- So I use the ↑ key to retrieve my previous command ...
- then add course_files at the end
$ ls ~/course_files/
it116_files it117_files it244_files it245_files
- The file I want is in the it244_files
directory
- So I add this directory to the previous command ...
- again using ↑
$ ls ~/course_files/it244_files/
- The output of this command is very long but I can see the
five_lines.txt file in the listitng
- So I repeat this procedure to get the pathname to this file
$ ls ~/course_files/it244_files/five_lines.txt
/home/ghoffman/course_files/it244_files/five_lines.txt
- Now that I have the correct pathname ...
- I can use command line editing to turn the previous command ...
- into call to
cp I need
- I use ↑ to get the previous command ...
- then use Control A to go to the start of the line ...
- where I change
ls to cp
- Then I use Control E to go to the end of the line ...
- where I add . (dot) after a space
- This gives me
$ cp ~/course_files/it244_files/five_lines.txt .
Review
Hidden Filenames
- A file whose filename begins with a period, . , is a "hidden" or "invisible" file
ls does not display these files unless you use the -a option
- These files are used to configure your Unix environment
Startup Files
- Files like .login and .bash_profile
are located in your home directory
- They are startup files
- Startup files contain Unix commands that are run before you get the first shell prompt
- These commands customize your Unix environment
- You can set Unix variables which can help you with your work ...
- and do other things
The . and .. Directory Entries
- Every directory has at least two entries . and ..
- When a new directory is created these are the first two entries
- . stands for the current directory
- . . stands for the parent directory of your current directory
- . . is the directory immediately above your current location
- . is most often used in two circumstances
- To run a program in your current directory
- To move or copy a file to your current directory
Pathnames
- Every file has a
pathname which is used to access the file
- A pathname has two components
- A path to reach the file
- The name of the file
- The path is a list of directories that you must go through ..
- to reach the file you want
- A path is like an address on a letter ...
- or directions to get somewhere
- The name of the file is always at the end of a pathname
- When the slash, / appears between
names in a pathname ...
- it is a separator
- It is used to separate a directory name from what comes after it
- When a / is the first character in a pathname
- it stands for the
root directory
- There are two types of pathnames
Absolute Pathnames
- The top of the filesystem is a directory called the root directory
- The root directory is represented by a single slash character, /
- It can stand alone or appear as the first character before a directory name
- An absolute path
is a list of directories starting with the root directory ...
- and ending with the directory that contains the file
- When you add the filename to the end of an absolute path ...
- you have an
absolute pathname
Tilde, ~, in Pathnames
Relative Pathnames
- Absolute pathnames are useful because you can use them anywhere
- But they are long and easy to mistype
- For most purposes, it is easier to use
relative pathnames
- In a relative pathname, the path starts from your current directory
- In an absolute pathname, the path starts from the root, /
- While all absolute pathnames start with a slash, /,
or a tilde, ~
- Relative pathnames never do
- As far as Unix is concerned ...
- it makes no difference whether you use an absolute or relative pathname
- There are four types of relative pathnames:
- When the file is in your current directory
- When the file is in a subdirectory of your current directory
- When the file is in a directory that is above your current directory
- When the file is in a directory that is neither above or below the current directory
Relative Pathnames in Your Current Directory
- A relative pathname of a file or directory in your current directory is simple
- It is the name of that file or directory
Relative Pathnames in a Subdirectory
- Things get a little more complicated ...
- when you are dealing with a file in a subdirectory
- Here, you must list every directory ...
- between your current directory and the file you want
- You must use a slash, / after each directory name
Relative Pathnames above the Current Directory
- When the file or directory is above the current directory ...
- you can't list the directory names
- Instead, you have to use the special . . entry in each directory
- Use one . . for each directory up the chain in the path
- Use a slash, /, between each ..
Relative Pathnames Neither above Nor below the Current Directory
- What if the file is neither above nor below?
- Here you have to go up to a common ancestor ...
- and then down to the directory that holds what you want
- The path starts with one or more . . ...
- to get to the directory that is the common ancester of your
current directory ...
- and the directory holding the file you are trying to reach
- Once you get to the common ancestor ...
- you go down to the directory that holds the file
Attendance
New Material
Access Permissions
- All Unix files and directories have
access permissions
- The access permissions allow the
owner
of a file or directory ...
- to decide who gets to do what with the file or directory
- By default, the owner of a file or directory is the account that created it
- Every file, directory or device on a Unix filesystem ...
- has three types of permissions
- If you have read permission
you can look at the data in the file
- You can run
cat, more or less on these files
- If you only have read permission on a file ...
- you cannot change it
- To change what is inside a file you need
write permission
- To run a program file you must have
execute permission
- If you make a Bash script executable ...
-
you can run it without having type
bash before the name of the script
- Each of the three types of permissions is either on or off
- But the access permissions apply to three types of users
- The owner
- The group
- Every other Unix account
- Every file or directory has an owner and a group assigned to it
- The account that created the file is usually the owner
- A group
is a collection of Unix accounts
- The last class of users ...
- is any account that is not the owner or a member of the group
- Unix calls this class of users "other"
Groups
- A group is just a collection of Unix account names
- For example there is a group called faculty ...
- that contains Unix accounts of all instructors in the CS department
- An individual account can be a member of more than one group
- To see the groups that my account belongs to ...
- I can use the
group command
$ groups
faculty itclub curcom oversight it-faculty it116-1 it116-1G it117-1 it117-1G it244-2 it244-2G
- To see the accounts belonging to a specific group ...
- you run the
group command
$ group it-faculty
msolah
tiago
teebanj
apotaszn
it244gh
ckelly
ghoffman
stran
tsoro
zyang
olivia93
- On most Linux systems when a new account is created ...
- a new group is also created for that account
- That group has the same name as the account
- At first this group only contains the username of the account
- When the user creates a new file or directory ...
- this special group is assigned to it
- The owner can change the group assigned to a file
- But they cannot change what accounts are in the group
- Only the system administrator can add or remove account from a
group
- Our Unix machines do something different
- All students accounts are assigned the default group ugrad
- And all faculty are assigned the default group faculty
- This group will be assigned to any file or directory I create
$ ls -l ~/tmp
total 4
drwxrwxr-x 2 ghoffman faculty 4096 May 3 15:59 dir1
-rw-rw-r-- 1 ghoffman faculty 0 May 3 15:59 foo.txt
Viewing Access Permissions
chmod
Using chmod with Numeric Arguments
- The numeric permissions format uses three digits
- Each digit is a number from 0 to 7
- The first digit gives the permissions of the owner
- The second digit gives the permissions assigned to the group
- The third digit gives the permissions for every other account
- Each of these classes of users ..
- must be assigned values for read, write and execute permissions
- How do you get three pieces of information out of one number?
- By adding powers of two
- Each digit is the sum of three other numbers
- When calculating a permission number you start with 0
- Then add
- 4 if you want to give read permission
- 2 if you want to give write permission
- 1 if you want to give execute permission
- Notice that all the number are powers of two
- If we write these values in binary notation
- 100 represents 4
- 010 represents 2
- 001 represents 1
- A single decimal digit from 0 to 7 is represented by 3 binary digits
- This is how we get three pieces of information out of one digit
- For example, to give full permissions I would add
- 4 for read permission
- 2 for write permission
- 1 for execute permission
- So the total, 7, grants all three permissions
- 7 is 111 in binary
- Let's look at some other digits
- 6 in binary is 110
- The leftmost digit is 1 indicating read permission
- The center digit is 1 indicating write permission
- The last digit is 0 indicating that execute permission is not granted
- 5 in binary is 101
- The first digit is 1 so read permission is granted
- The second digit is 0 so write permission is not granted
- The last digit is 1 so execute permission is granted
- The following table shows the permissions granted
- by each of the digits from 0 to 7
| # | ls -l | Permissions |
| 0 | --- | None |
| 1 | --x | Execute only |
| 2 | -w- | Write only |
| 3 | -wx | Write and execute |
| 4 | r-- | Read only |
| 5 | r-x | Read and execute |
| 6 | rw- | Read and write |
| 7 | rwx | Read, write, and execute |
- Only a few of these combination are commonly used ...
- like
rwx and r--
- This scheme is confusing when you first encounter it ...
- but it becomes easier as you use it
- Try to remember this chant
4 2 1
read write execute
owner group everyone
- The first line contains the numbers added to 0 ...
- to form the permissions digit
- The second is the type of permission
- And the third is the category of users
- Repeat this to yourself several times and it should sink in
- You must call
chmod with three digits
- Let's look at some examples
- When you create a new file ...
- it will have certain default permissions
$ touch foo.txt
$ ls
foo.txt
$ ls -l
total 0
-rw-r--r-- 1 it244gh libuuid 0 2012-02-09 15:51 foo.txt
- The owner can read and write the file, but not execute it
- The group and everyone else can only read the file
- To make the file unreadable to everyone except the owner, run
$ chmod 600 foo.txt
$ ls -l
total 0
-rw------- 1 it244gh libuuid 0 2012-02-09 15:51 foo.txt
- To change the file back to its default permissions, run
$ chmod 644 foo.txt
$ ls -l
total 0
-rw-r--r-- 1 it244gh libuuid 0 2012-02-09 15:51 foo.txt
- Here are some common arguments to
chmod ...
- along with what you will see if you run ls -l
| Digits | ls -l |
| 600 | rw------- |
| 644 | rw-r--r-- |
| 664 | rw-rw-r-- |
| 666 | rw-rw-rw- |
| 755 | rwxr-xr-x |
| 777 | wxrwxrwx |
chmod Practice
Class Exercise
Class Quiz