Directory Manipulation





                    CHAPTER 13: DIRECTORY MANIPULATION


   Directory Manipulation 

     - Perl provides a number of functions to perform various
       operations on directories

     - These are very similar to the corresponding UNIX system call, 
       library function or command

     - See the UNIX man pages for details


   Chdir Function

     - Changes the current working directory (CWD) of the calling
       process

     - chdir (DIRNAME)
       chdir DIRNAME

     - Returns 1 for success, 0 for failure

     - On failure $! is set to the value of errno

     - Uses chdir(2)
     
     - Typical use:

         $status = chdir ("../bin");


   Mkdir Function

     - Creates a directory

     - mkdir (DIRNAME, MODE)

     - Returns 1 for success, 0 for failure

     - On failure $! is set to the value of errno

     - Uses mkdir(1)

     - Typical use:

         $status = mkdir ("toys", 0755);


   Rmdir Function

     - Removes an empty directory

     - rmdir (DIRNAME)
       rmdir DIRNAME

     - Returns 1 for success, 0 for failure

     - On failure $! is set to the value of errno

     - Use $_ if DIRNAME is omitted

     - Uses rmdir(1)

     - Typical use:

         $status = rmdir ("toys");


   What Is A Directory Handle?

     - Name for a connection to a directory

     - Used to read the list of filenames contained in a directory

     - Directory handle is an identifier similar to a variable name, 
       but without any special prefix character

     - Recommended that directory handles be all uppercase to avoid 
       conflict with reserved words

     - Directory handles have their own namespace

     - Directory handles are always opened read-only.  Files in the 
       directory can not be renamed or deleted using directory handles.


   Opendir Function

     - Opens a directory handle

     - opendir (DIRHANDLE, DIRNAME)

     - Returns 1 for success, 0 for failure

     - On failure $! is set to the value of errno

     - Uses opendir(3)

     - Typical use:

         $status = opendir (DH, "toys");


   Closedir Function

     - Closes a directory handle

     - closedir (DIRHANDLE)
       closedir DIRHANDLE

     - Returns 1 for success, 0 for failure

     - On failure $! is set to the value of errno

     - Uses closedir(3)

     - Typical use:

         $status = closedir (DH);


   Readdir Function

     - Reads a directory entry

     - readdir (DIRHANDLE)
       readdir DIRHANDLE

     - In a scalar context, returns the next directory entry for the 
       directory handle DIRHANDLE (or undef if there are no more entries).  
       In an array context, returns all the remaining directory entries 
       (or the empty list if there are no more entries.)

     - Uses readdir(3)

     - Use of readdir is more efficient than globbing with <*>

     - Typical use:

         @allfiles = readdir (DH);


   Rewinddir Function

     - Sets the current position of a directory handle to the beginning 
       of a directory 

     - rewinddir (DIRHANDLE)
       rewinddir DIRHANDLE

     - Returns 1 for success, 0 for failure

     - On failure $! is set to the value of errno

     - Uses rewinddir(3)

     - Typical use:

         $status = rewinddir (DH);


   Example Program

     - Here is a simple implementation of the ls program:

       opendir (DH, $dir) || die ("could not open directory $dir\n");

       foreach $file (sort readdir (DH))
       {
         print ($file\n");
       }

       closedir (DH);

    - Note that the readdir() function returns even the "." files.
      To eliminate these use:

      sort grep (!/^\.\.?$/, readdir (DH))




Bob Tarr
University of Maryland, Baltimore County
tarr@umbc.edu