perlpal has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

My requirement is to create multiple level directories on Linux i.e create a directory say "Logs"(static name).Within this directory , create another directory say "UnitTest" (dynamic name given by the script).

The directory structure would end up looking like this :

Logs/UnitTest/<files>

How do i create multiple levels of directories?. I am aware that the "mkdir" function creates a single directory.Am i missing out on a variant that meets my purpose?

Thanks in advance!

  • Comment on Creating multiple level directory structure

Replies are listed 'Best First'.
Re: Creating multiple level directory structure
by moritz (Cardinal) on Aug 11, 2010 at 14:14 UTC
    The File::Path core module comes with the mkpath function, which does what you want.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Creating multiple level directory structure
by Ratazong (Monsignor) on Aug 11, 2010 at 14:18 UTC

    From the mkdir-docs (last sentence):

    To recursively create a directory structure, look at the mkpath function of the File::Path module.

    HTH, Rata

Re: Creating multiple level directory structure
by tospo (Hermit) on Aug 11, 2010 at 14:42 UTC
Re: Creating multiple level directory structure
by VinsWorldcom (Prior) on Aug 11, 2010 at 14:19 UTC
    mkdir -p Logs/UnitTest/foo

    Update: As toolic notes below, 'mkdir -p' is indeed for the Linux command. In my reading of the OP, I got the words 'Linux' and 'function' and jumped to the conclusion that he was after something like:

    system('mkdir Logs/UnitTest/foo');

    which may certainly *not* be the case.

      Just to be explicitly clear, mkdir -p is for the Unix mkdir command (which could be invoked from perl using system), not the Perl builtin mkdir function.
Re: Creating multiple level directory structure
by zentara (Cardinal) on Aug 11, 2010 at 17:44 UTC
Re: Creating multiple level directory structure
by Anonymous Monk on Aug 11, 2010 at 14:14 UTC
    One by one by one
    mkdir 'Logs'; mkdir 'Logs/UnitTest'; mkdir 'Logs/UnitTest/Funk';
    File::Path