LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problems with mass-unzip (https://www.linuxquestions.org/questions/linux-newbie-8/problems-with-mass-unzip-4175478740/)

nouse 09-27-2013 06:26 AM

Problems with mass-unzip
 
Hi there again,

I do have MANY folders called
1,2,3,....,i

In all of them is a file called "Output.zip" (same name, different content)
I want to do a simple

unzip */output.zip (from an upper hierarchy level)

What happens is:

caution: filename not matched: 9/output.zip

and many more cautions.

I have no idea, why that is.

Any idea?

-------


The next step would include the printout of a single line in one of the files located in the zip folder:



What i do is the following:

cd 1
unzip *
grep -c "^>" file1.clstr
<write down the result> [a number]
cd ..
cd 2
unzip *
grep -c "^>" file1.clstr*
cd ..

and repeat.

* All output.zips contain the same file structure and names.


Now the question is.....can i automate this with a For - loop? with an textfile as an output:

Go to dir i, unzip all, give last line of file1, print that as "dir i: result" into a text file.


I guess this would demand quite the script, but i am a beginner, and it makes me sad that i am probably spending a lot of time on unnecessary things.

So, many thanks again!

AlucardZero 09-27-2013 11:49 PM

Code:

NAME
      unzip - list, test and extract compressed files in a ZIP archive

SYNOPSIS
      unzip  [-Z] [-cflptTuvz[abjnoqsCDKLMUVWX$/:^]] file[.zip] [file(s) ...]  [-x xfile(s) ...]
      [-d exdir]

What you should take from the man page is that "unzip" does not unzip multiple zips in one command. It does one, and any other unswitched arguments are the files to extract out of the zip.

Here's a loop to get you started:
Code:

for i in $(ls); do
  cd $i;
  unzip Output.zip;
  cd ..;
done


Firerat 09-28-2013 02:04 AM

Code:

for i in "*/*.zip";do
  pushd "${i%/*}"
      unzip "${i/*\/}"
  popd
done

http://mywiki.wooledge.org/BashGuide/Arrays
for some reasons why $(ls) is a bad idea.

http://www.tldp.org/LDP/abs/html/refcards.html#AEN22664
for the "${i%/*}" and "${i/*\/}"

nouse 09-30-2013 07:01 AM

Hi there,
thanks for your help.
For feedback:
I first tried Firerat`s script, this was the output

sh massunzip.sh
massunzip.sh: 3: massunzip.sh: pushd: not found
massunzip.sh: 4: massunzip.sh: Bad substitution

Alucard`s script however works fine. Thank you!

Firerat 09-30-2013 07:12 AM

no pushd?

what shell are you using?

ahh, I see you are using sh

use bash


although Alucard`s script works, it is poor and will fail at some point
for instance, when you have a space in the dirname

Code:

#!/bin/bash
for i in "*/*.zip";do
  pushd "${i%/*}"
      unzip "${i/*\/}"
  popd
done

chmod 700 massunzip.sh
./massunzip.sh

nouse 10-01-2013 05:10 AM

Hi, thanks again for putting so much effort in my case. I first have to understand the difference between bash and sh ;), then ill try out your sophisticated version.
Based on Firerat's script, i did the following:

Code:

#!/bin/bash
for i in $(ls); do
  cd $i;
  basename $i;
  grep -c "^>" output.1.clstr;
  cd ..;
done

It works out the way i wanted, but i do want to export the complete output into a textfile. Is that possible?

Firerat 10-01-2013 06:18 AM

hmm, not based on my script

has the $(ls) which is just pointless , and 'dangerous' in a script as it will wordsplit whitespace

Anyway,,, let us ignore that ...




Code:

#!/bin/bash
LogFile=${PWD}/MyLog
InterestedIn="output.1.clstr"
for i in "*/*.zip";do
  pushd "${i%/*}"
      unzip "${i/*\/}"
      LineCount=$(grep -c "^>" "$InterestedIn")
      LastLine="$(tail -n1 "$InterestedIn")"
      printf "%s\n" "Dir ${i/*\/} No. of Lines : $LineCount : last line : $LastLine" >> "${LogFile}"
  popd
done


had trouble figuring out exactly what you want
and your filesnames changed ( output / file1 )

Oh, above is untested.


and personally I would be tempted to only extract the interesting file to a temp dir , work with it and then remove.
but I don’t really know what you want out of the whole process


All times are GMT -5. The time now is 09:00 AM.