Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Open Directories and Read File Names

This chapter provides tutorial examples and notes about opening directories. Topics include using opendir() to open a directory handle for a given path name; using readdir() to read file names and sub directory names; using recursive methods to process all files of a directory tree.

opendir() - Open Directory to Read File Names

opendir.pl - Sample Program to Read Directories

DirTree.pl - Displaying the Directory Tree

DirGrep.pl - Searching Text in Directory Files

Conclusions:

  • opendir(DIR, $path) - Opening a directory handle to a directory path.
  • readdir(DIR) - Reading one entry or all entries from directory handle.
  • A recursive method is needed to process all entries of a directory tree.
  • DirGrep.pl pattern path - A good sample program to search for text in a directory tree.

Dr. Herong Yang, updated in 2008
Open Directories and Read File Names