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

我会发出什么命令或命令?

重要的是,这不会阻塞其中包含空格的文件名。

有帮助吗?

解决方案

如果要将文件解压缩到相应的文件夹,可以试试这个

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"'

其他提示

这是一个涉及find命令和while循环的解决方案:

find . -name "*.zip" | while read filename; do unzip -o -d "`basename -s .zip "$filename"`" "$filename"; done;

正确处理所有文件名(包括换行符)并提取到与文件位于同一位置的目录的解决方案,只需删除扩展名:

find . -name '*.zip' -exec sh -c 'unzip -o -d "${0%.*}" "$0"' '{}' ';'

请注意,您可以通过使用.jar添加文件类型来轻松处理更多文件类型(例如-o),例如:

find . '(' -name '*.zip' -o -name '*.jar' ')' -exec ...

您可以在单个命令行中使用find和-exec标志来执行作业

find . -name "*.zip" -exec unzip {} \;

使用-r标志的gunzip之类的东西?....

递归地传播目录结构。如果在命令行中指定的任何文件名是目录,gzip将下降到目录并压缩它在那里找到的所有文件(或者在gunzip的情况下解压缩它们)。

http://www.computerhope.com/unix/gzip.htm

这完全符合我们的要求:

解压缩文件:

find . -name "*.zip" | xargs -P 5 -I FILENAME sh -c 'unzip -o -d "$(dirname "FILENAME")" "FILENAME"'

上面的命令不会创建重复的目录。

删除所有zip文件:

find . -depth -name '*.zip' -exec rm {} \;

如果您使用的是cygwin,则basename命令的语法略有不同。

find . -name "*.zip" | while read filename; do unzip -o -d "`basename "$filename" .zip`" "$filename"; done;

我意识到这已经很老了,但是当我在寻找类似的解决方案时,它是谷歌的第一次点击,所以我会发布我在这里做的。我的情况略有不同,因为我基本上只想完全爆炸一个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文件中,则需要<file-where-this-function-is-stored>。如果您将函数存储在非交互式shell上加载的文件中(例如,.bashrc我相信),您可以删除整个source语句。希望这会对某人有所帮助。

一点警告 - explodeFile也会删除ziped文件,你当然可以通过注释掉最后一行来改变它。

另一个有趣的解决方案是:

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top