Perl mkdir -p : How to Automatically Create Recursive Directory

perl mkdir -pPerl stands for Practical Extraction and Report Language. Like the name suggests, Perl was specifically designed for text processing, and creating reports. Perl programs are relatively stable and run on a wide variety of different platforms. Perl applications are used for network programming, system administration and other tasks apart from text manipulation.  This language is extensible and provides support for both procedural and object oriented programming. Perl works well with markup languages such as HTML, XML and others. This popular and powerful programming language supports Unicode and is Y2K compliant. It is suitable to handle encrypted data which includes e-commerce transactions.   Programmers coming from a C or UNIX shell programming background will find it relatively easy to migrate to this platform.

Today, we walk you through the Perl mkdir –p command. This requires a basic understanding of the language. If you are new to Perl, you may want to take this introductory course before we get started.

The Perl mkdir command is the short form of make directory. This command is used to make directories in the order specified. The general syntax of mkdir command is as follows.

mkdir ‘/name_of_directory’, permission_setting;

This command has two parameters. The first parameter specifies the name of the directory that is to be created. The second parameter is used to set the user permissions on this directory. Note that the second parameter is not mandatory.

Using the mkdir -p Command to Create Recursive Directories

A directory traversal is a systematic method, through which the contents of computer directories are scanned. This starts from the current working directory. In many cases, directory traversals are recursive. In other words they scan the content of the working directory and also the content of every distinct folder or subdirectory contained within the working directory. Using mkdir –p option we can create intermediate directories. In other words, when a new directory is created using this command, the system also creates the parent directories which contain the newly created directories. You do not have to bother about the parent directories at all.  The syntax is as follows:

mkdir [-p] [-m mode] directory_name

This will create intermediate directories. If the -p option is is not given you will need the full path prefix of each of the directories to be created. These intermediate directories are created with the permission 0777. The owner will have the write and search permissions. In case the directory to be created already exists, it is not regarded as an error. Take a look at the following example:

mkdir -p /home/java/database/sourcefiles/c/bar

Here we use the -p option of the Perl mkdir command in order to create the directory and all intermediate subdirectories which are required. This is the simpler method than typing out all the equivalent commands, if you didn’t want to use -p – for example:

cd home

mkdir java

cd java

mkdir database

cd database

mkdir sourcefiles

cd sourcefiles

mkdir c

cd c

mkdir ba

The -m option sets the directory permissions according to the mode specified. These permissions can be read, modify and execute. The mode should be an octal number. If the mkdir command is executed successfully the value 0 is returned. In case an error occurs a value greater than 0 is returned. For this command, it is mandatory for the user to have write permission in the parent directory. In case of the existence of a symbolic mode, permissions are added or removed using the operators ‘+’ and ‘-‘. Take a look at the following example

mkdir -m a= rwx newdir;

Here this command gives all the users the permission to read, write and execute the contents of the newly created directory, newdir. The list of permissions that can be set is similar to Linux or other Unix flavors. To learn  more about the different permissions you can set in mkdir –p, you can take this Linux course.

Other Options that can be Used with the mkdir -p Command

mkdir -p -v <directories>

Here the intermediate directories are created and a message is displayed for each created directory or subdirectory. Take a look at the example below

mkdir -p -v /home/java/database/sourcefiles/c

mkdir -p --help

This command invokes the help files. To learn about other Perl commands and options, check out this course.

How to Create a Directory Structure Using the Current Year, Month and Date

This is achieved by executing the following command.

mkdir -p `date +%Y`/`date +%m-%B`/`date +%d`

The date command displays the current date. %Y is used to get the current year, %m stands for month and %d represent the day of the month in numerical format.

How to Create Intermediate Directories Using the Operating System Shell

Certain Perl version do not support the -p option. If your Linux or Operating system shell supports -p, you can use the system command as shown below:

system "mkdir -p <directoryname>";

Take a look at the following example to understand better.

system "mkdir -p newdir";

How to Create Intermediate Directories Using a Perl Script

If your Operating system also doesn’t support the -p option, you can still work around it by creating and using this alternative Perl script.

#!/usr/bin/perl

use warnings;

use strict;

my $dirname = "dir1/dir2";

my @dir = split /\//,$dirname;

for (0 .. $#dir) {

mkdir -p $dir[$_];

chdir $dir[$_];

}

In this code, split obtains a list of directory names. The for loop loops over the entire list. In each loop we create a directory with the help of mkdir function. The chdir function is used to go into the newly created into the directory. The loop terminates when the last directory from the list is created.

Create Intermediate Directories without Using the –p Option of mkdir

#!/usr/bin/perl

use warnings;

use strict;

my $dirname = "dir1/dir2";

my @folders = split /\/|\\/, $dirname;

map { mkdir $_; chdir $_; } @folders;

The above program uses the map function instead of chdir, or other alternatives. This just goes to show you that there are often multiple ways of achieving the same result.  You need to figure which alternative works best for your scenario. You may also want to see how it is done in other scripting languages. Take this course to get a peak into Bash scripting and Python as well.