Temporary folder that automatically destroyed after process exit

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











up vote
3
down vote

favorite












Can we use temporary folders like temporary files



TMP=$(mktemp ... )
exec 3<>$TMP
rm $TMP

cat <&3


which will be destroyed automatically after this shell exit?










share|improve this question







New contributor




Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    3
    down vote

    favorite












    Can we use temporary folders like temporary files



    TMP=$(mktemp ... )
    exec 3<>$TMP
    rm $TMP

    cat <&3


    which will be destroyed automatically after this shell exit?










    share|improve this question







    New contributor




    Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Can we use temporary folders like temporary files



      TMP=$(mktemp ... )
      exec 3<>$TMP
      rm $TMP

      cat <&3


      which will be destroyed automatically after this shell exit?










      share|improve this question







      New contributor




      Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Can we use temporary folders like temporary files



      TMP=$(mktemp ... )
      exec 3<>$TMP
      rm $TMP

      cat <&3


      which will be destroyed automatically after this shell exit?







      file-descriptors tmpfs






      share|improve this question







      New contributor




      Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      Bob Johnson

      241




      241




      New contributor




      Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.



          It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.



          A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT trap:



          tmpdir=$(mktemp -d)
          tmpfile=$(mktemp)

          trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT

          # The rest of the script goes here.


          Or you may call a cleanup function:



          cleanup () 
          rm -f "$tmpfile"
          rm -rf "$tmpdir"


          tmpdir=$(mktemp -d)
          tmpfile=$(mktemp)

          trap cleanup EXIT

          # The rest of the script goes here.





          share|improve this answer





























            up vote
            1
            down vote













            Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6



            trap cleanup 0 1 2 3 6

            cleanup()

            [ -d $TMP ] && rm -rf $TMP



            See this post for more info.






            share|improve this answer



























              up vote
              1
              down vote













              You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:



              #! /bin/sh
              dir=`mktemp -d`
              cd "$dir"
              exec 4>file 3<file
              rm -fr "$dir"

              echo yes >&4 # OK
              cat <&3 # OK

              cat file # FAIL
              echo yes > file # FAIL


              I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.



              If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir inside it.



              The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.






              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: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );






                Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.









                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480330%2ftemporary-folder-that-automatically-destroyed-after-process-exit%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                3
                down vote













                In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.



                It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.



                A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT trap:



                tmpdir=$(mktemp -d)
                tmpfile=$(mktemp)

                trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT

                # The rest of the script goes here.


                Or you may call a cleanup function:



                cleanup () 
                rm -f "$tmpfile"
                rm -rf "$tmpdir"


                tmpdir=$(mktemp -d)
                tmpfile=$(mktemp)

                trap cleanup EXIT

                # The rest of the script goes here.





                share|improve this answer


























                  up vote
                  3
                  down vote













                  In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.



                  It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.



                  A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT trap:



                  tmpdir=$(mktemp -d)
                  tmpfile=$(mktemp)

                  trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT

                  # The rest of the script goes here.


                  Or you may call a cleanup function:



                  cleanup () 
                  rm -f "$tmpfile"
                  rm -rf "$tmpdir"


                  tmpdir=$(mktemp -d)
                  tmpfile=$(mktemp)

                  trap cleanup EXIT

                  # The rest of the script goes here.





                  share|improve this answer
























                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.



                    It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.



                    A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT trap:



                    tmpdir=$(mktemp -d)
                    tmpfile=$(mktemp)

                    trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT

                    # The rest of the script goes here.


                    Or you may call a cleanup function:



                    cleanup () 
                    rm -f "$tmpfile"
                    rm -rf "$tmpdir"


                    tmpdir=$(mktemp -d)
                    tmpfile=$(mktemp)

                    trap cleanup EXIT

                    # The rest of the script goes here.





                    share|improve this answer














                    In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.



                    It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.



                    A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT trap:



                    tmpdir=$(mktemp -d)
                    tmpfile=$(mktemp)

                    trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT

                    # The rest of the script goes here.


                    Or you may call a cleanup function:



                    cleanup () 
                    rm -f "$tmpfile"
                    rm -rf "$tmpdir"


                    tmpdir=$(mktemp -d)
                    tmpfile=$(mktemp)

                    trap cleanup EXIT

                    # The rest of the script goes here.






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 1 hour ago

























                    answered 2 hours ago









                    Kusalananda

                    113k15217345




                    113k15217345






















                        up vote
                        1
                        down vote













                        Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6



                        trap cleanup 0 1 2 3 6

                        cleanup()

                        [ -d $TMP ] && rm -rf $TMP



                        See this post for more info.






                        share|improve this answer
























                          up vote
                          1
                          down vote













                          Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6



                          trap cleanup 0 1 2 3 6

                          cleanup()

                          [ -d $TMP ] && rm -rf $TMP



                          See this post for more info.






                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6



                            trap cleanup 0 1 2 3 6

                            cleanup()

                            [ -d $TMP ] && rm -rf $TMP



                            See this post for more info.






                            share|improve this answer












                            Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6



                            trap cleanup 0 1 2 3 6

                            cleanup()

                            [ -d $TMP ] && rm -rf $TMP



                            See this post for more info.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 hours ago









                            Dirk Krijgsman

                            4546




                            4546




















                                up vote
                                1
                                down vote













                                You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:



                                #! /bin/sh
                                dir=`mktemp -d`
                                cd "$dir"
                                exec 4>file 3<file
                                rm -fr "$dir"

                                echo yes >&4 # OK
                                cat <&3 # OK

                                cat file # FAIL
                                echo yes > file # FAIL


                                I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.



                                If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir inside it.



                                The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.






                                share|improve this answer


























                                  up vote
                                  1
                                  down vote













                                  You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:



                                  #! /bin/sh
                                  dir=`mktemp -d`
                                  cd "$dir"
                                  exec 4>file 3<file
                                  rm -fr "$dir"

                                  echo yes >&4 # OK
                                  cat <&3 # OK

                                  cat file # FAIL
                                  echo yes > file # FAIL


                                  I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.



                                  If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir inside it.



                                  The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.






                                  share|improve this answer
























                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:



                                    #! /bin/sh
                                    dir=`mktemp -d`
                                    cd "$dir"
                                    exec 4>file 3<file
                                    rm -fr "$dir"

                                    echo yes >&4 # OK
                                    cat <&3 # OK

                                    cat file # FAIL
                                    echo yes > file # FAIL


                                    I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.



                                    If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir inside it.



                                    The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.






                                    share|improve this answer














                                    You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:



                                    #! /bin/sh
                                    dir=`mktemp -d`
                                    cd "$dir"
                                    exec 4>file 3<file
                                    rm -fr "$dir"

                                    echo yes >&4 # OK
                                    cat <&3 # OK

                                    cat file # FAIL
                                    echo yes > file # FAIL


                                    I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.



                                    If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir inside it.



                                    The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 17 mins ago

























                                    answered 59 mins ago









                                    qubert

                                    3955




                                    3955




















                                        Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.









                                         

                                        draft saved


                                        draft discarded


















                                        Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.












                                        Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.











                                        Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.













                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480330%2ftemporary-folder-that-automatically-destroyed-after-process-exit%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        List of Gilmore Girls characters

                                        One-line joke