Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 477
hawe25 Avatar asked Nov 17 '25 07:11

hawe25


2 Answers

Here is one way to do it:

shopt -s dotglob extglob nullglob
zip -r project.zip Project/!(node_modules) Project/node_modules/mydir
like image 171
oguz ismail Avatar answered Nov 19 '25 20:11

oguz ismail


Try this:

shopt -s extglob
zip -r project.zip Project/ -x@<(printf "%s/*\n" Project/node_modules/!(mydir))
like image 36
Philippe Avatar answered Nov 19 '25 20:11

Philippe