You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

zip命令--exclude参数仅排除文件夹内文件无法排除文件夹本身的问题求助

zip命令--exclude参数仅排除文件夹内文件无法排除文件夹本身的问题求助

我最近在bash脚本里用zip命令打包文件,遇到了个头疼的问题:明明能排除scripts/文件夹里的所有文件,但这个文件夹本身却始终留在最终的zip包里。试了各种写法都没用,已经卡好几天了,求大伙帮忙看看问题出在哪!

先说说我的脚本情况:
我用字符串拼接zip命令,最后通过eval执行。奇怪的是,.vscode、.git这些文件夹用同样的语法就能连文件夹带内容一起排除,唯独scripts文件夹死活不行。我的核心命令片段是这样的:

zip_cmd="zip -r '${output_zip}' '${parent_folder}'/* \
--exclude '${parent_folder}'/.vscode/* \
--exclude '${parent_folder}'/.git/* \
--exclude '${parent_folder}'/temp/* \
--exclude '${parent_folder}'/scripts/* \
--exclude '${parent_folder}'/*.template.* \
--exclude '${parent_folder}'/$(basename "$0") \
"

# 额外处理__pycache__和特定可执行文件夹
mapfile -t exclude_pycache < <(find "${parent_folder}" -type d -name "__pycache__")
mapfile -t exclude_executables < <(find "${parent_folder}/voxconvert-executable" -mindepth 1 -maxdepth 1 -type d -not -name "${voxconvert_version}")
exclude_paths=("${exclude_executables[@]}" "${exclude_pycache[@]}")

for path in "${exclude_paths[@]}"; do
zip_cmd+=" --exclude \"$path/*\""
done

eval "$zip_cmd"
echo "Created zip file: $(pwd)/${output_zip}"

为了排除scripts文件夹本身,我试过这些写法:

  • --exclude '${parent_folder}'/scripts/"
  • --exclude '${parent_folder}'/scripts"
  • --exclude '${parent_folder}'/scripts*"
  • --exclude '${parent_folder}'/scripts\*"
  • --exclude '${parent_folder}'/scripts/\*"
  • --exclude '${parent_folder}'/scripts/*"

但不管怎么调整,zip包里还是会留一个空的scripts文件夹。为啥别的文件夹都正常,就它特殊?

另外补充下,我之前试过用数组构建命令(比如用7z的时候),但动态生成的排除路径总是失效,所以才改用字符串加eval的方式,这会不会和问题有关?当时的7z脚本大概是这样:

seven_zip_cmd=("7z" "a" "-r" "${output}" "${parent_folder}"/*)

exclude_patterns=(
"${parent_folder}/.vscode/*"
"${parent_folder}/.git/*"
"${parent_folder}/temp/*"
"${parent_folder}/*.sh"
"${parent_folder}/$(basename "$0")"
)

mapfile -t exclude_pycache < <(find "${parent_folder}" -type d -name "__pycache__")
mapfile -t exclude_executables < <(find "${parent_folder}/voxconvert-executable" -mindepth 1 -maxdepth 1 -type d -not -name "${voxconvert_version}")
exclude_paths=("${exclude_executables[@]}" "${exclude_pycache[@]}")

for path in "${exclude_paths[@]}"; do
path=$(realpath "$path" --relative-to="${parent_folder}")
exclude_patterns+=("${path}/*")
done

for pattern in "${exclude_patterns[@]}"; do
seven_zip_cmd+=("-x!${pattern}")
done

"${seven_zip_cmd[@]}"
echo "Created 7z file: $(pwd)/${output}"

备注:内容来源于stack exchange,提问作者Macky's UserStalker Bot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 07:23:13