Find all files and unzip specific file to local folder

我们两清 提交于 2019-12-12 03:34:10

问题


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? I would like file.txt to be unzipped to 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.


回答1:


@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.

Alternative 1: find with -exec + Shell Script (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

Use this alternative like this:

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

Alternative 2: find with | xargs _ Shell Script (unzips)

Same unzips.sh file.

Use this alternative like this:

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

Alternative 3: all commands in the same line (no .sh files)

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.



来源:https://stackoverflow.com/questions/46222207/find-all-files-and-unzip-specific-file-to-local-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!