查找所有文件并将特定文件解压缩到本地文件夹 [英] Find all files and unzip specific file to local folder
问题描述
find -name archive.zip -exec unzip {} file.txt \;
此命令查找所有名为archive.zip的文件,并将file.txt解压缩到执行该命令的文件夹中,是否可以将文件解压缩到找到.zip文件的相同文件夹中?我希望将file.txt解压缩到folder1.
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? I would like file.txt to be unzipped to folder1.
folder1\archive.zip
folder2\archive.zip
我意识到$ dirname在脚本中可用,但是我正在寻找一个单行命令.
I realize $dirname is available in a script but I'm looking for a one line command if possible.
推荐答案
@iheartcpp-我使用相同的基本命令成功运行了三种选择...
@iheartcpp - I successfully ran three alternatives using the same base command...
find . -iname "*.zip"
...,用于提供/的列表作为参数传递给下一个命令.
... which is used to provide the list of / to be passed as an argument to the next command.
替代方法1:使用-exec
+ Shell脚本(unzips.sh)查找
Alternative 1: find with -exec
+ Shell Script (unzips.sh)
文件unzips.sh:
File 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:使用| xargs
_ Shell脚本(解压缩)查找
Alternative 2: find with | xargs
_ Shell Script (unzips)
相同的unzips.sh
文件.
使用这种替代方法:
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;'
当然,还有其他替代方法,但希望上述替代方法可以有所帮助.
Of course, there are other alternatives but hope that the above ones can help.
这篇关于查找所有文件并将特定文件解压缩到本地文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!