If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > how to unzip files recursively

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-08-07, 02:59
jagans jagans is offline
Registered User
 
Join Date: Jul 2004
Posts: 48
how to unzip files recursively

Hi,

I have a zip file which contains thousands of zips and I want to extract all those into its own directories. Is there any linux command to done that?
I am using the linux unzip command.

Thanks in advance
Jagan
Reply With Quote
  #2 (permalink)  
Old 06-08-07, 08:56
ISPserver ISPserver is offline
Registered User
 
Join Date: Jun 2007
Posts: 9
How many recursion archive in archive?

Or in one.zip many zip files. And in zip files simple file.
Reply With Quote
  #3 (permalink)  
Old 06-08-07, 10:42
jagans jagans is offline
Registered User
 
Join Date: Jul 2004
Posts: 48
hi,
Thanks for the reply.
may be three level.
Reply With Quote
  #4 (permalink)  
Old 06-08-07, 10:44
jagans jagans is offline
Registered User
 
Join Date: Jul 2004
Posts: 48
in both ways its coming.
In one zip there are multiple zips. After extracting the main zip, it will have some directories and other zips and inside the directory thre are many zips.
Reply With Quote
  #5 (permalink)  
Old 06-09-07, 18:15
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Try and adapt the following script (runzip.sh) :
Code:
#
# Function  : runzip zip_file rm_flag
# Parameters: zip_file : File to unzip
#             rm_flag  = If set, remove zip file after unzip
#

runzip()
{
   #
   # Get parameters
   #

   local zip_file=$1
   local rm_flag=$2

   if [[ "${zip_file}" != *.zip ]]
   then
      zip_file=${zip_file}.zip
   fi

   #
   # Declare local variables
   #

   local zip_dir
   local zip_list
   local new_zip_file
   local new_unzip_sts

   #
   # Verify thar zip file exists
   #

   if [[ ! -e ${zip_file} ]]
   then
      echo "$0 - Zip file not found : ${zip_file}" >&2
      return 1
   fi

   #
   # Create unzip directory
   #

   zip_dir=$PWD/$(basename ${zip_file} .zip)
   if [ ! -d ${zip_dir} ]
   then
      if ! mkdir ${zip_dir}
      then
         echo "$0 - Failed to create directory : ${zip_dir}"
         return 1
      fi
   fi

   #
   # Unzip in unzip directory
   #

   if ! unzip -o ${zip_file} -d ${zip_dir}
   then
      echo "$0 - Unzip error for file : $PWD/${zip_file}"
      return 1
   fi

   #
   # Get list of new zip files
   #

   new_zip_list=${zip_dir}/.new_zip_list
   find ${zip_dir} -type f -name '*.zip' -print > ${new_zip_list}

   #
   # Recursive unzip of new zip files
   #

   unzip_sts=0
   cd ${zip_dir}
   while read new_zip_file
   do
      if ! runzip ${new_zip_file} remove_zip
      then
         unzip_sts=$?
         break
      fi
   done < ${new_zip_list}
   rm -f ${new_zip_list}
   cd ..

   #
   # Remove zip file if required
   #

   if [ -n "${rm_flag}" -a ${unzip_sts} -eq 0 ]
   then
      if ! rm ${zip_file}
      then
         echo "$0 - Failed to delete file : $PWD/${zip_file}"
      fi
   fi

   return 0
}

runzip $1
The zip file is unzipped in it's own directory in the cuurent directory.

Code:
$ ls dir0*
dir0.zip  
$ runzip.sh dir0
Archive:  dir0.zip
 extracting: /home/dir0/dir01.zip
 extracting: /home/dir0/dir02.zip
 extracting: /home/dir0/file0a
  inflating: /home/dir0/file0b
Archive:  /home/dir0/dir01.zip
 extracting: /home/dir0/dir01/dir011.zip
 extracting: /home/dir0/dir01/dir012.zip
 extracting: /home/dir0/dir01/file01a
 extracting: /home/dir0/dir01/file01b
 extracting: /home/dir0/dir01/file01c
Archive:  /home/dir0/dir01/dir011.zip
 extracting: /home/dir0/dir01/dir011/file011a
 extracting: /home/dir0/dir01/dir011/file011b
Archive:  /home/dir0/dir01/dir012.zip
 extracting: /home/dir0/dir01/dir012/file012a
Archive:  /home/dir0/dir02.zip
 extracting: /home/dir0/dir02/dir021.zip
 extracting: /home/dir0/dir02/dir022.zip
 extracting: /home/dir0/dir02/file02a
 extracting: /home/dir0/dir02/file02b
Archive:  /home/dir0/dir02/dir021.zip
 extracting: /home/dir0/dir02/dir021/file021a
 extracting: /home/dir0/dir02/dir021/file021b
Archive:  /home/dir0/dir02/dir022.zip
 extracting: /home/dir0/dir02/dir022/file022a
$ ls -d dir0*
dir0/  dir0.zip  
$ ls -lR dir0
dir0:
total 2
drwxr-xr-x+ 4 Jean-Pierre Aucun  0 Jun 10 00:11 dir01/
drwxr-xr-x+ 4 Jean-Pierre Aucun  0 Jun 10 00:11 dir02/
-rw-r--r--  1 Jean-Pierre Aucun 21 Jun  9 22:15 file0a
-rw-r--r--  1 Jean-Pierre Aucun 28 Jun  9 22:16 file0b

dir0/dir01:
total 0
drwxr-xr-x+ 2 Jean-Pierre Aucun 0 Jun 10 00:11 dir011/
drwxr-xr-x+ 2 Jean-Pierre Aucun 0 Jun 10 00:11 dir012/
-rw-r--r--  1 Jean-Pierre Aucun 0 Jun  9 22:47 file01a
-rw-r--r--  1 Jean-Pierre Aucun 0 Jun  9 22:47 file01b
-rw-r--r--  1 Jean-Pierre Aucun 0 Jun  9 22:47 file01c

dir0/dir01/dir011:
total 0
-rw-r--r-- 1 Jean-Pierre Aucun 0 Jun  9 22:47 file011a
-rw-r--r-- 1 Jean-Pierre Aucun 0 Jun  9 22:47 file011b

dir0/dir01/dir012:
total 0
-rw-r--r-- 1 Jean-Pierre Aucun 0 Jun  9 22:47 file012a

dir0/dir02:
total 0
drwxr-xr-x+ 2 Jean-Pierre Aucun 0 Jun 10 00:11 dir021/
drwxr-xr-x+ 2 Jean-Pierre Aucun 0 Jun 10 00:11 dir022/
-rw-r--r--  1 Jean-Pierre Aucun 0 Jun  9 22:47 file02a
-rw-r--r--  1 Jean-Pierre Aucun 0 Jun  9 22:47 file02b

dir0/dir02/dir021:
total 0
-rw-r--r-- 1 Jean-Pierre Aucun 0 Jun  9 22:48 file021a
-rw-r--r-- 1 Jean-Pierre Aucun 0 Jun  9 22:48 file021b

dir0/dir02/dir022:
total 0
-rw-r--r-- 1 Jean-Pierre Aucun 0 Jun  9 22:48 file022a
~>
__________________
Jean-Pierre.

Last edited by aigles; 06-09-07 at 18:22.
Reply With Quote
  #6 (permalink)  
Old 06-12-07, 04:17
jagans jagans is offline
Registered User
 
Join Date: Jul 2004
Posts: 48
that was great!!!
thanks thanks a loooot..
Reply With Quote
  #7 (permalink)  
Old 12-11-11, 23:18
poorthrill poorthrill is offline
Registered User
 
Join Date: Dec 2011
Posts: 1
Word Splitting

How would one modify this script to allow for a zip file with space in the name? e.g.
/home/download/this is a zip file.zip

I tried double quoting and escaping the variable in the script but that doesn't seem to work.
Reply With Quote
  #8 (permalink)  
Old 12-13-11, 14:14
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
I looked at it, but it seemed like a lot of work to create a test environment.
Maybe you could add something like the following:
Code:
$cat shrink
#/bin/bash                            
read in                               
len=`expr length "$in"`               
out=""                                
i=1                                   
while [ $i -le $len ]                 
do                                    
        letter=`echo $in|cut -c$i-$i` 
        if [ "a$letter" = "a " ]      
        then                          
#       letter="_"                    
        letter=""                     
        fi                            
        out=$out$letter               
        i=`expr $i + 1`               
done                                  
echo $out
and insert a line
Code:
out=`echo $filename|shrink`
to get rid of the spaces, or replace them with underscores.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On