LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-27-2013, 07:26 AM   #1
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Rep: Reputation: Disabled
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!
 
Old 09-28-2013, 12:49 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,808

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
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
 
Old 09-28-2013, 03:04 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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/*\/}"
 
Old 09-30-2013, 08:01 AM   #4
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Original Poster
Rep: Reputation: Disabled
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!
 
Old 09-30-2013, 08:12 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 10-01-2013, 06:10 AM   #6
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Original Poster
Rep: Reputation: Disabled
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?

Last edited by nouse; 10-01-2013 at 06:13 AM.
 
Old 10-01-2013, 07:18 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Does "unzip -X" work in Info-ZIP UnZip 6.00? ruario Linux - Software 5 09-15-2013 05:21 AM
What is the difference between using "jar xvf" and "unzip" to unzip the zip-file? thomas2004ch Linux - Newbie 4 08-27-2009 06:11 AM
FC4 Unzip Problems TC10284 Fedora 1 06-16-2006 10:20 AM
about unzip to unzip some chinese files' trouble arcow Linux - Software 0 03-24-2006 03:25 AM
problems with unzip jcai Linux - Newbie 2 09-15-2004 06:27 PM


All times are GMT -5. The time now is 04:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration