Donation?

Harley Hahn
Home Page

Send a Message
to Harley


A Personal Note
from Harley Hahn

Unix Book
Home Page

SEARCH

List of Chapters

Table of Contents

List of Figures

Chapters...
   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26

Glossary

Appendixes...
  A  B  C
  D  E  F
  G  H

Command
Summary...

• Alphabetical
• By category

Unix-Linux
Timeline

Internet
Resources

Errors and
Corrections

Endorsements


INSTRUCTOR
AND STUDENT
MATERIAL...

Home Page
& Overview

Exercises
& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Exercises and Answers for Chapter 24...

Working With Directories

Review Question #1:

What is a pathname?

What is the difference between an absolute pathname and a relative pathname?

Answer

A pathname is a description of the location of a file within the directory tree.

There are two types of pathnames: absolute pathname start with a / (slash) character; relative pathnames do not start with a /.

An absolute pathname specifies the full name of every directory, from the root directory to the actual file.

A relative pathname is interpreted as starting from the working (current) directory.

Review Question #2:

What is the working directory? What is another name for it?

Which command displays the name of your working directory? Show another way to display the same information.

Which three commands are used to change your working directory?

Suppose you want a constant reminder of the name of your working directory. How would you arrange that? Hint: Think about your shell prompt.

Answer

The working directory is the default directory, used when entering Unix commands. It is also called the current directory.

The pwd (print working directory) command displays the name of your working directory. You can also use echo $PWD (see Chapter 12).

To change your working directory, you can use the following commands:

cd change directory
pushd push directory
popd pop directory

To keep track of your working directory:

• If you are using a GUI-based terminal windows, it will probably display the name of your working directory in the title bar.

• Alternatively, you can display the name of your working directory in your shell prompt (see Chapter 13).

Review Question #3:

What program do you use to list the names of the files in a directory?

Which options do you use to display:

• All files, including dotfiles (hidden files)
• Names in reverse alphabetical order
• Entire tree, including all subdirectories
• Flag after each name (/ = directory)
• Size of each file, in human readable units
• Long listing (permissions, owner, and so on)
• Information about a directory itself, not its contents

When you look at a directory listing, what do the entries . and .. mean?

Answer

Use the ls program to list the files in a directory.

The options to use are:

-a All files, including dotfiles (hidden files)
-r Names in reverse alphabetical order
-R Entire tree, including all subdirectories
-F Flag after each name (/ = directory)
-hs Size of each file in human readable units
-l Long listing (permissions, owner, and so on)
-d Information about a directory itself, not its contents

Within a directory listing:

. refers to the directory itself
.. refers to the parent directory

Review Question #4:

What is globbing?

What are wildcards?

What are the five different wildcards? What does each one match?

How is globbing different from using regular expressions?

Answer

Globbing is an operation in which a wildcard pattern is replaced by a list of matching filenames, typically within a command that is processed the shell.

Wildcards are metacharacters, used when globbing, to create a pattern that can match multiple files.

The five wildcards are:

* match any sequence of zero or more characters
? match any single character
[list] match any character in list
[^list] match any character not in list
{string1,string2...} match one of the specified strings

Globbing and regular expressions both use metacharacters to match patterns. However, globbing is relatively simple and is used only to match filenames. Regular expressions are much more complex and are used in a large variety of applications to match many different types of patterns.

Review Question #5:

What program do you use to draw a diagram of a directory tree?

Which is the most useful option for this program?

Answer

To draw a diagram of a directory tree use the tree program. This program is available with Linux, but not with most types of Unix.

The most useful option is -d, which displays directories only.

Applying Your Knowledge #1:

Starting from your home directory, using as few commands as possible, create the following directories :

temp/
temp/books/
temp/books/unix/harley
temp/books/literature/
temp/books/literature/english/
temp/books/literature/english/shakespeare
temp/books/literature/english/shakespeare/hamlet
temp/books/literature/english/shakespeare/macbeth

Display a diagram of the directory tree starting from temp that shows only directories. (Note: You may not have the program to do this if you are not using Linux.)

Answer

You can create all 8 directories with only 3 commands:

mkdir -p temp/books/unix/harley
mkdir -p temp/books/literature/english/shakespeare/hamlet
mkdir temp/books/literature/english/shakespeare/macbeth

To display the directory tree (with Linux), showing only directories, use:

tree -d temp

The output is:

temp
`-- books
|-- literature
| `-- english
| `-- shakespeare
| |-- hamlet
| `-- macbeth
`-- unix
`-- harley

Applying Your Knowledge #2:

Starting with the directories from the the previous question, and using commands that are as short as possible, create the following empty files.

(Hint: Use the touch command to create the files. It is explained in Chapter 25, and there is an example in this chapter.)

• In the harley directory: create notes, questions, answers.
• In the english directory: create essay, exam-schedule.
• In the hamlet directory: create quotes.

Display a diagram of the directory tree starting from temp showing all files and directories.

Answer

To create the empty files, you can use:

cd ~/temp/books/unix/harley
touch notes questions answers
cd ../../literature/english/shakespeare/hamlet
touch quotes
cd ../../
touch essay exam-schedule

To display the directory tree (with Linux), showing directories and files, use:

tree ~/temp

The output is:

`-- books
|-- literature
| `-- english
| |-- essay
| |-- exam-schedule
| `-- shakespeare
| |-- hamlet
| | `-- quotes
| `-- macbeth
`-- unix
`-- harley
|-- answers
|-- notes
`-- questions

Applying Your Knowledge #3:

Clear the directory stack.

Now push the following two directories onto the stack:

~/temp/books/unix/harley
~/temp/books/literature/english/

Display the contents of the stack so that each element is on a separate line with a line number.

Using the stack, change to the harley directory.

Answer

Clear the directory stack:

dirs -c

Push the two directories onto the stack:

cd ~/temp/books
pushd unix/harley
pushd ../../literature/english/

Display the contents:

dirs -v

Change to the harley directory:

pushd +1

Applying Your Knowledge #4:

Create two versions of a command to display the access times of all the files in your working directory with a name that consists of the characters "backup", followed by a single character, followed by 2 digits. The first command should use predefined character classes. The second command should use ranges.

Answer

Using predefined character classes:

ls -lu backup?[[:digit:]][[:digit:]]

Using ranges:

ls -lu backup?[0-9][0-9]

For Further Thought #1:

You are teaching a Unix class, and it is time to explain how to create directories and how to think about the working directory. You have two choices.

First, you can explain the concepts abstractly: we create directories as needed, and we use the cd command to change the value of the working directory.

Alternatively, you can introduce the metaphor of a tree: directories are like branches of a tree, and using cd moves us from one branch to another.

Which approach do you think is better? Why?

Answer

With computer science majors or programming students, it is best to use abstract explanations, because the concepts that are fundamental to the students' education. For such students, it is worth taking a bit more time to ensure they truly understand what they are doing and why.

With students who are learning Unix only as a vehicle for doing work, it is better to give them a metaphor that is easy to understand quickly. Such students have no compelling need to understand why the directory system works as it does. They are only interested in (and rightly so) the procedures necessary to get their work done. In the years to come, when such students have problems that are beyond their capabilities, they can always ask for help from someone who does understand the abstract concepts.

In other words: everyone doesn't have to know everything.

For Further Thought #2:

To display file types, you can use either ls -F and file. Why is it necessary to have two such commands?

Answer

Most of the time, ls -F shows you what you need to know. Having -F as an option for ls is particularly useful, as you can use it as a default when listing your files.

The file command is for those few times when you need more detailed information about a file.

Jump to top of page