Zip with ignore within Bash script

187 views Asked by At

Found a wonderful little bash script that I've adapted to use for zip compressing managed directories, ignoring files like bower_components, .git and node_modules:

#!/bin/bash
# This script zips a directory, excluding specified files, types and subdirectories.
#  while zipping the directory it excludes hidden directories and certain file types

[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.bash_profile

DIRECTORY=$(cd `dirname $0` && pwd)

if [[ -z $1 ]]; then
  echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
else
  DIRECTORY_TO_COMPRESS=$1
  ZIPPED_FILE="$2.zip"
  COMPRESS_IGNORE_DIR=("\.git" "node_modules" "bower_components")

  IGNORE_LIST=("*/\.*" "\.* "\/\.*"")
  if [[ -n $COMPRESS_IGNORE_DIR ]]; then
      for IGNORE_DIR in "${COMPRESS_IGNORE_DIR[@]}"; do
          IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/***")  ## "$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/*"  perhaps is enough?
      done
  fi

  zip -r "$ZIPPED_FILE" "$DIRECTORY_TO_COMPRESS" -x "${IGNORE_LIST[@]}" # >/dev/null
  echo zip -r "$ZIPPED_FILE" "$DIRECTORY_TO_COMPRESS" -x "${IGNORE_LIST[@]}" # >/dev/null
  echo "Done"
fi

The only problem is that the directories I want to ignore are still being created, just empty.

Any suggestions?

1

There are 1 answers

0
leu On BEST ANSWER

The zip-man-page says

Note that currently the trailing / is needed for directories (as in

zip -r foo . -i dir/

to include directory dir).

So, likewise exclusion seems to work: Replacing the marked line in your script by

IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/")

should do the trick.

(my zip is version 3.0 on Ubuntu-Linux)