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.

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
4
  • This is elegant sollution and works very well, thank you :) – Ilja Mar 17 '19 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 '19 at 6:36
  • if [ -d "${module}" ]; does the trick – Ilja Mar 17 '19 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 '19 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.