1

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!

Fedor
  • 39
  • 1
  • 8

1 Answers1

1

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
joanis
  • 10,635
  • 14
  • 30
  • 40
  • 2
    That will fail horribly if any of the names contain spaces. – melpomene Jul 21 '19 at 18:19
  • Btw, would also like to mention, didn’t know about `dirname` command. Had been using `${BASH_SOURCE%/*}` for long. That would simplify my work now. – Mihir Luthra Jul 21 '19 at 18:19
  • Thank you but how to launch this script, I have > meaning that statement is not closed or expecting parameters to add... – Fedor Jul 21 '19 at 18:21
  • @Marc, save the script with any name. Execute it like `bash `. Also, it is probably showing `>` cuz you may have used quotes somewhere while entering which are waiting to be closed. – Mihir Luthra Jul 21 '19 at 18:23
  • @Mihir thank you, was on this step when you wrote) – Fedor Jul 21 '19 at 18:24
  • bash unziper.sh unziper.sh: line 2: syntax error near unexpected token `$'do\r'' 'nziper.sh: line 2: `for archive in */public_html/*.zip; do – Fedor Jul 21 '19 at 18:26
  • Okay, my fault, new line caused it. – Fedor Jul 21 '19 at 18:28
  • @Marc thank you so much for your time and efforts, I just started this option and was eager to complete. Your solution is very good, I cannot thumb it up due to low reputation, unfortunately. I will use it next time for unziping. The only question is how to specify directory where to search for -name '*.zip' cause I need to look it for only in */public_html(only here)/*.zip? – Fedor Jul 21 '19 at 18:32
  • You don't need to put this in a script. It's simple enough you can just type it on the command line whenever you need it. – joanis Jul 21 '19 at 18:33
  • @joanis how to search in public_html only folder... – Fedor Jul 21 '19 at 18:34
  • @Marc not sure I understand what you're missing. My solution uses `*/public_html/*.zip` just like you did in your question, so it's only going to look where you want, no? – joanis Jul 21 '19 at 18:36
  • @melpomene thanks for pointing that out. @ Mihir proposed an edit to fix that, which I accepted, so it should now be fine with spaces in the file names. – joanis Jul 21 '19 at 18:44
  • @melpomene OK, sometimes it takes more tries than other times to get it right. Thanks for the complete space-in-name solution. – joanis Jul 21 '19 at 18:48