bashdirectoryzipsubdirectory

Zip exclude all subfolder except one subfolder and its files


I have problem when zipping a folder, lets say I have folder Project with node_modules to be excluded, but I want to include only node_modules/mydir in my zip.

I tried with zip -r project.zip Project/ -x Project/node_modules/\* surely it removes mydir and also not zipping node_modules folder.

for now I just do a double zip, not that efficient, but do the job. currently looking for more efficient ways.

Anyone has any clue to achieve that? Or I have to do loop on -x "node_modules/<otherfolder>" leaving node_modules/mydir included?


Solution

  • Here is one way to do it:

    shopt -s dotglob extglob nullglob
    zip -r project.zip Project/!(node_modules) Project/node_modules/mydir