在 Bash 脚本中压缩并忽略
Zip with ignore within Bash script
找到一个 wonderful little bash script that I've adapted 用于 zip 压缩 managed 目录,忽略 bower_components
、.git
和 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 [=11=]` && pwd)
if [[ -z ]]; then
echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
else
DIRECTORY_TO_COMPRESS=
ZIPPED_FILE=".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
唯一的问题是我想忽略的目录仍在创建中,只是空的。
有什么建议吗?
zip 手册页说
Note that currently the trailing / is needed for directories (as in
zip -r foo . -i dir/
to include directory dir).
因此,排除似乎同样有效:将脚本中标记的行替换为
IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/")
应该可以解决问题。
(我的 zip 是 Ubuntu-Linux 上的 3.0 版)
找到一个 wonderful little bash script that I've adapted 用于 zip 压缩 managed 目录,忽略 bower_components
、.git
和 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 [=11=]` && pwd)
if [[ -z ]]; then
echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
else
DIRECTORY_TO_COMPRESS=
ZIPPED_FILE=".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
唯一的问题是我想忽略的目录仍在创建中,只是空的。
有什么建议吗?
zip 手册页说
Note that currently the trailing / is needed for directories (as in
zip -r foo . -i dir/
to include directory dir).
因此,排除似乎同样有效:将脚本中标记的行替换为
IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/")
应该可以解决问题。
(我的 zip 是 Ubuntu-Linux 上的 3.0 版)