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

Built-in Functions to Work with the File System

This section describes Perl built-in functions, chdir(), mkdir(), rmdir(), and unlink() to work with the file system, like to change the current directory, make or remove a directory, or delete a file.

A file system is a tree structure of directories with files associated to directories. Perl offers several built-in functions for you to work with the file system:

1. chdir() - Resets the current directory to a directory identified by the specified path name:

rc = chdir(path_name);

For example, chdir("..") resets the new current directory to the parent directory of the old current directory.

2. mkdir() - Makes a new directory defined by the specified path name:

rc = mkdir(path_name);

For example, mkdir("/temp/test") makes a new directory under /temp in the file system. Notice that "/" works like "\" with ActivePerl on Windows systems.

3. rmdir() - Removes a directory defined by the specified path name, when the directory is empty:

rc = rmdir(path_name);

For example, rmdir("/temp/test") removes the /temp/text directory in the file system. If it is not empty, this function will fail.

4. unlink() - A function to delete a list of files:

rc = unlink(file_list);

For example, unlink("/temp/junk.txt") removes the /temp/junk.txt file in the file system.

Sections in This Chapter

Built-in Functions to Work with the File System

File Test Operators

stat() - Returns File Statistics

Dr. Herong Yang, updated in 2008
Built-in Functions to Work with the File System