LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2007, 06:34 PM   #1
eldaria
LQ Newbie
 
Registered: Feb 2004
Posts: 22

Rep: Reputation: 15
Question Script to unzip and gzip files into directory,


Hello all.
I have a whole bunch of zip files that I would like to convert into individual gzip files.

Basically I want to unzip the zip file into a directory named the same as the zip file, but without the .zip
Then I want to individually gzip each file inside.
I need to have it in a sub directory since files inside the zip file can be named the same as in other zip files.
It is a bit difficult to explain so I will try to illustrate

Documents(Eng).zip
Document1.rtf
Document2.rtf
Documents(Ger).zip
Document1.rtf
Document2.rtf
Documents(Fre).zip
Document1.rtf
Document2.rtf


The part I'm having trouble with is the first step of unzipping into a folder named the same as the zip file.
I painfully did one of my directories with Zip files, one by one.
and then to gzip the files was easy,

In folder with all subfolders.
Code:
find -iname '*.rtf' -execdir gzip -9 {} \;
However I have 34 Directories with each containing some 600 zip files.
so as you can guess it is a pain to do it manually. :-)

I tried to make my script grab the file name and then use sed to strip the .zip, but I can't even get that to work.

To prevent any actual change I used just an echo to generate the output, but it get's messed up.
It correctly removes the .zip, but it cuts the filename on each space and special characters.

Code:
#!/bin/bash

for file in $( find -iname '*.zip')
do
        output=`echo $file | sed 's/.zip//g'`
        echo $output
done

exit 0
Any ideas why or maybe a better way of doing it?
 
Old 03-15-2007, 07:25 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by eldaria
Hello all.
I have a whole bunch of zip files that I would like to convert into individual gzip files.

Basically I want to unzip the zip file into a directory named the same as the zip file, but without the .zip
Then I want to individually gzip each file inside.
I need to have it in a sub directory since files inside the zip file can be named the same as in other zip files.
It is a bit difficult to explain so I will try to illustrate

Documents(Eng).zip
Document1.rtf
Document2.rtf
Documents(Ger).zip
Document1.rtf
Document2.rtf
Documents(Fre).zip
Document1.rtf
Document2.rtf


The part I'm having trouble with is the first step of unzipping into a folder named the same as the zip file.
I painfully did one of my directories with Zip files, one by one.
and then to gzip the files was easy,

In folder with all subfolders.
Code:
find -iname '*.rtf' -execdir gzip -9 {} \;
However I have 34 Directories with each containing some 600 zip files.
so as you can guess it is a pain to do it manually. :-)

I tried to make my script grab the file name and then use sed to strip the .zip, but I can't even get that to work.

To prevent any actual change I used just an echo to generate the output, but it get's messed up.
It correctly removes the .zip, but it cuts the filename on each space and special characters.

Code:
#!/bin/bash

for file in $( find -iname '*.zip')
do
        output=`echo $file | sed 's/.zip//g'`
        echo $output
done

exit 0
Any ideas why or maybe a better way of doing it?
Untested:
Code:
find -type f -iname '*.zip' |
 while IFS= read -r file
 do
   dir=${file%.zip}
   if mkdir "$dir"
   then
     cd "$dir" || { echo "Could not cd to $dir"; continue; }
     unzip "$file"
     gzip * ## If there are subdirs: find . -type f -exec gzip * \;
   fi
 done
 
Old 03-16-2007, 04:56 AM   #3
eldaria
LQ Newbie
 
Registered: Feb 2004
Posts: 22

Original Poster
Rep: Reputation: 15
Thank you very much with some modifications it worked,

It failed on 2 points.
It creates all the subfolders within eachother.
Code:
./Documents(Eng)/Documents(Ger)/Documents(Fre)
I solved that by adding a cd .. just after the line of gzip

Unzip also failed with error that it can't find the file.
I first thought it was related to spaces in the name, but later found that it looks in current directory for the file.
So added the full path to the file, and now it works.

Code:
#!/bin/bash

find -type f -iname '*.zip' |
 while IFS= read -r file
 do
   dir=${file%.zip}
   if mkdir "$dir"
   then
     curdir=`pwd`
     cd "$dir" || { echo "Could not cd to $dir"; continue; }
     unzip "$curdir/$file"
     gzip -v9 *
     cd ..
   fi
 done
exit 0
For the learning experience could you explain a bit?

I don't understand what happens with these bits:
Code:
while IFS= read -r file
I know the 'while' command from Visual basic, but not the 'IFS' and I have no man page for the 'read' or the 'while' command, so can't see the syntax.

Code:
dir=${file%.zip}
I understand it strips of the '.zip' but how, I was fighting with sed, but this seems easier.

Code:
|| { echo "Could not cd to $dir"; continue; }
Not sure how this part works, is it if cd fails, it will break the loop?

Hope you have patience to explain. ;-)
 
Old 03-16-2007, 10:59 AM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by eldaria
Thank you very much with some modifications it worked,

It failed on 2 points.
It creates all the subfolders within eachother.
I should have executed cd in a subshell:
Code:
    (
     cd "$dir" || { echo "Could not cd to $dir"; continue; }
     unzip "$file"
     gzip * ## If there are subdirs: find . -type f -exec gzip * \;
    )
Quote:
Code:
./Documents(Eng)/Documents(Ger)/Documents(Fre)
I solved that by adding a cd .. just after the line of gzip

Unzip also failed with error that it can't find the file.
I first thought it was related to spaces in the name, but later found that it looks in current directory for the file.
So added the full path to the file, and now it works.

Code:
#!/bin/bash

find -type f -iname '*.zip' |
 while IFS= read -r file
 do
   dir=${file%.zip}
   if mkdir "$dir"
   then
     curdir=`pwd`
You don't need pwd; the current directory is stored in $PWD.

Quote:
Code:
     cd "$dir" || { echo "Could not cd to $dir"; continue; }
     unzip "$curdir/$file"
     gzip -v9 *
     cd ..
   fi
 done
exit 0
For the learning experience could you explain a bit?

I don't understand what happens with these bits:
Code:
while IFS= read -r file
I know the 'while' command from Visual basic, but not the 'IFS' and I have no man page for the 'read' or the 'while' command, so can't see the syntax.

Read and while are built into the shell; read the shell's man page.
Quote:
Code:
dir=${file%.zip}
I understand it strips of the '.zip' but how,

Read the "Parameter Expansion" section of the shell's man page.
Quote:
I was fighting with sed, but this seems easier.

Code:
|| { echo "Could not cd to $dir"; continue; }
Not sure how this part works, is it if cd fails, it will break the loop?

Hope you have patience to explain. ;-)
 
  


Reply



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
unzip all .zip files in a directory tes15 Linux - Newbie 14 08-29-2014 11:46 AM
Script tar+gzip traverse a directory gn00kie Programming 7 06-16-2006 01:52 AM
script to gzip all directories in a directory br8kwall Programming 11 09-06-2005 10:42 PM
How to unzip and untar a xxx.tar.gzip.part file> linuxharsha Linux - Software 8 02-15-2004 04:27 AM
How to untar and unzip .tar.gzip.part file? linuxharsha Linux - Software 2 02-14-2004 10:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:02 AM.

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
Open Source Consulting | Domain Registration