Perl mkdir: How to Create Directories in Perl

Database ConceptPerl was originally designed for UNIX systems in the 1980s as a Practical Extraction and Report Processing Language, or, in non-technical terms, a language that makes text processing easier. The language has really expanded since then, and today it has a lot of uses apart from just text processing. Perl is now, for example, a very popular language when it comes to web development. You can use one of several freely available frameworks to run and maintain web apps, like Catalyst, Titanium and Mojo. Perl is also a great testing language. You can use it to test several different kinds of applications, even if the applications were written in a different language. Perl has a very wide variety of uses. It is even being used in game development, for example.

Perl has a simple, easy to understand syntax that even a complete programming newbie can pick up quickly. It has a strong community that backs its support and development. And best of all, Perl is open source. This means that you can modify Perl any way you want to and using it, even for commercial purposes, won’t cost you anything. If you’re interested in learning Perl or becoming a Perl programmer, we offer a great introductory Perl course. It shows you everything you need to know to become a Perl programmer, so that you can build your own web applications with the language.

In this tutorial, we’re going to show you how you can create directories with Perl using the mkdir function.

Creating Directories in Perl using mkdir

The mkdir function lets you create directories inside existing directories or at a specific location. The syntax for the mkdir function is as follows:

mkdir ‘/name_of_directory’, permission_setting;

The mkdir function has two parameters. The “name_of_directory” parameter is, of course, the name of the directory you want to create. The second parameter is optional, though it’s strongly recommended that you set it. The “permission_setting” parameter sets the permissions on the directory. For example, can the current user read the directory? Modify the directory? Delete it? The “permission_setting” parameter will decide. The “permission_setting” parameter accepts an octal value. Also note that the “permission_setting” parameter is for UNIX-based operating systems. If you don’t specify a “permission_setting” parameter, it will revert to 0755 by default.

Let’s create a simple directory with the name “mydir” to show you how it’s done:

mkdir ‘mydir’, 0755;

This will create a directory called “mydir” in the directory you’re currently inside. The 0755 is the octal permission value. 0755 tells Perl that you, the current user, are allowed to read, modify and delete the directory –you have full permission to do anything you want with the directory. Other users will only be allowed to read the directory and not modify or delete it.

What if you wanted to create a directory in a different location than the one you’re at presently? You can do that like this:

$newdir ="/tmp/mydir";
mkdir $newdir, 0755;

As you can see, here we have set a path where we want the directory to be created, inside the folder called “tmp”. Then, we created the directory using the variable “newdir”. If Perl is unable to create a directory with the specified name, for some reason, it will return a $! error.

You can include this as part of any Perl script. Learn how to write your own Perl scripts with this course.

The Different Permissions you can set

There are a lot of different permissions you can set, which you’ll need at different times. We’ll give you a short list, but please note that it doesn’t contain ALL the possible permission combinations, just the most common ones:

Octal ValuePermission Info
001All users can execute
002All users can write
004All users can read
0010Only group users can execute
0020Only group users can write
0040All users in group can read
0100Only owner can execute
0200Only owner can write
0400Only owner can read
0666All users can read and write

 

This list of permissions is common across all Linux and Unix variants (side: you learn more about it in this Linux course).

You can set permissions the way we showed you earlier, or you can use a separate variable for it to improve readability:

my $newdir = “mydir”;
my $permissionset = “0020”; # only group users can write
mkdir $newdir, oct($permissionset);

This will create a directory called “mydir” with permission 0020. Remember, the permissions have to be an octal value, which is why we specified the oct () function.

Checking if the Directory has Been Created Successfully

Of course, you don’t know if the directory has been created successfully until you check. While the mkdir function returns a true value if it creates a directory successfully, this value isn’t visible to us unless we specifically check for it. We can do that like this:

use strict;
use warnings;
sub main {
my $newdirectory = "mydir";
unless(mkdir $newdirectory) {
die "The directory $directory has NOT been created!";
}
}
main();

Output (on running it 2 times):

The directory mydir has NOT been created!

This Perl script is simple enough to understand. Here, all we‘ve done is instructed Perl to tell us if the directory doesn’t get created successfully with the help of the unless function. Try to run this script two times. The second time, you will obtain the output:  “The directory mydir has NOT been created!”. This is because Perl will be unable to create a directory with the name “mydir” twice.

You can also use the –e operator to check if the directory exists in your system.

$newdir = "mydir";
mkdir $newdir, 0755;
if (-e $newdir)
{
print “Directory has NOT been created!”;
}
else
{
print “Directory has been created successfully”;
}

Output:

Directory has NOT been created
or
Directory has been created successfully

Here, the –e operator checks if the file with the name “mydir” is present on your system, and you will receive the appropriate output.

As you can see, creating a directory is a fairly straightforward process. To learn more about how to create, modify and delete directories in Perl take this comprehensive Perl course, that uses many real world examples. You can also take this introductory course to Bash Scripting and Python, to see how other scripting languages work. And don’t forget to have fun while you’re at it!