如何从 Unix 命令行递归解压目录及其子目录中的档案?
unzip
命令没有递归解压缩档案的选项。
如果我有以下目录结构和档案:
/Mother/Loving.zip /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes.zip
我想将所有档案解压到与每个档案同名的目录中:
/Mother/Loving/1.txt /Mother/Loving.zip /Scurvy/Sea Dogs/2.txt /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes/3.txt /Scurvy/Cures/Limes.zip
我会发出什么命令?
重要的是,这不会因文件名中含有空格而阻塞。
The unzip
command doesn't have an option for recursively unzipping archives.
If I have the following directory structure and archives:
/Mother/Loving.zip /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes.zip
And I want to unzip all of the archives into directories with the same name as each archive:
/Mother/Loving/1.txt /Mother/Loving.zip /Scurvy/Sea Dogs/2.txt /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes/3.txt /Scurvy/Cures/Limes.zip
What command or commands would I issue?
It's important that this doesn't choke on filenames that have spaces in them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(12)
另一个有趣的解决方案是:
DESTINY=[Give the output that you intend]
# Don't forget to change from .ZIP to .zip.
# In my case the files were in .ZIP.
# The echo were for debug purpose.
find . -name "*.ZIP" | while read filename; do
ADDRESS=$filename
#echo "Address: $ADDRESS"
BASENAME=`basename $filename .ZIP`
#echo "Basename: $BASENAME"
unzip -d "$DESTINY$BASENAME" "$ADDRESS";
done;
我意识到这已经很老了,但当我在寻找类似问题的解决方案时,它是 Google 上的第一批点击之一,所以我将在这里发布我所做的事情。 我的场景略有不同,因为我基本上只是想完全爆炸一个 jar 以及其中包含的所有 jar,因此我编写了以下 bash 函数:
function explode {
local target="$1"
echo "Exploding $target."
if [ -f "$target" ] ; then
explodeFile "$target"
elif [ -d "$target" ] ; then
while [ "$(find "$target" -type f -regextype posix-egrep -iregex ".*\.(zip|jar|ear|war|sar)")" != "" ] ; do
find "$target" -type f -regextype posix-egrep -iregex ".*\.(zip|jar|ear|war|sar)" -exec bash -c 'source "<file-where-this-function-is-stored>" ; explode "{}"' \;
done
else
echo "Could not find $target."
fi
}
function explodeFile {
local target="$1"
echo "Exploding file $target."
mv "$target" "$target.tmp"
unzip -q "$target.tmp" -d "$target"
rm "$target.tmp"
}
注意
如果您将其存储在一个文件中,而该文件不能像我一样为非交互式 shell 读取,则需要使用该文件。 如果您将函数存储在非交互式 shell 上加载的文件中(例如,我相信是 .bashrc
),您可以删除整个 source
语句。 希望这会对某人有所帮助。
一个小警告 - explodeFile
还会删除压缩文件,您当然可以通过注释掉最后一行来更改它。
像gunzip 使用 -r 标志之类的东西?......
递归地遍历目录结构。 如果命令行上指定的任何文件名是目录, gzip 将下降到该目录并压缩它在其中找到的所有文件(或者在gunzip的情况下解压缩它们)。
如果您想将文件提取到相应的文件夹,您可以尝试这个适用于
find . -name "*.zip" | while read filename; do unzip -o -d "`dirname "$filename"`" "$filename"; done;
可以处理高 I/O 的系统的多处理版本:
find . -name "*.zip" | xargs -P 5 -I fileName sh -c 'unzip -o -d "$(dirname "fileName")/$(basename -s .zip "fileName")" "fileName"'
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这对我有用
this works for me