Linux

How to Recursively Zip a Folder While Ignoring Specific Directories

Zachary Carciu
Advertisement

How to Recursively Zip a Folder While Ignoring Specific Directories

When zipping a project folder, you may want to exclude large directories like node_modules to reduce file size and speed up compression. The zip command in Linux and macOS provides an easy way to do this using the -x flag.

Recursively Zip a Folder While Ignoring node_modules

To create a ZIP archive of a project folder while ignoring node_modules, use the following command:

zip -r my-project.zip project/ -x "project/node_modules/*"

Explanation:

  • zip -r my-project.zip project/ → Recursively zip the project/ directory into my-project.zip.
  • -x "project/node_modules/*" → Exclude everything inside project/node_modules/.

Exclude Multiple Directories

If you need to exclude multiple directories (e.g., .git, tmp), you can extend the -x option:

zip -r my-project.zip project/ -x "project/node_modules/*" "project/.git/*" "project/tmp/*"

Alternative: Zipping from Within the Project Directory

If you are inside the project/ directory, you can use:

cd project
zip -r ../my-project.zip . -x "node_modules/*"

This ensures node_modules is ignored while compressing everything else in project/.

Conclusion

Using the zip command with the -x flag allows you to efficiently create ZIP archives while excluding unnecessary files or directories. This is particularly useful when sharing projects or creating backups without including large dependencies like node_modules.

Advertisement