简体   繁体   English

查找所有文件并将特定文件解压缩到本地文件夹

[英]Find all files and unzip specific file to local folder

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

This command finds all files named archive.zip and unzips file.txt to the folder that I execute the command from, is there a way to unzip the file to the same folder where the .zip file was found? 此命令查找所有名为archive.zip的文件,并将file.txt解压缩到执行该命令的文件夹中,是否可以将文件解压缩到找到.zip文件的同一文件夹中? I would like file.txt to be unzipped to folder1. 我希望将file.txt解压缩到folder1。

folder1\archive.zip
folder2\archive.zip

I realize $dirname is available in a script but I'm looking for a one line command if possible. 我意识到$ dirname在脚本中可用,但我正在寻找一个单行命令(如果可能)。

@iheartcpp - I successfully ran three alternatives using the same base command... @iheartcpp-我使用相同的基本命令成功运行了三种选择...

find . -iname "*.zip"

... which is used to provide the list of / to be passed as an argument to the next command. ...,用于提供/的列表作为参数传递给下一个命令。

Alternative 1: find with -exec + Shell Script (unzips.sh) 备选方案1:使用-exec + Shell脚本(unzips.sh)查找

File 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

Use this alternative like this: 使用这种替代方法:

find . -iname '*.zip' -exec ./unzips.sh {} \;

Alternative 2: find with | xargs 替代方法2:使用| xargs | xargs _ Shell Script (unzips) | xargs _ Shell脚本(解压缩)

Same unzips.sh file. 相同的unzips.sh文件。

Use this alternative like this: 使用这种替代方法:

find . -iname '*.zip' | xargs ./unzips.sh

Alternative 3: all commands in the same line (no .sh files) 替代方案3:同一行中的所有命令(没有.sh文件)

Use this alternative like this: 使用这种替代方法:

find . -iname '*.zip' | xargs sh -c 'for f in $@; do unzip -o -d `dirname $f` $f; done;'

Of course, there are other alternatives but hope that the above ones can help. 当然,还有其他替代方法,但希望上述替代方法可以有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM