18

In Perl, how can I create a subdirectory and, at the same time, create parent directories if they do not exist? Like UNIX's mkdir -p command?

Axeman
  • 29,660
  • 2
  • 47
  • 102
skiphoppy
  • 97,646
  • 72
  • 174
  • 218
  • Added this question so I'll be able to find it again easily in the future, instead of looking it up every time. I always look for this in POSIX.pm, for some reason. – skiphoppy Jun 26 '09 at 17:50

5 Answers5

30
use File::Path qw(make_path);
make_path("path/to/sub/directory");

The deprecated mkpath and preferred make_path stemmed from a discussion in Perl 5 Porters thread that's archived here.

In a nutshell, Perl 5.10 testing turned up awkwardness in the argument parsing of the makepath() interface. So it was replaced with a simpler version that took a hash as the final argument to set options for the function.

skiphoppy
  • 97,646
  • 72
  • 174
  • 218
Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90
  • 3
    @skiphoppy: This still isn't "like UNIX's mkdir -p command" as stated in the question. mkdir's man page shows -p, --parents no error if existing, make parent directories as needed But make_path will return false if the directory exists. (ie, make_path "/tmp/foo" or die; #will die if /tmp/foo exists) – Randall May 17 '11 at 16:48
3

Use mkpath from the File::Path module:

use File::Path qw(mkpath);
mkpath("path/to/sub/directory");
skiphoppy
  • 97,646
  • 72
  • 174
  • 218
  • 2
    Researching this just now, I see that mkpath() is deprecated and the official interface is now make_path(). If someone writes up a great explanation as to what that buys us, I'll make that the accepted answer. – skiphoppy Jun 26 '09 at 17:52
3

Kindly ignore if you are looking for a Perl module with 'mkdir -p' functionality but the following code would work:

my $dir = '/root/example/dir';

system ("mkdir -p $dir 2> /dev/null") == 0 
    or die "failed to create $dir. exiting...\n";

You can use a module but then you have to install it on each server you are going to port your code on. I usually prefer to use system function for a work like mkdir because it's a lesser overhead to import and call a module when I need it only once to create a directory.

Ashish Kumar
  • 811
  • 4
  • 8
  • That approach makes sense if you want to avoid installing modules. I usually prefer to demand the installation of modules I need, or I refuse to support the system. :) I prefer to use modules so I can reap the benefit of the testing and research that went into them, so I can avoid mistakes such as failing to check system calls or losing output of error messages, and because it's less overhead to not have to create a process. – skiphoppy Sep 17 '12 at 14:48
  • 1
    Doesn't that create a dependency to the operating system? – Peter Mortensen Jun 03 '17 at 17:52
0

ref http://perldoc.perl.org/File/Path.html

"The make_path function creates the given directories if they don't exists [sic!] before, much like the Unix command mkdir -p"

mcchiz
  • 742
  • 6
  • 11
0

mkdir() allows you to create directories in your Perl script.

Example:

use strict;
use warnings;

my $directory = "tmp";

unless(mkdir($directory, 0755)) {
        die "Unable to create $directory\n";

This program create a directory called "tmp" with permissions set to 0755 (only the owner has the permission to write to the directory; group members and others can only view files and list the directory contents).

Ankireddy
  • 179
  • 1
  • 6