Unzip to a specific level


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unzip to a specific level
# 1  
Old 01-25-2016
Unzip to a specific level

Hello gurus, this should be simple but I cant seem to figure this out. Please assist.

I have several thousand zip files in a directory

Code:
A.ZIP
B.ZIP
.
.
..
Z.ZIP

All these zipped files have a single subdirectory at level1 and then other files and folders under the subdirectory.

Example

Code:
A.ZIP
------Subfolder12
------------FilesandSubSubFolders


If I unzip A.ZIP, the unzipped folder is called Subfolder12, which I want to be named as A

If I use

Code:
unzip A.zip

The result is

Code:
Subfolder12
--------FilesandSubfolders



If I do


Code:
unzip A.zip -d A

the result is

Code:
A
-----Subfolder12
-------------FilesandSubfolders


What I want is

Code:
A
------FilesandSubfolders

i`m trying to do this in a loop.

Code:
for file in *.zip
do
unzip $file -d ${file%.zip}
done

# 2  
Old 01-25-2016
Perhaps

Code:
for f in *.zip; do
    if [[ -e "$f" ]]; then
        unzip "$f" && mv -v "Subfolder12" "${f%.zip}"
    fi
done

Untested.
This User Gave Thanks to Aia For This Post:
# 3  
Old 01-25-2016
Quote:
Originally Posted by Aia
Perhaps

Code:
for f in *.zip; do
    if [[ -e "$f" ]]; then
        unzip "$f" && mv -v "Subfolder12" "${f%.zip}"
    fi
done

Untested.
The subfolder is not necessarily called subfolder12, its any random name at level1. So every zip will have a different name for the subfolder at level 1.
# 4  
Old 01-25-2016
In that case we have to find out what's that first directory's name. I am using unzip to tell me.
Code:
for f in *.zip; do
    if [[ -e "$f" ]]; then
        unzip "$f"
        firstdir=$(unzip -qql "$f" | head -1)
        mv -v "${firstdir##* }" "${f%.zip}"
    fi
done

If you have a tar utility that supports --strip-componets:

Code:
for f in *.zip; do
    if [[ -e "$f" ]]; then
        mkdir "${f%.zip}" && tar xvzf "$f" --strip-components=1 -C "${f%.zip}"
    fi
done

Again, all these are edited on the fly. Untested.
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unzip the .zip file without using unzip utility in UNIX

I have .zip file, i want to list all the files archived in the zip file. unzip utility is not working for me in unix. Please help me resolve this issue Thanks ganesh. (3 Replies)
Discussion started by: Ganesh L
3 Replies

2. Red Hat

SSL certificate generation on OS level or application level

We have a RHEL 5.8 server at the production level and we have a Java application on this server. I know of the SSL certificate generation at the OS (RHEL) level but it is implemented on the Java application by our development team using the Java keytool. My doubt is that is the SSL generation can... (3 Replies)
Discussion started by: RHCE
3 Replies

3. UNIX for Advanced & Expert Users

Unzip Help

Hello All, I am trying to write a shell script(linux) where it just unzips the abc.zip file in the same folder. If I write unzip abc.zip in my shell script and try to run the shell script I am getting Error : ?Invalid command But when I run the command unzip abc.zip at the command prompt... (5 Replies)
Discussion started by: su2
5 Replies

4. SCO

Unzip

Hi, I have installed Unzip 5.3 on SCO Unix 5.05 Openserver and it successfully installs but when I go to unzip a file by using unzip <filename> I get ksh: unzip: not found . Any ideas?:o (1 Reply)
Discussion started by: twitchmaker
1 Replies

5. Solaris

Difference between run level & init level

what are the major Difference Between run level & init level (2 Replies)
Discussion started by: rajaramrnb
2 Replies

6. Shell Programming and Scripting

How to Unzip a file using unzip utility for files zipped without zip utility ?

Hi, I need to zip/compress a data file and send to a vendor. The vendor does have only unzip utility and can accept only .ZIP files. I do not have zip utility in my server. How do I zip/compress the file so that it can be deflated using unzip command ? I tried gzip & compress commands, but... (1 Reply)
Discussion started by: Sabari Nath S
1 Replies

7. Shell Programming and Scripting

How to Unzip a .ZIP file in Unix without using unzip cmd..?????

Hi All I have ftped a .ZIP file (zipped using WinZip in Windows) to my Unix server (HP-UX). I don't have unzip cmd available in my curent Unix version Please let me know any cmd in UNIX (other than unzip) using which I can unzip this .ZIP file . Please elaborate on the commands aval and... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

8. UNIX for Advanced & Expert Users

Which Base Level Filesets needed by a specific program?

hello... thats a great forum btw :) my problem is that I need a list of the Base Level Filesets (BLF) which are needed by a specific program. Is there any command/tool which shows me that? during the installation I can choose "Preview only" so that I can see what BLF´s are missing etc but... (4 Replies)
Discussion started by: cypher82
4 Replies

9. Shell Programming and Scripting

Script to unzip specific files

As part of an audit at work I need to review a lot of Windows workstations. One thing I need to do is review all of the .doc files. My problem is that I guessed a number of these files would be in zip archives. This is the case but they are mixed in with a lot of other file types that I am not... (1 Reply)
Discussion started by: stumpyuk
1 Replies

10. HP-UX

How to Unzip a .ZIP file in Unix without using unzip cmd..?????

Hi All I have ftped a .ZIP file (zipped using WinZip in Windows) to my Unix server (HP-UX). I don't have unzip cmd available in my curent Unix version Please let me know any cmd in UNIX (other than unzip) using which I can unzip this .ZIP file . Please elaborate on the commands aval and... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies
Login or Register to Ask a Question