81191

Find all files and unzip specific file to local folder

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.

Recommend

  • Reading filesystem and returning it
  • Customize jQuery On-Screen Keyboard
  • Escaping in wget bash command
  • MSBuild recursive copy
  • Gulp - compile sass by folder, and modify parent directories
  • [in .vbs][3in1] Create multiple folder 180 => Create subfolders in All 180 => Move file and re
  • Using for loop to move files from subdirectories to parent directories
  • Difficulties with installation script for hybris 5.6.02
  • What is the JSON format to specify multiple “sources” under AWS::CloudFormation::Init?
  • Windows Mercurial global keychain
  • What object should a Monitor wait on?
  • Adding listchoices to Pdfform with c# code
  • Generating ActionScript value objects from an xsd schema
  • python regexp for a few thousand words
  • Android app preferences are being cleared automatically
  • NLTK CoreNLPDependencyParser: Failed to establish connection
  • APC - Unable to load dynamic library
  • Create a unique primary key (hash) from database columns
  • JDBC 4's java.sql.Clob.free() method and backwards compatibility
  • Pattern combining type test and literal
  • Android LinearLayout and Images
  • link library to all targets in cmake project
  • How to move axis labels automatically in rgl R
  • Correctly Importing Apache Commons Math Package
  • Security issues with PHP's Readfile method
  • onBackPressed() not being executed
  • Paperclip, set path outside of rails root folder
  • Using $this when not in object context
  • Retrieving value from sql ExecuteScalar()
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Importing jscolor library in angular 2
  • Delete MySQLi record without showing the id in the URL
  • A cron job substitute?
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • Append folder name and increment by 1 using batch script
  • Programmatically clearing map cache
  • playing video using jmf