如何读取文本文件内的压缩包路径并批量解压至指定目录
批量解压压缩包问题解决方案
原命令报错原因
你执行的
find directoryWherefindZip/ -iname '*.mzp' | unzip -d directoryWhereUnzip/无法运行,是因为unzip默认不接收管道传递的文件路径作为输入参数,需要明确指定要解压的压缩包路径。你可以选择添加路径读取逻辑后循环调用unzip,也可以用xargs传递参数实现需求,两种方式都可行。
实现方案
查看已存储的压缩包路径列表
如果你已经把所有待处理压缩包路径存入了文本文件(假设文件名为zip_paths.txt),直接执行以下命令即可查看所有路径:cat zip_paths.txt
批量解压方法
两种方案可选,都支持处理带空格、特殊字符的文件路径:
方案1:xargs调用(写法更简洁)
- 从路径文本文件读取路径批量解压:
xargs -a zip_paths.txt -I {} unzip {} -d directoryWhereUnzip/ - 直接通过find查找并解压,无需提前存路径文件:
find directoryWherefindZip/ -iname '*.mzp' -print0 | xargs -0 -I {} unzip {} -d directoryWhereUnzip/
方案2:循环调用(灵活度更高,方便加自定义判断逻辑)
- 从路径文本文件读取路径批量解压:
while IFS= read -r zip_path; do unzip "$zip_path" -d directoryWhereUnzip/ done < zip_paths.txt
- 直接通过find查找并解压:
find directoryWherefindZip/ -iname '*.mzp' -print0 | while IFS= read -r -d '' zip_path; do unzip "$zip_path" -d directoryWhereUnzip/ done
可选参数说明
如果解压过程中存在重名文件,可以给unzip追加对应参数调整处理逻辑:
- 加
-o参数:自动覆盖重名文件,无需交互确认 - 加
-n参数:自动跳过重名文件,不覆盖已有文件
内容的提问来源于stack exchange,提问作者Djozeppe
相关产品推荐
相关产品推荐

