Preserve ownership of entire folder hierarchy in tar?

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











up vote
3
down vote

favorite












I can preserve ownership of folderB and all files and folders inside when creating and extracting a tar file as follows:



tar -cpf out.tar folderA/folderB
sudo tar -xpf out.tar --same-owner


However, folderA is owned by root when extracting unless the folder already exists. Is there any way to preserve ownership of the entire folder hierarchy with tar?







share|improve this question

























    up vote
    3
    down vote

    favorite












    I can preserve ownership of folderB and all files and folders inside when creating and extracting a tar file as follows:



    tar -cpf out.tar folderA/folderB
    sudo tar -xpf out.tar --same-owner


    However, folderA is owned by root when extracting unless the folder already exists. Is there any way to preserve ownership of the entire folder hierarchy with tar?







    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I can preserve ownership of folderB and all files and folders inside when creating and extracting a tar file as follows:



      tar -cpf out.tar folderA/folderB
      sudo tar -xpf out.tar --same-owner


      However, folderA is owned by root when extracting unless the folder already exists. Is there any way to preserve ownership of the entire folder hierarchy with tar?







      share|improve this question













      I can preserve ownership of folderB and all files and folders inside when creating and extracting a tar file as follows:



      tar -cpf out.tar folderA/folderB
      sudo tar -xpf out.tar --same-owner


      However, folderA is owned by root when extracting unless the folder already exists. Is there any way to preserve ownership of the entire folder hierarchy with tar?









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 18 at 22:42
























      asked May 18 at 19:24









      user276833

      285




      285




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          This happens because



          tar -cpf out.tar folderA/folderB


          doesn’t store folderA as a separate object in the tarball, so it doesn’t have any way of recording the ownership and permissions of folderA.



          To preserve the ownership, you need to tell tar to do so when you create the tarball; with GNU tar at least, the following works:



          tar -cpf out.tar --no-recursion folderA --recursion folderA/folderB


          This stores folderA (and its permissions etc.) without recursing, and folderA/folderB with its contents.






          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%2f444686%2fpreserve-ownership-of-entire-folder-hierarchy-in-tar%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            This happens because



            tar -cpf out.tar folderA/folderB


            doesn’t store folderA as a separate object in the tarball, so it doesn’t have any way of recording the ownership and permissions of folderA.



            To preserve the ownership, you need to tell tar to do so when you create the tarball; with GNU tar at least, the following works:



            tar -cpf out.tar --no-recursion folderA --recursion folderA/folderB


            This stores folderA (and its permissions etc.) without recursing, and folderA/folderB with its contents.






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              This happens because



              tar -cpf out.tar folderA/folderB


              doesn’t store folderA as a separate object in the tarball, so it doesn’t have any way of recording the ownership and permissions of folderA.



              To preserve the ownership, you need to tell tar to do so when you create the tarball; with GNU tar at least, the following works:



              tar -cpf out.tar --no-recursion folderA --recursion folderA/folderB


              This stores folderA (and its permissions etc.) without recursing, and folderA/folderB with its contents.






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                This happens because



                tar -cpf out.tar folderA/folderB


                doesn’t store folderA as a separate object in the tarball, so it doesn’t have any way of recording the ownership and permissions of folderA.



                To preserve the ownership, you need to tell tar to do so when you create the tarball; with GNU tar at least, the following works:



                tar -cpf out.tar --no-recursion folderA --recursion folderA/folderB


                This stores folderA (and its permissions etc.) without recursing, and folderA/folderB with its contents.






                share|improve this answer













                This happens because



                tar -cpf out.tar folderA/folderB


                doesn’t store folderA as a separate object in the tarball, so it doesn’t have any way of recording the ownership and permissions of folderA.



                To preserve the ownership, you need to tell tar to do so when you create the tarball; with GNU tar at least, the following works:



                tar -cpf out.tar --no-recursion folderA --recursion folderA/folderB


                This stores folderA (and its permissions etc.) without recursing, and folderA/folderB with its contents.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 18 at 19:45









                Stephen Kitt

                140k22302363




                140k22302363






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444686%2fpreserve-ownership-of-entire-folder-hierarchy-in-tar%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)