Search code examples
linuxbashunzip

Bulk Unziping Into Folders Path


I have Linux based server with domains:

domain1.com
   public_html
      archive1.zip
domain2.com
   public_html
      archive2.zip
domain3.com
   public_html
      archive3.zip

I'm trying to run unziping in the following way:

unzip */public_html/*.zip

It works perfectly for one domain (except the fact that it unzips in folder where I launch this command).

Is there any way for bulk unziping to get the following output:

domain1.com
   public_html
      archive1.zip
      content_of_archive1
domain2.com
   public_html
      archive2.zip
      content_of_archive2
domain3.com
   public_html
      archive3.zip
      content_of_archive3

Thank you for your time and help!


Solution

  • This calls for a for loop:

    for archive in */public_html/*.zip; do
       unzip "$archive" -d "$( dirname "$archive" )"
    done
    

    PS: I was going to use pushd to move to the directory, but @Mihir provided the simpler solution with -d in their comment.

    PPS: You don't need a script to run this - just type it verbatim at the bash prompt, newlines included. Or you can fold it into a one-liner:

    for archive in */public_html/*.zip; do unzip "$archive" -d "$( dirname "$archive" )"; done
    

    Either, the bash command history will let you come back to what you typed and modifier and/or rerun it as needed.