Using terminal to zip unknown amount of packages with dynamic names

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to figure out a solution for unix based systems (maxOS, Linux) to zip unknown amount of packages, ideally without requiring users to install any additional third party software.



I have folder structure like this



MyProject
/packages
/custom-modules
/Module1
/ios
/src
/Module2
/ios
/src
...


Amount of custom modules and their names can vary. I now need a solution that would allow me to zip all module src folders and name them accordingly i.e. final output would look like this



MyProject
/packages
/custom-modules
/Module1
/ios
/src
/Module1.zip
/Module2
/ios
/src
/Module2.zip
...


Ideally every time such command / script is ran it would delete old already existing zip files and generate new ones.










share|improve this question






























    1















    I am trying to figure out a solution for unix based systems (maxOS, Linux) to zip unknown amount of packages, ideally without requiring users to install any additional third party software.



    I have folder structure like this



    MyProject
    /packages
    /custom-modules
    /Module1
    /ios
    /src
    /Module2
    /ios
    /src
    ...


    Amount of custom modules and their names can vary. I now need a solution that would allow me to zip all module src folders and name them accordingly i.e. final output would look like this



    MyProject
    /packages
    /custom-modules
    /Module1
    /ios
    /src
    /Module1.zip
    /Module2
    /ios
    /src
    /Module2.zip
    ...


    Ideally every time such command / script is ran it would delete old already existing zip files and generate new ones.










    share|improve this question


























      1












      1








      1








      I am trying to figure out a solution for unix based systems (maxOS, Linux) to zip unknown amount of packages, ideally without requiring users to install any additional third party software.



      I have folder structure like this



      MyProject
      /packages
      /custom-modules
      /Module1
      /ios
      /src
      /Module2
      /ios
      /src
      ...


      Amount of custom modules and their names can vary. I now need a solution that would allow me to zip all module src folders and name them accordingly i.e. final output would look like this



      MyProject
      /packages
      /custom-modules
      /Module1
      /ios
      /src
      /Module1.zip
      /Module2
      /ios
      /src
      /Module2.zip
      ...


      Ideally every time such command / script is ran it would delete old already existing zip files and generate new ones.










      share|improve this question
















      I am trying to figure out a solution for unix based systems (maxOS, Linux) to zip unknown amount of packages, ideally without requiring users to install any additional third party software.



      I have folder structure like this



      MyProject
      /packages
      /custom-modules
      /Module1
      /ios
      /src
      /Module2
      /ios
      /src
      ...


      Amount of custom modules and their names can vary. I now need a solution that would allow me to zip all module src folders and name them accordingly i.e. final output would look like this



      MyProject
      /packages
      /custom-modules
      /Module1
      /ios
      /src
      /Module1.zip
      /Module2
      /ios
      /src
      /Module2.zip
      ...


      Ideally every time such command / script is ran it would delete old already existing zip files and generate new ones.







      linux command-line osx zip compression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 16 at 14:27









      Jeff Schaller

      45k1164147




      45k1164147










      asked Mar 16 at 14:24









      IljaIlja

      1084




      1084




















          1 Answer
          1






          active

          oldest

          votes


















          2














          Starting from the MyProject/packages/custom-modules directory, this one-liner can do the job:



          for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          The idea here is get all the module directory names using the wildcard/glob. Then for each directory, change to the 'ios' subdirectory and run the zip command. It's possible to execute the zip command directly, but that will include the extended path in the archive, which you may not want. Finally, jump back to the parent directory and continue with the next iteration.



          Here's a demo. This is the original directory structure:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          └── src
          ├── file1
          └── file2

          6 directories, 4 files


          And now we run the command:



          [haxiel@testvm1 custom-modules]$ for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          And the resulting directory structure is this:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   ├── Module1.zip
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          ├── Module2.zip
          └── src
          ├── file1
          └── file2

          6 directories, 6 files


          And here is a sample ZIP file showing its contents:



          [haxiel@testvm1 custom-modules]$ unzip -l Module1/ios/Module1.zip
          Archive: Module1/ios/Module1.zip
          Length Date Time Name
          --------- ---------- ----- ----
          0 03-16-2019 20:42 src/
          0 03-16-2019 20:42 src/file1
          0 03-16-2019 20:42 src/file2
          --------- -------
          0 3 files





          share|improve this answer























          • This is elegant sollution and works very well, thank you :)

            – Ilja
            Mar 17 at 6:12











          • there are some .json config files inside custom-modules folder, and script attempts to zip those as well i.e. config.json/ios... is there a way to indicate that only folders should be used?

            – Ilja
            Mar 17 at 6:36











          • if [ -d "$module" ]; does the trick

            – Ilja
            Mar 17 at 6:38











          • @Ilja I had left the error handling to the && operator here. If the first cd command fails for any reason, the zip command is not executed. If you don't want to see the failure cases, you could simply ignore the error stream with cd "$module/ios/" 2> /dev/null.

            – Haxiel
            Mar 17 at 7:52











          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',
          autoActivateHeartbeat: false,
          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
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506682%2fusing-terminal-to-zip-unknown-amount-of-packages-with-dynamic-names%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Starting from the MyProject/packages/custom-modules directory, this one-liner can do the job:



          for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          The idea here is get all the module directory names using the wildcard/glob. Then for each directory, change to the 'ios' subdirectory and run the zip command. It's possible to execute the zip command directly, but that will include the extended path in the archive, which you may not want. Finally, jump back to the parent directory and continue with the next iteration.



          Here's a demo. This is the original directory structure:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          └── src
          ├── file1
          └── file2

          6 directories, 4 files


          And now we run the command:



          [haxiel@testvm1 custom-modules]$ for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          And the resulting directory structure is this:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   ├── Module1.zip
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          ├── Module2.zip
          └── src
          ├── file1
          └── file2

          6 directories, 6 files


          And here is a sample ZIP file showing its contents:



          [haxiel@testvm1 custom-modules]$ unzip -l Module1/ios/Module1.zip
          Archive: Module1/ios/Module1.zip
          Length Date Time Name
          --------- ---------- ----- ----
          0 03-16-2019 20:42 src/
          0 03-16-2019 20:42 src/file1
          0 03-16-2019 20:42 src/file2
          --------- -------
          0 3 files





          share|improve this answer























          • This is elegant sollution and works very well, thank you :)

            – Ilja
            Mar 17 at 6:12











          • there are some .json config files inside custom-modules folder, and script attempts to zip those as well i.e. config.json/ios... is there a way to indicate that only folders should be used?

            – Ilja
            Mar 17 at 6:36











          • if [ -d "$module" ]; does the trick

            – Ilja
            Mar 17 at 6:38











          • @Ilja I had left the error handling to the && operator here. If the first cd command fails for any reason, the zip command is not executed. If you don't want to see the failure cases, you could simply ignore the error stream with cd "$module/ios/" 2> /dev/null.

            – Haxiel
            Mar 17 at 7:52















          2














          Starting from the MyProject/packages/custom-modules directory, this one-liner can do the job:



          for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          The idea here is get all the module directory names using the wildcard/glob. Then for each directory, change to the 'ios' subdirectory and run the zip command. It's possible to execute the zip command directly, but that will include the extended path in the archive, which you may not want. Finally, jump back to the parent directory and continue with the next iteration.



          Here's a demo. This is the original directory structure:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          └── src
          ├── file1
          └── file2

          6 directories, 4 files


          And now we run the command:



          [haxiel@testvm1 custom-modules]$ for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          And the resulting directory structure is this:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   ├── Module1.zip
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          ├── Module2.zip
          └── src
          ├── file1
          └── file2

          6 directories, 6 files


          And here is a sample ZIP file showing its contents:



          [haxiel@testvm1 custom-modules]$ unzip -l Module1/ios/Module1.zip
          Archive: Module1/ios/Module1.zip
          Length Date Time Name
          --------- ---------- ----- ----
          0 03-16-2019 20:42 src/
          0 03-16-2019 20:42 src/file1
          0 03-16-2019 20:42 src/file2
          --------- -------
          0 3 files





          share|improve this answer























          • This is elegant sollution and works very well, thank you :)

            – Ilja
            Mar 17 at 6:12











          • there are some .json config files inside custom-modules folder, and script attempts to zip those as well i.e. config.json/ios... is there a way to indicate that only folders should be used?

            – Ilja
            Mar 17 at 6:36











          • if [ -d "$module" ]; does the trick

            – Ilja
            Mar 17 at 6:38











          • @Ilja I had left the error handling to the && operator here. If the first cd command fails for any reason, the zip command is not executed. If you don't want to see the failure cases, you could simply ignore the error stream with cd "$module/ios/" 2> /dev/null.

            – Haxiel
            Mar 17 at 7:52













          2












          2








          2







          Starting from the MyProject/packages/custom-modules directory, this one-liner can do the job:



          for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          The idea here is get all the module directory names using the wildcard/glob. Then for each directory, change to the 'ios' subdirectory and run the zip command. It's possible to execute the zip command directly, but that will include the extended path in the archive, which you may not want. Finally, jump back to the parent directory and continue with the next iteration.



          Here's a demo. This is the original directory structure:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          └── src
          ├── file1
          └── file2

          6 directories, 4 files


          And now we run the command:



          [haxiel@testvm1 custom-modules]$ for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          And the resulting directory structure is this:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   ├── Module1.zip
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          ├── Module2.zip
          └── src
          ├── file1
          └── file2

          6 directories, 6 files


          And here is a sample ZIP file showing its contents:



          [haxiel@testvm1 custom-modules]$ unzip -l Module1/ios/Module1.zip
          Archive: Module1/ios/Module1.zip
          Length Date Time Name
          --------- ---------- ----- ----
          0 03-16-2019 20:42 src/
          0 03-16-2019 20:42 src/file1
          0 03-16-2019 20:42 src/file2
          --------- -------
          0 3 files





          share|improve this answer













          Starting from the MyProject/packages/custom-modules directory, this one-liner can do the job:



          for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          The idea here is get all the module directory names using the wildcard/glob. Then for each directory, change to the 'ios' subdirectory and run the zip command. It's possible to execute the zip command directly, but that will include the extended path in the archive, which you may not want. Finally, jump back to the parent directory and continue with the next iteration.



          Here's a demo. This is the original directory structure:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          └── src
          ├── file1
          └── file2

          6 directories, 4 files


          And now we run the command:



          [haxiel@testvm1 custom-modules]$ for module in * ; do cd "$module/ios/" && zip -qr "$module.zip" src/ && cd - &> /dev/null ; done


          And the resulting directory structure is this:



          [haxiel@testvm1 custom-modules]$ tree
          .
          ├── Module1
          │   └── ios
          │   ├── Module1.zip
          │   └── src
          │   ├── file1
          │   └── file2
          └── Module2
          └── ios
          ├── Module2.zip
          └── src
          ├── file1
          └── file2

          6 directories, 6 files


          And here is a sample ZIP file showing its contents:



          [haxiel@testvm1 custom-modules]$ unzip -l Module1/ios/Module1.zip
          Archive: Module1/ios/Module1.zip
          Length Date Time Name
          --------- ---------- ----- ----
          0 03-16-2019 20:42 src/
          0 03-16-2019 20:42 src/file1
          0 03-16-2019 20:42 src/file2
          --------- -------
          0 3 files






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 16 at 15:42









          HaxielHaxiel

          3,62811021




          3,62811021












          • This is elegant sollution and works very well, thank you :)

            – Ilja
            Mar 17 at 6:12











          • there are some .json config files inside custom-modules folder, and script attempts to zip those as well i.e. config.json/ios... is there a way to indicate that only folders should be used?

            – Ilja
            Mar 17 at 6:36











          • if [ -d "$module" ]; does the trick

            – Ilja
            Mar 17 at 6:38











          • @Ilja I had left the error handling to the && operator here. If the first cd command fails for any reason, the zip command is not executed. If you don't want to see the failure cases, you could simply ignore the error stream with cd "$module/ios/" 2> /dev/null.

            – Haxiel
            Mar 17 at 7:52

















          • This is elegant sollution and works very well, thank you :)

            – Ilja
            Mar 17 at 6:12











          • there are some .json config files inside custom-modules folder, and script attempts to zip those as well i.e. config.json/ios... is there a way to indicate that only folders should be used?

            – Ilja
            Mar 17 at 6:36











          • if [ -d "$module" ]; does the trick

            – Ilja
            Mar 17 at 6:38











          • @Ilja I had left the error handling to the && operator here. If the first cd command fails for any reason, the zip command is not executed. If you don't want to see the failure cases, you could simply ignore the error stream with cd "$module/ios/" 2> /dev/null.

            – Haxiel
            Mar 17 at 7:52
















          This is elegant sollution and works very well, thank you :)

          – Ilja
          Mar 17 at 6:12





          This is elegant sollution and works very well, thank you :)

          – Ilja
          Mar 17 at 6:12













          there are some .json config files inside custom-modules folder, and script attempts to zip those as well i.e. config.json/ios... is there a way to indicate that only folders should be used?

          – Ilja
          Mar 17 at 6:36





          there are some .json config files inside custom-modules folder, and script attempts to zip those as well i.e. config.json/ios... is there a way to indicate that only folders should be used?

          – Ilja
          Mar 17 at 6:36













          if [ -d "$module" ]; does the trick

          – Ilja
          Mar 17 at 6:38





          if [ -d "$module" ]; does the trick

          – Ilja
          Mar 17 at 6:38













          @Ilja I had left the error handling to the && operator here. If the first cd command fails for any reason, the zip command is not executed. If you don't want to see the failure cases, you could simply ignore the error stream with cd "$module/ios/" 2> /dev/null.

          – Haxiel
          Mar 17 at 7:52





          @Ilja I had left the error handling to the && operator here. If the first cd command fails for any reason, the zip command is not executed. If you don't want to see the failure cases, you could simply ignore the error stream with cd "$module/ios/" 2> /dev/null.

          – Haxiel
          Mar 17 at 7:52

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506682%2fusing-terminal-to-zip-unknown-amount-of-packages-with-dynamic-names%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown






          Popular posts from this blog

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

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?