Harley Hahn's Guide to
|
A Personal Note
Chapters...
Command
INSTRUCTOR |
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:
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)
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:
Within a directory listing:
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:
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/
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
To display the directory tree (with Linux), showing only directories, use: tree -d temp The output is:
temp
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.
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
To display the directory tree (with Linux), showing directories and files, use: tree ~/temp The output is:
`-- books
Applying Your Knowledge #3: Clear the directory stack. Now push the following two directories onto the stack:
~/temp/books/unix/harley
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
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. Exercises: Introduction | Chapter list
© All contents Copyright 2024, Harley Hahn
|