How to create directories recursively like mkdir -p in Perl?

Question

How to create directories recursively like mkdir -p in Perl?

Creating directories recursively in Perl, similar to the mkdir -p command in Unix-like systems, means creating not just a single directory but all the missing parent directories along the given path. Perl's built-in mkdir function only creates one directory at a time and fails if the parent directory does not exist. To replicate the behavior of mkdir -p, you need a way to ensure all intermediate directories are created as well.

Common Approaches

  • Manual recursion or iteration: Splitting the path and making each directory one by one, checking for existence.
  • Using core module File::Path: This is the most reliable and convenient method. The make_path function creates all directories needed.

Recommended Solution: Using File::Path::make_path

The File::Path module is included in core Perl (since Perl 5.004 for make_path), so no extra installation is needed. It provides robust recursive directory creation that works across platforms.
  • make_path($dir) creates $dir and all necessary parent directories.
  • You can also specify permissions (mode) optionally.
  • It will not error if the directory already exists, similar to mkdir -p.

Perl-Specific Concepts

  • mkdir in Perl uses a single argument for path, and an optional mode (permissions) as the second argument.
  • When directories exist, mkdir returns false (failure), but make_path handles this gracefully.
  • Perl sigils ($ for scalar, @ for arrays) are visible in examples showing path variables.

Example: Create Nested Directory Path Recursively

use strict;
use warnings;
use File::Path qw(make_path);

my $dir = "foo/bar/baz";

eval {
    make_path($dir, { mode => 0755 });
    print "Successfully created directory path: $dir\n";
};
if ($@) {
    die "Failed to create directory path '$dir': $@\n";
}

This example tries to create the nested directory foo/bar/baz with permissions 0755. If the directories already exist, it will not fail. Errors (such as due to permissions issues) are trapped in the eval block and reported with $@.

Common Pitfalls and Gotchas

  • Permissions: On some platforms, the actual permissions created may be affected by the process' umask.
  • Cross-platform paths: Use File::Spec or similar if building file paths portably.
  • Directory already exists: Unlike mkdir, make_path won’t die if the directory exists (like mkdir -p).
  • Mode argument: Remember mode is an octal number, so use leading zero (e.g., 0755), otherwise it will be interpreted as decimal.

Summary

To create directories recursively in Perl, you can either write your own loop checking and creating directories one by one, but this is error-prone. Instead, rely on the core File::Path module's make_path function, which is designed to work like mkdir -p and is simple, robust, and cross-platform.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 26ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Successfully created directory path: foo/bar/baz
STDERR
(empty)

Was this helpful?

Related Questions