IT 244: Introduction to Linux/Unix
Class 8
Tips and Examples
Review
New Material
Studying
Microphone
Homework 4
I have posted Homework 4 here.
As usual, it will be due next Sunday at 11:59 PM.
Quiz 2
I have posted the answers to Quiz 2
here.
Questions
Are there any questions before I begin?
Tips and Examples
Be Sure You Know Where You Are
- Whenever you are working at the command line ...
- you always have a current directory
- It is important that you always know your current directory
- If you think you are in one directory ...
- but you really are in another ...
- your command may not work
- Every semester I get at least one email from a student ...
- saying some file has disappeared
- Linux will never delete a file unless you tell it to
- The file is always somewhere
- But it may not be where you think it is
- Often the file is in your home directory
- When you think a file has disappeared look for it in other directories
- Another problem can arise if you are in the wrong directory
- If you try to save a file and get error message about permission ...
- it means you are in the wrong directory
- The owner of a directory decides who can do what in that directory
- If you are in a directory where you do not have write permission ...
- you won't be able to create anything
- A text editor like
nano
will usually open a file whatever directory you are in
- But you won't be able to save it ...
- unless you have write permission in that directory
- We'll talk about permission in a future class
Finding Errors in Homework Scripts
Review
The Hierarchical Filesystem
- Unix uses a hierarchical filesystem
- This means there is one directory at the top called the
root directory
- The root directory is indicated by a simple slash character, /
- All other directories are contained within the root directory ...
- or one of its many subdirectories
- So the shape of the hierarchical filesystem is like a pyramid
- This structure is called a tree because it looks like a tree turned upside down
- A hierarchical filesystem also resembles a family tree
- So we often use terms that describe family members when talking about directories
- The directory up one level from your current directory is called the
parent directory
- All directories and above the current directory are called
ancestors
- All directories inside the current directories are called
child directories
- All directories inside or below the current directory can be called
descendants
- All directories and files within the same parent directory are called
siblings
Unix Files and Directories
- Files
are linear arrangements of data on a storage device
- There are several types of files
- Program files
- Text files
- Data files
- Configuration files
- Directories are simply containers that hold files and other directories
- On Windows and the Mac they are called folders
Filenames
- When you ask Unix for a file you must give it two pieces of information
- The name of the file
- The location of the file in the hierarchical file system
- Every file has a filename
- The maximum number of characters in a filename varies from one Unix to another
- Most Unix flavors allow file names of up to 255 characters
- It is best to keep filenames short
- This makes typing and remembering them easier
- Never use a space in a file or directory name
- This is a bad idea
- Use an underscore, _ , instead of a space in file names
- To avoid problems, only use the following characters in file names
- Lowercase letters (a-z)
- Digits (0-9)
- Underscore _
- Dash -
- Period .
- You cannot have two files with the same name in the same directory
Case Sensitivity
- Unix is case sensitive
- This means that "Foo", "foo" and "FOO" are three different things
- At least as far as Unix is concerned
- Unix utility and program names are always lowercase
- Some operating systems do not distinguish between UPPERCASE and lowercase characters
- Windows is such a system
- Make life easy for yourself
- Use only lowercase characters in Unix filenames
Filename Extensions
- Extensions are strings of characters that appear at the end of the filename after a period, .
- Extensions are not recognized by the Unix filesystem
- As far as Unix is concerned they are just legal characters that are part of the filename
- Some Unix programs expect their files to have certain extensions
- For example, the C compiler,
gcc
expects the filenames of source code files to end in .c
- The Java compiler,
javac
expects Java source files to have .java at the end of the filename
- The extensions are required by these programs
- But not by Unix
Current Directory
- The way a Unix command works depends on your environment
- One of the most important parts of your environment is your current directory
- The
pwd
(print working directory) command will always tell you your current directory
Your Home Directory
Navigating the Hierarchical File Systems
- Any file or directory in the filesystem will be one of four positions ...
- relative to your current directory
- It can be inside your current directory
- It can be below your current directory
- It can be above your current directory
- It can be off to the side of your current directory
- In this last case, you must go up before you can go down to reach this file
Attendance
New Material
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
- Two special hidden files . and .. appear in every directory
$ ls -a
. .. hw3 work
- We'll discuss them shortly
Startup Files
- The files .login and .bash_profile are
startup files
- Startup files contain Unix commands ...
- that are run just before you get the first shell prompt
- They customize your Unix environment
- They only work when placed inside your home directory
- They can set variables which can help you with your work
- We'll talk more about this in a future class
The . and . . Directory Entries
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
- A pathname is like an address on a letter
- A name and directions to get there
- The name of the file is always at the end of a pathname
- What comes before the filename is the path to the directory that holds the file
- A path is the list of
directories you must go through to get to the file
- A slash, / , at the beginning of a pathname
- Refers to the root directory
- Other slashes separate the directory name from whatever follows it
- There are two types of pathnames
Absolute Pathnames
- The directory at the top of the filesystem is called the root directory
- The root directory is represented by a single slash, /
- 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 holds the file
- When you add the filename to the end of an absolute path ...
- you have an
absolute pathname
- The absolute pathname of my startup file, .bash_profile ...
- in my home directory ...
- is /home/ghoffman/.bash_profile
- This means that my home directory, ghoffman ...
- is under the directory named home ...
- and this directory is under the root directory, /
- The advantage of an absolute pathname ...
- is that it can be used from any part of the filesystem
- It does not depend on where you are at a given moment
- The disadvantage is that absolute pathnames tend to be long
- It is easy to make a mistake when typing an absolute pathname
- An absolute pathname always begins with either a slash, /
or a tilde, ~
Tilde, ~, in Pathnames
Relative Pathnames
- Absolute pathnames are useful because you can use them anywhere
- But they are long and easy to mistype
- 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 directory, /
- The only difference between an absolute and a relative path is where they start
- An address on a letter is like an absolute pathname
- If you address a letter to me at
Glenn Hoffman
40 Central Street
Somerville, Massachusetts 02143
USA
you can mail it to me anywhere in the world
- It will get to me
- If someone asked me the directions to the bathrooms room from the my office
I would say
Go out the door
Turn right
Walk until you come to a corridor on your left
Turn left into the corridor
The bathroom doors will be on the left
- These directions only work from the my office and a few others
- This is like a relative path
- While all absolute pathnames start with a slash, /, or a tilde,
~ ...
- relative pathnames never do
- If the first character is not a slash, /, or a tilde, ~,
it is a relative pathname
- As far as Unix is concerned it makes no difference ...
- whether you use and absolute or relative pathname
- Relative pathnames are more convenient and are most often used
- 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 of your current directory
Relative Pathnames in Your Current Directory
Relative Pathnames in a Subdirectory
Relative Pathnames above the Current Directory
- Every directory has a . . entry
- . . refers to the directory immediately above the directory
- If I wanted to list the contents of the parent directory I would use
ls ..
- You can use many . . entries to refer to a directory several levels up
- Use one . . for each directory up the chain of directories ...
- with a / 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 directory ...
- and then go down to the directory that holds the file
- Use as many . . as you need to get to a common ancestor directory
- The common ancestor directory is the first directory ...
- that both your current directory and the other directory have in common
- Once you get to the common ancestor ...
- you go down to the directory that holds the file
Standard Directories
- In the early days of Linux each distribution stored its important files in different directories
- This made it hard to document programs
- It also made things hard for developers working with different flavors of Linux
- Fortunately, reason has since prevailed
- We now have something called the FHS
- It stands for Linux Filesystem Hierarchy Standard
- It has been adopted by most major Linux distributions
- The standard does not say where to find different kinds of files
- Instead it says that if you have a certain directory ...
- what kind of files you should see in that directory
- Most Linux distributions do not use all these directories
- But when they do, they use them for the type of files specified by the standard
Your Home Directory versus /home
- Most Unix accounts have a home directory
- But not all
- The Ubuntu command that creates an account ...
- has an option to not create a home directory
- On most Linux systems home directories are contained in the directory
/home
- This is directly under the root directory
- Which is written as /
- On the Mac, home directories are contained in /Users
- Also directly under the root directory
- Do not confuse /home with your home directory
The Two Uses of /
- / is used in two different ways when writing pathnames
- When / appears at the beginning of a pathname ...
- it refers to the root directory
- The directory at the top of the filesystem
- The root directory is the only directory ...
- that is not contained in another directory
- Here is an example of this use of /
ls /home
- When the / is not at the beginning
of a pathname it is used a separator
- A character to mark where a director name ends ...
- and another name begins
- Here is an example of this use of /
ls it244/hw
- This is the relative pathname of the hw directory
inside the it244 directory
- A pathname can use the / in both ways
ls /home/ghoffman
- Here the first / means the root ...
- but the second is used to separate the home
from ghoffman
The Two Special Uses of .
Studying
Class Exercise
Class Quiz