Chapter 7. Working with directories

Table of Contents
7.1. How directories work
7.2. Creating directories
7.3. Removing directories
7.4. Switching your current directory
7.5. Opening Directories
7.6. Reading Directories
7.7. Closing an open directory
7.8. Example of reading a directory
7.9. Example of making a DIR command for QSHELL
7.10. Example of Reading a directory recursively

7.1. How directories work

Back when we first discussed stream files, and path names, we talked about directories. We know that directories are similar to libraries in their ability to act as "containers" for stream files. In other words, stream files are stored inside directories in the IFS.

In addition to storing stream files inside a directory, most file systems also allow you to store directories inside a directory. A directory inside a directory is sometimes referred to as a "sub-directory." From a programmers perspective, directories and sub-directories behave exactly the same way, so we will simply refer to them all as "directories."

Each directory has a "mode" (access permission bits) just as the stream files do. Most file systems allow you to run chmod() to change the mode, just as you would with a file.

The mode of a directory acts slightly differently, however. If you have "read" permission to a directory, it means that you're allowed to see the list of files that it contains. If you have "write" permission, it means that you're able to add files, delete files, rename files, etc in the directory. If you have "execute" permission to a directory, it means that you're allowed to "search" the directory. Some programs, such as the "find" utility in QShell, will skip searching directories that you do not have execute authority to.

Just in case you missed it, let me stress this again: If you have "write" permission to a directory, you can add, delete, or rename files in that directory. Even though you don't have access to the file itself!

For example, let's say you created a directory called "scott". In that directory, you put a file called "dont_let_bob_read.txt". Let's say that the directory's mode allows read, write and execute to the owner, the group and everyone else. Let's say that the file gives read and write access to the owner, but nothing to the group or the world. Okay, now Bob sees his name on the file, he tries to open it with DSPF, but he can't. He can't read the file. So, he tries to delete it... He can! The file is gone.

Why can Bob do that? Because, technically, when you are adding, removing, or changing the names of stream files in a directory, you aren't modifying the file data. You're adding, removing or changing entries in the directory itself.

There are two "special" entries inside each directory, as well. They are "." (a single dot) and ".." (two dots). These signify the "current directory" and the "parent directory" respectively.