find -name archive.zip -exec unzip {} file.txt \;

此命令查找名为archive.zip的所有文件,并将file.txt解压缩到我执行此命令的文件夹,是否有方法将文件解压缩到找到.zip文件的同一文件夹?我想把file.txt解压到folder1。
folder1\archive.zip
folder2\archive.zip

我意识到$dirname在脚本中是可用的,但如果可能的话,我正在寻找一个单行命令。

最佳答案

@iheartcpp-我用同一个基本命令成功地运行了三个选项。。。

find . -iname "*.zip"

... 它用于提供要作为参数传递给下一个命令的/的列表。
备选方案1:使用-exec+Shell脚本(unzips.sh)查找
文件unzips.sh:
#!/bin/sh
# This will unzip the zip files in the same directory as the zip are

for f in "$@" ; do
    unzip -o -d `dirname $f` $f
done

使用这种替代方法:
find . -iname '*.zip' -exec ./unzips.sh {} \;

备选方案2:使用Shell脚本查找(unzips)
相同的| xargs文件。
使用这种替代方法:
find . -iname '*.zip' | xargs ./unzips.sh

备选方案3:同一行中的所有命令(没有.sh文件)
使用这种替代方法:
find . -iname '*.zip' | xargs sh -c 'for f in $@; do unzip -o -d `dirname $f` $f; done;'

当然,也有其他的选择,但希望以上的选择能有所帮助。

08-06 04:36