When does `mkdir -p /path/to/dir` exist with nonzero status?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












mkdir -p /path/to/something


when does the above command exit with nonzero exit status?



Does it happen, if and only if /path/to/something doesn't exist as any file (or as a directory?) before running the command and the command fails to create it as a directory?



man mkdir says



 -p, --parents
no error if existing, make parent directories as needed


Thanks.







share|improve this question



















  • Try mkdir -p /dev/i/am/a/hacker as non-root and see it fail. If you don't have permissions it will fail and return a non-zero value.
    – cylgalad
    May 8 at 7:25















up vote
-1
down vote

favorite












mkdir -p /path/to/something


when does the above command exit with nonzero exit status?



Does it happen, if and only if /path/to/something doesn't exist as any file (or as a directory?) before running the command and the command fails to create it as a directory?



man mkdir says



 -p, --parents
no error if existing, make parent directories as needed


Thanks.







share|improve this question



















  • Try mkdir -p /dev/i/am/a/hacker as non-root and see it fail. If you don't have permissions it will fail and return a non-zero value.
    – cylgalad
    May 8 at 7:25













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











mkdir -p /path/to/something


when does the above command exit with nonzero exit status?



Does it happen, if and only if /path/to/something doesn't exist as any file (or as a directory?) before running the command and the command fails to create it as a directory?



man mkdir says



 -p, --parents
no error if existing, make parent directories as needed


Thanks.







share|improve this question











mkdir -p /path/to/something


when does the above command exit with nonzero exit status?



Does it happen, if and only if /path/to/something doesn't exist as any file (or as a directory?) before running the command and the command fails to create it as a directory?



man mkdir says



 -p, --parents
no error if existing, make parent directories as needed


Thanks.









share|improve this question










share|improve this question




share|improve this question









asked May 8 at 2:46









Tim

22.6k63222401




22.6k63222401











  • Try mkdir -p /dev/i/am/a/hacker as non-root and see it fail. If you don't have permissions it will fail and return a non-zero value.
    – cylgalad
    May 8 at 7:25

















  • Try mkdir -p /dev/i/am/a/hacker as non-root and see it fail. If you don't have permissions it will fail and return a non-zero value.
    – cylgalad
    May 8 at 7:25
















Try mkdir -p /dev/i/am/a/hacker as non-root and see it fail. If you don't have permissions it will fail and return a non-zero value.
– cylgalad
May 8 at 7:25





Try mkdir -p /dev/i/am/a/hacker as non-root and see it fail. If you don't have permissions it will fail and return a non-zero value.
– cylgalad
May 8 at 7:25











2 Answers
2






active

oldest

votes

















up vote
5
down vote













To elaborate on the previous answer with a few examples.

It could be when trying to create folders inside system folders:



ubuntu:~$ mkdir /proc/test
mkdir: cannot create directory ‘/proc/test’: No such file or directory
ubuntu:~$ mkdir -p /dev/null/test
mkdir: cannot create directory ‘/dev/null’: Not a directory


Or when you don't have permission to the previous folder:



ubuntu:~$ mkdir -p /root/test
mkdir: cannot create directory ‘/root’: Permission denied


It will also fail if there is a file by the same name in the folder:



ubuntu:~$ touch /tmp/test
ubuntu:~$ mkdir -p /tmp/test
mkdir: cannot create directory ‘/tmp/test’: File exists





share|improve this answer




























    up vote
    2
    down vote













    From https://www.gnu.org/software/coreutils/manual/html_node/mkdir-invocation.html#mkdir-invocation:



    An exit status of zero indicates success, and a nonzero value indicates failure. 


    Failure is when the directories need to be created but the operation fails.






    share|improve this answer





















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442442%2fwhen-does-mkdir-p-path-to-dir-exist-with-nonzero-status%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote













      To elaborate on the previous answer with a few examples.

      It could be when trying to create folders inside system folders:



      ubuntu:~$ mkdir /proc/test
      mkdir: cannot create directory ‘/proc/test’: No such file or directory
      ubuntu:~$ mkdir -p /dev/null/test
      mkdir: cannot create directory ‘/dev/null’: Not a directory


      Or when you don't have permission to the previous folder:



      ubuntu:~$ mkdir -p /root/test
      mkdir: cannot create directory ‘/root’: Permission denied


      It will also fail if there is a file by the same name in the folder:



      ubuntu:~$ touch /tmp/test
      ubuntu:~$ mkdir -p /tmp/test
      mkdir: cannot create directory ‘/tmp/test’: File exists





      share|improve this answer

























        up vote
        5
        down vote













        To elaborate on the previous answer with a few examples.

        It could be when trying to create folders inside system folders:



        ubuntu:~$ mkdir /proc/test
        mkdir: cannot create directory ‘/proc/test’: No such file or directory
        ubuntu:~$ mkdir -p /dev/null/test
        mkdir: cannot create directory ‘/dev/null’: Not a directory


        Or when you don't have permission to the previous folder:



        ubuntu:~$ mkdir -p /root/test
        mkdir: cannot create directory ‘/root’: Permission denied


        It will also fail if there is a file by the same name in the folder:



        ubuntu:~$ touch /tmp/test
        ubuntu:~$ mkdir -p /tmp/test
        mkdir: cannot create directory ‘/tmp/test’: File exists





        share|improve this answer























          up vote
          5
          down vote










          up vote
          5
          down vote









          To elaborate on the previous answer with a few examples.

          It could be when trying to create folders inside system folders:



          ubuntu:~$ mkdir /proc/test
          mkdir: cannot create directory ‘/proc/test’: No such file or directory
          ubuntu:~$ mkdir -p /dev/null/test
          mkdir: cannot create directory ‘/dev/null’: Not a directory


          Or when you don't have permission to the previous folder:



          ubuntu:~$ mkdir -p /root/test
          mkdir: cannot create directory ‘/root’: Permission denied


          It will also fail if there is a file by the same name in the folder:



          ubuntu:~$ touch /tmp/test
          ubuntu:~$ mkdir -p /tmp/test
          mkdir: cannot create directory ‘/tmp/test’: File exists





          share|improve this answer













          To elaborate on the previous answer with a few examples.

          It could be when trying to create folders inside system folders:



          ubuntu:~$ mkdir /proc/test
          mkdir: cannot create directory ‘/proc/test’: No such file or directory
          ubuntu:~$ mkdir -p /dev/null/test
          mkdir: cannot create directory ‘/dev/null’: Not a directory


          Or when you don't have permission to the previous folder:



          ubuntu:~$ mkdir -p /root/test
          mkdir: cannot create directory ‘/root’: Permission denied


          It will also fail if there is a file by the same name in the folder:



          ubuntu:~$ touch /tmp/test
          ubuntu:~$ mkdir -p /tmp/test
          mkdir: cannot create directory ‘/tmp/test’: File exists






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 8 at 3:23









          Mikael Kjær

          43638




          43638






















              up vote
              2
              down vote













              From https://www.gnu.org/software/coreutils/manual/html_node/mkdir-invocation.html#mkdir-invocation:



              An exit status of zero indicates success, and a nonzero value indicates failure. 


              Failure is when the directories need to be created but the operation fails.






              share|improve this answer

























                up vote
                2
                down vote













                From https://www.gnu.org/software/coreutils/manual/html_node/mkdir-invocation.html#mkdir-invocation:



                An exit status of zero indicates success, and a nonzero value indicates failure. 


                Failure is when the directories need to be created but the operation fails.






                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  From https://www.gnu.org/software/coreutils/manual/html_node/mkdir-invocation.html#mkdir-invocation:



                  An exit status of zero indicates success, and a nonzero value indicates failure. 


                  Failure is when the directories need to be created but the operation fails.






                  share|improve this answer













                  From https://www.gnu.org/software/coreutils/manual/html_node/mkdir-invocation.html#mkdir-invocation:



                  An exit status of zero indicates success, and a nonzero value indicates failure. 


                  Failure is when the directories need to be created but the operation fails.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 8 at 2:53









                  dsstorefile1

                  1,516212




                  1,516212






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442442%2fwhen-does-mkdir-p-path-to-dir-exist-with-nonzero-status%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay