Perl

PERL Mkdir() Function

“The mkdir() function is a built-in function of PERL to create a directory by setting the permission bits. It is a useful function when it requires storing data by creating a directory and making the data secure by setting the access permission. The uses of this function have been shown in this tutorial by using multiple PERL scripts.”

Syntax

The syntax of the mkdir() function is given below.

boolean mkdir (directory, mode)

The function can take two argument values. The first argument is mandatory, and the name of the new directory is passed in this argument. The second argument is optional, and the permission bits for the directory are set by this argument. The function returns true on success and returns false on failure.

Different uses of the mkdir() function have been shown in the next part of the tutorial.

Example-1: Create a Directory

Create a PERL file with the following script that will create a directory on the current location or print an error message if it is unable to create the directory. If the directory named “test” already exists in the current location or there is no permission to create a directory on the current location, then an error message will be printed; otherwise, the directory will be created.

$dir_name = "test";

# Create the directory

mkdir($dir_name)

# Print error message

or die "Unable to create $dir_name directory.\n";

Run the following commands to execute the PERL code and check the current directory list.

$ perl mkdir1.pl

$ ls

Output

The following output will appear if the directory is created successfully.

Example-2: Create a Directory if it Does not Exist

Create a PERL file with the following script that will create a directory if the directory does not exist in the current location by using the mkdir() function. The directory name will be taken from the user, and the -e option with the directory name has been used in the “if” condition to check whether the directory already exists in the current location or not. If the directory name taken from the user does not exist in the current location, then the directory will be created, and a success message will be printed after executing the code.

print "Enter the directory name: ";

#Take directory name from the user

$dir_name = <> ;

#Remove newline

chomp($dir_name);

#Check the directory exist or not

if (-e "$dir_name")

{

    #Print message if the directory exists
    print "Directory already exist.\n";
}
else
{
    #Create the directory
    mkdir($dir_name)
    #Print error message
    or die "Unable to create $dir_name directory.\n";
    #Print the success message
    print "Directory created successfully.\n";
}

Run the following commands to execute the PERL code and check the current directory list.

$ perl mkdir2.pl

$ ls

Output

According to the following output, the directory name taken from the user exists in the current location for the first execution. The directory name taken from the user does not exist in the current location for the second execution. So, the directory has been created successfully, and the success message has been printed.

Example-3: Create a Directory With the Permission Bits

The second argument of the mkdir() function has been used to set the permission bits of the directory that will be created by this function. Create a PERL file with the following code that will create a directory name taken from the user with permission, “0744,” if the directory does not exist in the current location. The oct() function has been used in the code to convert the permission bits from the string value to the number value.

print "Enter the directory name: ";

#Take directory name from the user

$dir_name = <> ;

#Remove newline

chomp($dir_name);

#Check the directory exist or not

if (-e "$dir_name")

{
    #Print message if the directory exists
    print "Directory already exist.\n";
}
else
{
    $permission = "0744";
    #Create the directory with permission
    mkdir ($dir_name, oct($permission))
    #Print error message
    or die "Unable to create $dir_name directory.\n";
    #Print the success message
    print "Directory created successfully.\n";
}

Run the following commands to execute the PERL code and check the current directory list.

$ perl mkdir3.pl

$ ls -l

Output

According to the following output, the directory named “testdir” did not exist in the current location before, and the directory was created successfully after executing the code. The output of the “ls -l” command shows that the owner of the directory has all permissions, and the group and other users have the read permission only that has been set by the permissions bits, 0744.

Example-4: Create a Directory Based on the Directory Path

Create a PERL file with the following code that will create a directory based on the given path if the directory does not exist. The –d option has been used with the directory path in the “if” condition to check whether the directory exists in the defined location or not. If the directory path does not exist, then an error message will be displayed.

#Set the directory path

$dir_path = "/home/fahmida/perl/temp/mydir";

#Check the directory exist or not

if ( -d "$dir_path")

{
    #Print message if the directory exists
    print "Directory already exist.\n";
}
else
{
    mkdir($dir_path)
    #Print error message
    or die "Unable to create $dir_path directory.\n";
    #Print the success message
    print "Directory created successfully.\n";
}

Run the following commands to execute the PERL code, go to the directory path and check whether the directory is created or not.

$ perl mkdir3.pl

$ cd temp

$ ls

Output

According to the following output, the “mydir” directory was not existing before on the defined path, and the directory was created successfully.

Conclusion

The ways of creating a directory with or without the permission bits by using the mkdir() function have been shown in this tutorial. I hope this tutorial will help the new PERL user to know the uses of the mkdir() function in PERL properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.