Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to create sub-directory and main directory.

Posted on 2003-03-25
Medium Priority
Last Modified: 2010-05-18
I want to create a directory and a sub directory in it!

I was using: mkdir($dir,0777);

This will work for the last sub directory name.
if $dir="/home/shop/test/one";
the code will create a new directory in dir test if the dir tes already exists!

But what if the directory test does not exists?
What code do I have to use to check if dir home is available - if not create - if shop is available - if not create - if shop is available - if not create - and at last if one is available - if not create!

Question by:weversbv
  • 3
  • 2
LVL 20

Expert Comment

ID: 8202070
use File::Path;

mkpath( $dir, 0777);
LVL 20

Accepted Solution

jmcg earned 500 total points
ID: 8202073
Ooops. Let's correct that:

use File::Path;

mkpath( $dir, 0, 0777);

The documentation can be viewed at:


Author Comment

ID: 8202643
I file::path a standard module/ function in perl?
If not can it be done with standard modules/ functions?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 20

Expert Comment

ID: 8203185
Yes, File::Path should be a standard part of your Perl distribution, if it's recent enough. I have it on 5.6.0.

Yes, you can do it with plain Perl, but that would require breaking up the pathname into components, making each directory if it doesn't already exist, and fixing the modes on the directories after you've created their subdirectories. In short, most of the code in the File::Path module.

On a Unix or Linux system, you could use

system( "mkdir -p $dir");

if the default permissions are acceptable.

Author Comment

ID: 8208285
I use:

use File::Path;
mkpath( $dir, 0, 0777);

But the rights will be set to 0755?
What is wrong?
LVL 85

Expert Comment

ID: 8208344
#you have umask set to 022, try
umask 0;

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question