2

For example my folder name is Test.zip

Test.zip includes various zip folders like te1.zip, te2.zip , te3.zip even te1.zip include various zip folders.

So i need to unzip in linux at once.

Could you please let me know how to do this.

Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38
user439643
  • 41
  • 1
  • 3

2 Answers2

0

Short script something like this should do the job (please note, that you need to have a backup of your original file, as the script needs to remove it in order to process everything recursively).

while [ "$(find . -type f -name '*.zip' | wc -l)" -gt 0 ]; 
  do 
    find -type f -name '*.zip' -exec mkdir '{}'_dir \; \ 
                               -exec unzip -d '{}'_dir -- '{}' \; \
                               -exec rm -- '{}' \;
  done

On some systems it might be possible to use -delete instead of -exec rm -- '{}' \;.

  • The command will go through your current directory (expectation that this is an empty directory with the only Test.zip (or whatever zip file you have) available in it).
  • For every found archive it will create a new directory with archive name + _dir (to avoid collision if some archives contain files with matching names).
  • It will unzip the archive to newly created directory and remove processed zip file
  • Once all files are processed, it will iterate one more time and check whether zip files are still available in directory tree
  • Note, that in case there will be a zip bomb somewhere inside, the script will fall to endless loop.
rush
  • 27,055
  • 7
  • 87
  • 112
0

This is a great candidate for a recursive script.

Save this as a script (e.g. my_unzip.sh) Will not work if you paste to command line as-is:

#!/usr/bin/env bash

: ${1:?Please supply a file} # ${1} is the zip being fed to the script -- a little user-friendliness never went amiss
DIR="${1%.zip}" # this is where the current zip will be unzipped
mkdir -p "${DIR}" || exit 1 # might fail if it's a file already
unzip -n -d "${DIR}" "${1}" # unzip current zip to target directory
find "${DIR}" -type f -name '*.zip' -print0 | xargs -0 -n1 "${0}" # scan the target directory for more zips and recursively call this script (via ${0})

Make it executable

chmod +x my_unzip.sh

And run it with your zip file as a parameter:

my_unzip.sh /some/path/yourfile.zip

Preserves your original file, comments inline to explain what's happening.

Cowardly, in that it never tries to overwrite that already exists. This behaviour can be changed by updating unzip command to remove -n, to get prompted, or change it to -o to force overwriting.

bxm
  • 4,561
  • 1
  • 20
  • 21