77

I, need to zip a directory excluding some subdirectory and file; I used this:

zip -r zipfile.zip . -x ".*" -x "app/bower_components" -x "node_modules" -x "*.zip" -x "node_modules"

without any success; node_modules is a folder in the principal one while bower_components is inside the folder app

2
  • what is your result and what are your expected result?
    – D-E-N
    Nov 5, 2013 at 17:57
  • You want to exclude all subdirectories or only some of them ? Jun 10, 2015 at 17:16

7 Answers 7

65

I simply make a guess what you want.

-x ".*"

exclude all files beginning with a dot

do it like:

-x .\*

exclude all files (with a dot in the filename)

do it like:

-x \*.\*

--

-x "app/bower_components" -x "node_modules"

exclude this directory and all files in it

do it like:

-x app/bower_components/\* -x node_modules/\*

--

-x "*.zip"

exclude all zip-Files

do it like:

-x \*.zip

You exclude node_modules twice

4
  • 2
    thanks I got it, using various post arrived to that: zip -r * app/* -x\app/bower_components/* -x\node_modules/* -x*.zip
    – arpho
    Nov 7, 2013 at 15:34
  • @arpho Your comment above should be an answer as it appears to be working for me...
    – chesedo
    Jun 22, 2016 at 11:48
  • -x app/bower_components/* -x node_modules/* doesn work for me why?
    – jw_
    Jan 13, 2020 at 3:18
  • zip -r zipped.zip . -x "public/*" This is what worked for me after adding quotes
    – Gpak
    Jan 18, 2022 at 8:03
34

exclude all node_modules folders in all folders.

zip -r node.zip . -x "**/node_modules/*"
3
  • 6
    Thank you. This is the only answer that really works. Jan 6, 2020 at 18:54
  • Why not **/node_modules/** Jul 23, 2022 at 19:43
  • @JellyFilledNuts because by using /* you selecting all folders and files in path. With /** you just determinate any path but no content ... working exmp would be also /**/* at the end but its not necessary for this case
    – x-magix
    Jan 24 at 23:48
24

Assuming your directory is a git repository (and judging by the question, it very likely is), you can add directories that you want to exclude to the .gitignore file and use the git archive command to zip contents of your directory:

git archive --format=zip HEAD -o zipfile.zip

In your example, the .gitignore file would have to look like this:

node_modules
app/bower_components
14

Something like this should do the trick:

zip -r zipped.zip dir1/ -x */\.* *.git* \.* *.zip *.csv *.json *.rb *.bak *.swp *.back *.merge *.txt *.sh dir1/node_modules/**\* dir1/bower_components/**\* dir1/dist/**.*

Where following -x is a list of directories and file (extension) types to exclude.

1
  • 1
    Now that I know the **\* is for recursive exclusion. Many thanks.
    – Duc Tran
    Mar 9, 2017 at 23:31
3

This works perfect for me on Ubuntu 16.04:

sudo zip -r /home/user/backup/$(date +"%Y-%m-%d")/home_user.zip /home/user -x "*backup*" -x "*.cache*" -x "*test*"
1

in every project skip node_modules like this:

for i in */; do zip -r "${i%/}.zip" "$i" -x "*/\node_modules/*" ; done
1

In the folder you want to be zipped:

zip -r abc.zip . -x "node_modules/**" -x "*.git/**" 
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .