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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 84

Expert Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

762 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