
Question:
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.
Answer1:@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.
<strong>Alternative 1: find with -exec
+ Shell Script (unzips.sh)</strong>
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 {} \;
<strong>Alternative 2: find with | xargs
_ Shell Script (unzips)</strong>
Same unzips.sh
file.
Use this alternative like this:
find . -iname '*.zip' | xargs ./unzips.sh
<strong>Alternative 3: all commands in the same line (no .sh files)</strong>
Use this alternative like this:
find . -iname '*.zip' | xargs sh -c 'for f in $@; do unzip -o -d `dirname $f` $f; done;'
<hr />Of course, there are other alternatives but hope that the above ones can help.