Is there a programmatical difference between directories created with mktemp -d or mkdir?

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











up vote
2
down vote

favorite












I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:



#!/bin/bash

scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)


Each time I run this command I see a new temporary directory created under /tmp/ (I didn't know it will appear there until reading Roaima's answer here):



IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).



If there is no programmatical difference, why should one prefer mktemp -d over the more minimal mkdir?










share|improve this question























  • I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use mktemp when added security matters.
    – Mr Shunz
    6 hours ago











  • @JohnDoea, I found mktemp -d easier to use.
    – prosti
    44 mins ago














up vote
2
down vote

favorite












I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:



#!/bin/bash

scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)


Each time I run this command I see a new temporary directory created under /tmp/ (I didn't know it will appear there until reading Roaima's answer here):



IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).



If there is no programmatical difference, why should one prefer mktemp -d over the more minimal mkdir?










share|improve this question























  • I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use mktemp when added security matters.
    – Mr Shunz
    6 hours ago











  • @JohnDoea, I found mktemp -d easier to use.
    – prosti
    44 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:



#!/bin/bash

scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)


Each time I run this command I see a new temporary directory created under /tmp/ (I didn't know it will appear there until reading Roaima's answer here):



IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).



If there is no programmatical difference, why should one prefer mktemp -d over the more minimal mkdir?










share|improve this question















I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:



#!/bin/bash

scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)


Each time I run this command I see a new temporary directory created under /tmp/ (I didn't know it will appear there until reading Roaima's answer here):



IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).



If there is no programmatical difference, why should one prefer mktemp -d over the more minimal mkdir?







directory mkdir mktemp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 42 mins ago

























asked 6 hours ago









JohnDoea

155726




155726











  • I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use mktemp when added security matters.
    – Mr Shunz
    6 hours ago











  • @JohnDoea, I found mktemp -d easier to use.
    – prosti
    44 mins ago
















  • I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use mktemp when added security matters.
    – Mr Shunz
    6 hours ago











  • @JohnDoea, I found mktemp -d easier to use.
    – prosti
    44 mins ago















I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use mktemp when added security matters.
– Mr Shunz
6 hours ago





I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use mktemp when added security matters.
– Mr Shunz
6 hours ago













@JohnDoea, I found mktemp -d easier to use.
– prosti
44 mins ago




@JohnDoea, I found mktemp -d easier to use.
– prosti
44 mins ago










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










When using mkdir, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname if dirname is an existing name in the current directory.



When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d will find a name that is not already taken by something else.



mktemp -d makes it easier and more secure to create a temporary directory. Without mktemp -d, one would have to try to mkdir with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).



mktemp also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d would then create the temporary directory beneath that path.






share|improve this answer





























    up vote
    1
    down vote













    The thing with mktemp -d since it creates a temporary directory with a random name is:
    You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.



    This is why you wrote scripttmp=$(mktemp -d) to hold the value. In general it is easy to use.



    Unless you don't care, you may use this little example to generate the random name and remove the folder later:



    var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
    mkdir "/tmp/$var"
    #some code
    rm -rf "/tmp/$var"



    Is there a programmatical difference between directories created with mktemp -d or mkdir?




    Well there is. With mkdir you generate the directory name, and with mktemp -d the system creates the directory for you based on /dev/urandom gettimeofday, getpid, ... and other functions to gain the name uniqueness.






    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%2f471589%2fis-there-a-programmatical-difference-between-directories-created-with-mktemp-d%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



      accepted










      When using mkdir, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname if dirname is an existing name in the current directory.



      When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d will find a name that is not already taken by something else.



      mktemp -d makes it easier and more secure to create a temporary directory. Without mktemp -d, one would have to try to mkdir with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).



      mktemp also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d would then create the temporary directory beneath that path.






      share|improve this answer


























        up vote
        5
        down vote



        accepted










        When using mkdir, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname if dirname is an existing name in the current directory.



        When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d will find a name that is not already taken by something else.



        mktemp -d makes it easier and more secure to create a temporary directory. Without mktemp -d, one would have to try to mkdir with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).



        mktemp also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d would then create the temporary directory beneath that path.






        share|improve this answer
























          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          When using mkdir, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname if dirname is an existing name in the current directory.



          When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d will find a name that is not already taken by something else.



          mktemp -d makes it easier and more secure to create a temporary directory. Without mktemp -d, one would have to try to mkdir with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).



          mktemp also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d would then create the temporary directory beneath that path.






          share|improve this answer














          When using mkdir, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname if dirname is an existing name in the current directory.



          When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d will find a name that is not already taken by something else.



          mktemp -d makes it easier and more secure to create a temporary directory. Without mktemp -d, one would have to try to mkdir with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).



          mktemp also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d would then create the temporary directory beneath that path.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered 6 hours ago









          Kusalananda

          107k14209331




          107k14209331






















              up vote
              1
              down vote













              The thing with mktemp -d since it creates a temporary directory with a random name is:
              You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.



              This is why you wrote scripttmp=$(mktemp -d) to hold the value. In general it is easy to use.



              Unless you don't care, you may use this little example to generate the random name and remove the folder later:



              var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
              mkdir "/tmp/$var"
              #some code
              rm -rf "/tmp/$var"



              Is there a programmatical difference between directories created with mktemp -d or mkdir?




              Well there is. With mkdir you generate the directory name, and with mktemp -d the system creates the directory for you based on /dev/urandom gettimeofday, getpid, ... and other functions to gain the name uniqueness.






              share|improve this answer


























                up vote
                1
                down vote













                The thing with mktemp -d since it creates a temporary directory with a random name is:
                You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.



                This is why you wrote scripttmp=$(mktemp -d) to hold the value. In general it is easy to use.



                Unless you don't care, you may use this little example to generate the random name and remove the folder later:



                var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
                mkdir "/tmp/$var"
                #some code
                rm -rf "/tmp/$var"



                Is there a programmatical difference between directories created with mktemp -d or mkdir?




                Well there is. With mkdir you generate the directory name, and with mktemp -d the system creates the directory for you based on /dev/urandom gettimeofday, getpid, ... and other functions to gain the name uniqueness.






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The thing with mktemp -d since it creates a temporary directory with a random name is:
                  You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.



                  This is why you wrote scripttmp=$(mktemp -d) to hold the value. In general it is easy to use.



                  Unless you don't care, you may use this little example to generate the random name and remove the folder later:



                  var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
                  mkdir "/tmp/$var"
                  #some code
                  rm -rf "/tmp/$var"



                  Is there a programmatical difference between directories created with mktemp -d or mkdir?




                  Well there is. With mkdir you generate the directory name, and with mktemp -d the system creates the directory for you based on /dev/urandom gettimeofday, getpid, ... and other functions to gain the name uniqueness.






                  share|improve this answer














                  The thing with mktemp -d since it creates a temporary directory with a random name is:
                  You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.



                  This is why you wrote scripttmp=$(mktemp -d) to hold the value. In general it is easy to use.



                  Unless you don't care, you may use this little example to generate the random name and remove the folder later:



                  var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
                  mkdir "/tmp/$var"
                  #some code
                  rm -rf "/tmp/$var"



                  Is there a programmatical difference between directories created with mktemp -d or mkdir?




                  Well there is. With mkdir you generate the directory name, and with mktemp -d the system creates the directory for you based on /dev/urandom gettimeofday, getpid, ... and other functions to gain the name uniqueness.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 47 mins ago

























                  answered 6 hours ago









                  prosti

                  20013




                  20013



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471589%2fis-there-a-programmatical-difference-between-directories-created-with-mktemp-d%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery