Home > Net >  Zip script to enforce a particular zipped directory structure
Zip script to enforce a particular zipped directory structure

Time:08-20

So I have the following project directory structure:

myapp/
  package/
    <PACKAGE_CONTENTS>
    (this is a very deeply nested directory structure under here
     with many files and subdirs)
  fizz/
    a.txt
    b.jpg
    c.pdf
    <lots more files and subdirs here>
  buzz/
    d.json
    e.xml
    <lots more files and subdirs here>
  foobaz/
    f.yml
    g.py
    <lots more files and subdirs here>
  flimflam.yarp

What I need (end result) is a ZIP file that, if I were to unzip it, would have the following directory structure:

flimflam.yarp
<PACKAGE_CONTENTS>
fizz/
  a.txt
  b.jpg
  c.pdf
  <lots more files and subdirs here>
buzz/
  d.json
  e.xml
  <lots more files and subdirs here>
foobaz/
  f.yml
  g.py
  <lots more files and subdirs here>

Where <PACKAGE_CONTENTS> is everything inside the myapp/package/ directory, preserved exactly as it appears in myapp/package/, but not under myapp/package/. Hence if the only thing inside myapp/package/ was a file called bears.boop, then the unzipped ZIP file would produce:

flimflam.yarp
bears.boop
fizz/
  a.txt
  b.jpg
  c.pdf
  <lots more files and subdirs here>
buzz/
  d.json
  e.xml
  <lots more files and subdirs here>
foobaz/
  f.yml
  g.py
  <lots more files and subdirs here>

But again, myapp/package/ is a very large directory with many levels of files and subdirs.

So far I have worked out:

cd package
zip -r ../myapp.zip .
cd ..
zip -g myapp.zip flimflam.yarp

And this works, when I unzip myapp.zip I get:

flimflam.yarp
<PACKAGE_CONTENTS>

So far so good. But now I need to include the fizz, buzz and foobaz directories, and in such a way that their directory structure is perfectly preserved from the root.. What zip command and/or bash scripting can I leverage here to recursively crawl these other directories and place them in the ZIP as I need them to be?

CodePudding user response:

You should try passing all contents of myapp except package to the second zip command like so:

cd package
zip -r ../myapp.zip .
cd ..
shopt -s extglob
zip -g -r myapp.zip !(package)
  • Related