Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2004
    Posts
    48

    Unanswered: 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

  2. #2
    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.

  3. #3
    Join Date
    Jul 2004
    Posts
    48
    hi,
    Thanks for the reply.
    may be three level.

  4. #4
    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.

  5. #5
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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
    ~>
    Last edited by aigles; 06-09-07 at 19:22.
    Jean-Pierre.

  6. #6
    Join Date
    Jul 2004
    Posts
    48
    that was great!!!
    thanks thanks a loooot..

  7. #7
    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.

  8. #8
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •