patternbashgitMinor
Bash Compress Managed Directory Ignoring .git, bower_components, node_modules, etc
Viewed 0 times
manageddirectorybower_componentsnode_modulescompressgitignoringbashetc
Problem
Fairly easy way to compress a wordpress plugin (or similar) as a zip file without including all the dependencies used to create and manage the development, but which don't belong in the final product.
#!/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_FILE=("\.git" "*.zip" "*.csv" "*.json" "gulpfile.js" "*.rb" "*.bak" "*.swp" "*.back" "*.merge" "*.txt" "*.sh" "bower_components" "node_modules")
COMPRESS_IGNORE_DIR=("bower_components" "node_modules")
IGNORE_LIST=("*/\.*" "\.* "\/\.*"")
if [[ -n $COMPRESS_IGNORE_FILE ]]; then
for IGNORE_FILES in "${COMPRESS_IGNORE_FILE[@]}"; do
IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_FILES/*")
done
for IGNORE_DIR in "${COMPRESS_IGNORE_DIR[@]}"; do
IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/")
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 $DIRECTORY_TO_COMPRESS "compressed as" $ZIPPED_FILE.
fiSolution
I'm just going to poke at some of your unix conventions and validation.
Your general handling looks polished. I like the way that you handle whether this is an interactive terminal, but wonder whether it would simply be better to use the more standard check for
The way you find the user directory is great, and is similar to what I normally do too. There are some variations on it, but it is more than good enough.
This next one is the one that concerns me most though:
That code has three problems....
This is how I would do it (and I would use
Then there is no need to indent, or so on, for the remaining code.
The remaining code looks relatively good. There's no error handling on the
One last thing, your first comments are duplicated:
If you're going to comment things, make them good.
Your general handling looks polished. I like the way that you handle whether this is an interactive terminal, but wonder whether it would simply be better to use the more standard check for
$PS1 or the i flag ( reference here ). If you are going to use tty to tell, you should probably rely on the exit status rather than the text message.The way you find the user directory is great, and is similar to what I normally do too. There are some variations on it, but it is more than good enough.
This next one is the one that concerns me most though:
if [[ -z $1 ]]; then
echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
else
.....That code has three problems....
- your script takes 2 parameters, but you only check for 1.
- if the parameter is missing, you still exit with a success exit code (0). You should exit with something else .. (like
1).
- there is no need for an else condition. Treat it like a guard-clause...
This is how I would do it (and I would use
$# and not $2 because having too many arguments is as bad as not having enough....if [[ $# -ne 2 ]]; then
echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
exit 1
fiThen there is no need to indent, or so on, for the remaining code.
The remaining code looks relatively good. There's no error handling on the
zip operation though.One last thing, your first comments are duplicated:
# This script zips a directory, excluding specified files, types and subdirectories.
# while zipping the directory it excludes hidden directories and certain file typesIf you're going to comment things, make them good.
Code Snippets
if [[ -z $1 ]]; then
echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
else
.....if [[ $# -ne 2 ]]; then
echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
exit 1
fi# This script zips a directory, excluding specified files, types and subdirectories.
# while zipping the directory it excludes hidden directories and certain file typesContext
StackExchange Code Review Q#93381, answer score: 2
Revisions (0)
No revisions yet.