Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

mkdir usage

by skyworld_chen (Acolyte)
on Dec 21, 2012 at 01:47 UTC ( #1009828=perlquestion: print w/replies, xml ) Need Help??
skyworld_chen has asked for the wisdom of the Perl Monks concerning the following question:


I would like to test if a directory exists. If not, mkdir it. I wrote code like this:

#!/usr/bin/perl if (! -d abc) { mkdir "abc", "0777" or die "Can't mkdir abc: $!"; }

I guess if the directory exist, there would prompt nothing. But it is interesting that the linux prompts "Can't generate abc: File exists". Can anybody help me to understand why the code still want to mkdir even if (! -d abc) implies false? thanks.

Replies are listed 'Best First'.
Re: mkdir usage
by BrowserUk (Pope) on Dec 21, 2012 at 02:02 UTC

    1. if (! -d abc) { should be if (! -d 'abc') {

      use warnings; would have caught that:

      C:\test>perl -wE"if( -d abc ) { say 'hi' }" Unquoted string "abc" may clash with future reserved word at -e line 1 +. Name "main::abc" used only once: possible typo at -e line 1. -d on unopened filehandle abc at -e line 1.
    2. You cannot create a directory if a file of the same name already exists.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: mkdir usage
by toolic (Bishop) on Dec 21, 2012 at 02:07 UTC
    You could also consider using File::Path::make_path
      Ya, this is my preferred option, because you don't need to test whether the directory already exists since it will make the directory if it needs to and won't complain if it already exists.
Re: mkdir usage
by jwkrahn (Monsignor) on Dec 21, 2012 at 05:25 UTC
    mkdir "abc", "0777" or die "Can't mkdir abc: $!";

    The second argument to mkdir expects a numerical value so providing a string means it will be interpreted as a number and the string "0777" is numerically different than the number 0777:

    $ perl -le'print 0 + "0777"' 777 $ perl -le'print 0 + 0777' 511 $ perl -e'mkdir "testone", 0777 or die "Cannot mkdir testone because: +$!"' $ ls -dl testone drwxr-xr-x 2 john john 4096 2012-12-20 21:12 testone $ rmdir testone $ perl -e'mkdir "testone", "0777" or die "Cannot mkdir testone because +: $!"' $ ls -dl testone dr----x--t 2 john john 4096 2012-12-20 21:13 testone $ rmdir testone
Re: mkdir usage
by LanX (Bishop) on Dec 21, 2012 at 02:25 UTC
    As a side note:

    your condition will also be true on other files than directories, but mkdir will of course fail with a 'File exists' error.

    Cheers Rolf

      I got the idea. Thanks for all of your kind replies.

Re: mkdir usage
by 2teez (Vicar) on Dec 21, 2012 at 02:11 UTC

    just a side comment:
    Using mkdir, MASK default to "0777", if omitted. So, you really don't have to specify in your OP.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: mkdir usage
by Marshall (Abbot) on Dec 21, 2012 at 06:37 UTC
    !/usr/bin/perl -w

    # I most often use "-w" on first line the rather than "use warnings;".
    # There can be difference later when using other modules.
    # In this case, there is no difference.
    if (! -d abc) { mkdir "abc", "0777" or die "Can't mkdir abc: $!"; }
    coud have been:
    if (! -d "abc") { mkdir "abc", "0777" or die "Can't mkdir abc: $!"; }
      These various quoting gizmos do make a difference.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009828]
Approved by toolic
Front-paged by 2teez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-04-22 05:40 GMT
Find Nodes?
    Voting Booth?