Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a directory and parent directories in one Perl command?

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?

like image 215
skiphoppy Avatar asked Jun 26 '09 17:06

skiphoppy


People also ask

How do I create a folder in parent directory?

A parent directory is a directory that is above another directory in the directory tree. To create parent directories, use the -p option. When the -p option is used, the command creates the directory only if it doesn't exist.

Which option can be used to create a directory structure with the missing parent directories?

Building a structure with multiple subdirectories using mkdir requires adding the -p option. This makes sure that mkdir adds any missing parent directories in the process.

How do you create a new directory in Perl?

Perl Articlesmkdir() allows you to create directories in your Perl script. The following program creates a directory called "temp". use strict; use warnings; sub main { my $directory = "temp"; unless(mkdir $directory) { die "Unable to create $directory\n"; } } main();


2 Answers

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.

like image 84
Clinton Pierce Avatar answered Oct 04 '22 04:10

Clinton Pierce


Use mkpath from the File::Path module:

use File::Path qw(mkpath);
mkpath("path/to/sub/directory");
like image 38
skiphoppy Avatar answered Oct 04 '22 04:10

skiphoppy