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 25...

Working With Files

Review Question #1:

Which command do you use to create a brand new, empty file? Why do you rarely need to use this command?

Give three common situations in which a file would be created for you automatically.

Answer

To create a brand new, empty file, you use the touch command. You rarely need to use touch, because Unix creates files for you automatically as the need arises.

Three common situations in which this happens are:

• You use a program that needs to create a file (for example, when you edit a new file with vi)

• You redirect standard output to a file that does not already exist

• You copy a file to a target file that does not already exist

Review Question #2:

Examine each of the following character strings and decide whether or not it would make a good filename. If not, explain why.

data-backup-02
data_backup_02
DataBackup02
DATABACKUP02
Data Backup 02
Data;Backup,02
databackup20
data/backup/20

Answer

Strictly speaking, when you choose a filename, the only rules you must follow are:

• The name must be no more than 255 characters
• The name must not contain a / (slash) character.

For practical purposes, however, it is best to use only the following characters:

• Lowercase letters (a, b, c...)
• Uppercase letters (A, B, C...)
• Numbers (0, 1, 2...)
• Dot (.)
• Hyphen (-)
• Underscore (_)

For most files, it is best to use all lowercase names. With this in mind, let us analyze the following names:

data-backup-02 good
data_backup_02 good
DataBackup02 uses uppercase letters
DATABACKUP02 uses uppercase letters
Data Backup 02 uses uppercase letters, contains a space
Data;Backup02 uses uppercase letters, contains a semicolon
databackup20 good
data/backup/20 contains slashes

Although five of these characters strings would make poor names, only the last one is illegal.

Review Question #3:

What are file permissions?

What are the two main uses for file permissions?

Which program is used to set or change file permissions?

What are the three types of file permissions?

For each type, explain what it means when applied (a) to an ordinary file (b) to a directory.

Answer

File permissions are authorizations that specifies how a file may be accessed.

There are two main uses for file permissions. First, to restrict access by other users or (depending on your point of view) to allow access by other users. Second, to guard against your own errors. For example, if a file does not have write permission, you can't delete it accidentally.

To set or change file permissions, you use the chmod program.

There are three types of file permissions: read permission, write permission, and execute permission. They govern the following operations:

Ordinary Files
Read Read from the file
Write Write to the file
Execute Execute the file
Directories
Read Read the directory
Write Create, move, copy or delete entries
Execute Search the directory

Review Question #4:

What is a link?

What is a symbolic link?

What is a hard link and a soft link?

How do you create a link?

How do you create a symbolic link?

Answer

A link is a connection between a filename and its inode.

A symbolic link (symlink) as a special type of link that is, literally, the pathname of another file.

A hard link is another name for a regular link; a soft link is another name for a symbolic link.

To create a link, use the ln program.

To create a symbolic link, use ln -s.

Review Question #5:

Which three programs are used to find a file or a set of files?

When would you use each one?

Answer

To fine a file or a set of files, you can use whereis, locate or find.

You use whereis to find the files associated with a specific Unix command: binary (executable) files, source files, and documentation files.

You use locate and find as general file finding tools: locate looks for file names in a special database; find actually searches a directory tree for what you want.

Using locate is simpler and faster, so it should be your first choice. However, find is a lot more powerful; you use it when locate can't do the job.

Applying Your Knowledge #1:

Within your home directory, create a directory named temp and change to it. Within this directory, create two subdirectories named days and months. Within each directory, create two files named file1 and file2.

Hint: Use a subshell (see Chapter 15) to change the working directory and create the files.

Once all the files are created, use the tree program (Chapter 24) to display a diagram of the directory tree showing both directories and files. If your system doesn't have tree, use ls -R instead.

Answer

To prepare, you can use the following command to remove a temp directory should one already exist:

rm -r ~/temp

Within your home directory, create a directory named temp and change to it:

cd; mkdir temp; cd temp

Within this directory, create two subdirectories named days and months :

mkdir days months

Within each directory, create two files named file1 and file2:

(cd days; touch file1 file2)
(cd months; touch file1 file2)

Display a diagram of the directory tree showing both directories and files:

tree

If your system doesn't have tree, use:

ls -R

Applying Your Knowledge #2:

Continuing from the previous exercise:

Within the days directory, rename file1 to monday and rename file2 to tuesday. Then copy monday to friday.

Within the months directory, rename file1 to december and file2to july.

Move back to temp and use tree to display another diagram of the directory tree. If your system doesn't have tree, use ls -R instead.

Create a link from december to april. Create a symbolic link from december to may. Display a long listing of the months directory. What do you notice?

To clean up, use a single command to delete the temp directory, all its subdirectories, and all the files in those directories. Use one final command to confirm that temp no longer exists.

Answer

Within the days directory...

Rename file1 to monday:

cd days
mv file1 monday

Rename file2 to tuesday:

mv file2 tuesday

Copy monday to friday:

cp monday friday

Within the months directory...

Rename file1 to december:

cd ../months
mv file1 december

Rename file2 to july:

mv file2 july

Move back to temp:

cd ..

Display another diagram of the directory tree:

tree
ls -R

If your system doesn't have tree, use:

ls -R

Create a link from december to april:

ln december april

Create a symbolic link from december to may:

ln -s december may

Display a long listing of the months directory

ls -l

You will notice the hard links and the symbolic link.

Delete the temp directory, all its subdirectories, and all the files in those directories:

cd; rm -r temp

Confirm that temp no longer exists:

ls -l temp

Applying Your Knowledge #3:

Use a text editor to create a file named green. Within the file, enter the line:

I am a smart person.

Save your work and quit the editor.

Create a link to the file green and call it blue. Display a long listing of green and blue and make a note of their modification times.

Wait 3 minutes, then use the text editor to edit the file green. Change the one line of text to:

I am a very smart person.

Save your work and quit the editor.

Display a long listing of green and blue. Why do they both have the same (updated) modification time even though you only edited one file? What would have happened if you had changed blue instead of green?

Answer

Create the file:

vi green
iI am a smart person. (press the <Esc> key)
ZZ

Create the link; display the long listing:

ln green blue
ls -l green blue

Output:

-rw-rw-r-- 2 harley staff 21 Nov 25 19:38 blue
-rw-rw-r-- 2 harley staff 21 Nov 25 19:38 green

Wait 3 minutes.

Edit the file:

vi green
:s/smart/very smart/
ZZ

Display the long listing:

ls -l green blue

Output:

-rw-rw-r-- 2 harley staff 26 Nov 25 19:41 blue
-rw-rw-r-- 2 harley staff 26 Nov 25 19:41 green

Even though you changed only green, the modification time changed for both green and blue. This is because they are links to the same file. The same thing would have happened had you changed blue.

Applying Your Knowledge #4:

You are setting up a Web site in which all the HTML files are in subdirectories of a directory named httpdocs in your home directory. Use a pipeline to find all the files with the extension .html and change their permissions to the following:

Owner: read + write
Group: nothing
Other: read

Answer

Start by figuring out file permissions:

Owner:read + write=4+2=6
Group:nothing=0=0
Other:read=4=4

` Thus, the file mode should be 604. Now find the files you need and change their file mode to 604:

find ~/httpdocs -name '*.html' | xargs chmod 604

For Further Thought #1:

Most GUI-based file managers maintain a "trash" folder to store deleted files, so they can be recovered if necessary. With such systems, a file is not gone for good until it is deleted from the "trash". Why do the Unix text-based tools not offer such a service?

Answer

• GUI-based file managers must serve non- technical people, who don't have much knowledge or experience. Such people are more likely to make mistakes.

• When you use a GUI-based file manager, it is easy to delete the wrong files.

• GUI-based file managers are better for managing very large numbers of files. In such cases, it is often better to encourage people to delete what they don't need. This is easier for them if they know they can restore a deleted file (at least for a short period of time), should that become necessary.

For Further Thought #2:

Imagine going back in a time machine to 1976, when I first started to use Unix. You find me and ask for a tour of the Unix system I am using.

To your surprise, you see files, directories, subdirectories, a working directory, a home directory, special files, links, inodes, permissions, and so on. You also see all the standard commands: ls, mkdir, pwd, cd, chmod, cp, rm, mv, ln and find. Indeed, you notice that virtually every idea and tool you learned about in this chapter was developed more than thirty years ago.

What is it about the basic design of the Unix filesystem that has enabled it to survive for so long and still be so useful?

Is this unusual in the world of computing?

Answer

There are several reasons that the Unix filesystem has been used for so long.:

• Unix was well-designed; resources were scarce

• Unix was machine independent, so it could be ported to other systems

• Unix proved to be scalable and adaptable

• Unix tools and ideas make a lot of sense to smart people

• Inertia: people generally don't like to change when something seems to be working

In some ways this is unusual, as most computer tools are replaced long before 30 years. However, the best ideas stay around indefinitely as long as people still use them. Unix itself has managed to survive for a long time — thanks in large part to Linux — and, during this time, the original filesystem design was able to continually prove itself to be useful.

For Further Thought #3:

The find program is a powerful tool, but very complicated. Imagine a GUI-based version of the program that enables you to choose options from a large drop-down list, enter file patterns into a form, select various tests from a menu, and so on.

Would such a program be be easier to use than the standard text-based version?

Could a GUI version of find replace the text-based version?

Answer

A GUI-based version find would be easier for:

• Casual users who don't use find very often

• People who type slowly or make a lot of mistakes when they type

• Situations in which you need to perform very complex find (or find+execute) operations

A GUI-based version find could not replace the text-based version because the text-based version:

• Is faster and easier to use (once you are used to it)

• Can be used remotely

• Necessary for scripting

Jump to top of page