How to know in advance if a .zip has a parent directory inside

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











up vote
-2
down vote

favorite
1












On several occasions, say I have a.zip, which unzips into a/* itself.



However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a and unzip into it.



But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/* which is not ideal.



Is there a way to unzip a.zip into a/* irrespective of its type among the two types which I described above?







share|improve this question
























    up vote
    -2
    down vote

    favorite
    1












    On several occasions, say I have a.zip, which unzips into a/* itself.



    However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a and unzip into it.



    But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/* which is not ideal.



    Is there a way to unzip a.zip into a/* irrespective of its type among the two types which I described above?







    share|improve this question






















      up vote
      -2
      down vote

      favorite
      1









      up vote
      -2
      down vote

      favorite
      1






      1





      On several occasions, say I have a.zip, which unzips into a/* itself.



      However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a and unzip into it.



      But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/* which is not ideal.



      Is there a way to unzip a.zip into a/* irrespective of its type among the two types which I described above?







      share|improve this question












      On several occasions, say I have a.zip, which unzips into a/* itself.



      However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a and unzip into it.



      But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/* which is not ideal.



      Is there a way to unzip a.zip into a/* irrespective of its type among the two types which I described above?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '17 at 10:04









      Cheeku

      993




      993




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Try this:



          archive="archive.zip"
          has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
          if test "$has_parent" -eq 1; then
          unzip $archive
          else
          dir="./$(basename $archive%%.zip)"
          mkdir "$dir"
          unzip -d "$dir" $archive
          fi


          If using zipinfo you can squeeze $has_parent line to this:



          has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)


          Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.






          share|improve this answer





























            up vote
            1
            down vote













            Use zipinfo for displaying, zip contents.:



            $ zipinfo -1 a.zip


            Though, this probably doesn't answer your question if you are looking for an automation answer.



            Edit: What you could do is check each line of output from zipinfo for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.



            Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:



            $ MKROOT=0
            $ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
            $ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi


            Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.






            share|improve this answer


















            • 1




              zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l will output 1 if all paths have the same leading directory.
              – Mark Perryman
              Nov 28 '17 at 10:47











            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%2f407453%2fhow-to-know-in-advance-if-a-zip-has-a-parent-directory-inside%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
            1
            down vote



            accepted










            Try this:



            archive="archive.zip"
            has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
            if test "$has_parent" -eq 1; then
            unzip $archive
            else
            dir="./$(basename $archive%%.zip)"
            mkdir "$dir"
            unzip -d "$dir" $archive
            fi


            If using zipinfo you can squeeze $has_parent line to this:



            has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)


            Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.






            share|improve this answer


























              up vote
              1
              down vote



              accepted










              Try this:



              archive="archive.zip"
              has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
              if test "$has_parent" -eq 1; then
              unzip $archive
              else
              dir="./$(basename $archive%%.zip)"
              mkdir "$dir"
              unzip -d "$dir" $archive
              fi


              If using zipinfo you can squeeze $has_parent line to this:



              has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)


              Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.






              share|improve this answer
























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Try this:



                archive="archive.zip"
                has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
                if test "$has_parent" -eq 1; then
                unzip $archive
                else
                dir="./$(basename $archive%%.zip)"
                mkdir "$dir"
                unzip -d "$dir" $archive
                fi


                If using zipinfo you can squeeze $has_parent line to this:



                has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)


                Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.






                share|improve this answer














                Try this:



                archive="archive.zip"
                has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
                if test "$has_parent" -eq 1; then
                unzip $archive
                else
                dir="./$(basename $archive%%.zip)"
                mkdir "$dir"
                unzip -d "$dir" $archive
                fi


                If using zipinfo you can squeeze $has_parent line to this:



                has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)


                Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 28 '17 at 11:15

























                answered Nov 28 '17 at 10:50









                NarūnasK

                8021618




                8021618






















                    up vote
                    1
                    down vote













                    Use zipinfo for displaying, zip contents.:



                    $ zipinfo -1 a.zip


                    Though, this probably doesn't answer your question if you are looking for an automation answer.



                    Edit: What you could do is check each line of output from zipinfo for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.



                    Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:



                    $ MKROOT=0
                    $ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
                    $ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi


                    Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.






                    share|improve this answer


















                    • 1




                      zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l will output 1 if all paths have the same leading directory.
                      – Mark Perryman
                      Nov 28 '17 at 10:47















                    up vote
                    1
                    down vote













                    Use zipinfo for displaying, zip contents.:



                    $ zipinfo -1 a.zip


                    Though, this probably doesn't answer your question if you are looking for an automation answer.



                    Edit: What you could do is check each line of output from zipinfo for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.



                    Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:



                    $ MKROOT=0
                    $ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
                    $ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi


                    Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.






                    share|improve this answer


















                    • 1




                      zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l will output 1 if all paths have the same leading directory.
                      – Mark Perryman
                      Nov 28 '17 at 10:47













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Use zipinfo for displaying, zip contents.:



                    $ zipinfo -1 a.zip


                    Though, this probably doesn't answer your question if you are looking for an automation answer.



                    Edit: What you could do is check each line of output from zipinfo for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.



                    Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:



                    $ MKROOT=0
                    $ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
                    $ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi


                    Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.






                    share|improve this answer














                    Use zipinfo for displaying, zip contents.:



                    $ zipinfo -1 a.zip


                    Though, this probably doesn't answer your question if you are looking for an automation answer.



                    Edit: What you could do is check each line of output from zipinfo for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.



                    Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:



                    $ MKROOT=0
                    $ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
                    $ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi


                    Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 28 '17 at 11:07

























                    answered Nov 28 '17 at 10:41









                    AntumDeluge

                    397




                    397







                    • 1




                      zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l will output 1 if all paths have the same leading directory.
                      – Mark Perryman
                      Nov 28 '17 at 10:47













                    • 1




                      zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l will output 1 if all paths have the same leading directory.
                      – Mark Perryman
                      Nov 28 '17 at 10:47








                    1




                    1




                    zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l will output 1 if all paths have the same leading directory.
                    – Mark Perryman
                    Nov 28 '17 at 10:47





                    zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l will output 1 if all paths have the same leading directory.
                    – Mark Perryman
                    Nov 28 '17 at 10:47


















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407453%2fhow-to-know-in-advance-if-a-zip-has-a-parent-directory-inside%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