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

iheartcpp
  • 371
  • 1
  • 5
  • 14

1 Answers1

0

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

Almir Campos
  • 2,833
  • 1
  • 30
  • 26