Spidey
Spidey

Reputation: 193

Extract zip files contents and rename the directory dynamically

I have an application zip file created using Play Framework. It create the zip file with name A-1.0.zip. This zip file contains the directory with name A-1.0. (1.0 changes according to the version)

I wanted to extract the zip file and rename the folder from A-1.0 to A. So that my application init.d script finds the directory to start the application. This shuld be done dynamically using shell script.

Is there a way where i can extract all the zip files into A folder instead of extracting into A-1.0 and renaming?? Please help!

The following is what I tried....

unzip A-1.0.zip -d ~/A

(I know that it is very dumb of me to do this !!)

This extracted the file into ~/A/A-1.0/[contents]

I need to extract all the [contents] into ~/A instead of ~/A/A-1.0/. I dunno how to do this using command line.....

My init.d script searched for ~/A/bin/A -Dhttp.port=6565 -Dconfig.file=~/A/conf/application.conf to start the Play! application.

To make this script working, I extract all into A-1.0/ then I rename with mv ~/A-1.0 ~/A manually.

Upvotes: 8

Views: 13807

Answers (4)

Shade
Shade

Reputation: 33

I was looking to unzip all .zip files in the current directory into directories with names of the zip files (even after you rename them)

the following command is not elegant but works

cd to/the/dir
find *.zip | cut -d. -f1 | xargs -I % sh -c "unzip %.zip -d %; ls % | xargs -I @ sh -c 'mv %/@/* %; rm -rf %/@'"

Upvotes: 0

Thor_Bux
Thor_Bux

Reputation: 1157

EDIT: This answer does not preserve subdirectories. It works fine if one doesn't have or need the subdirectory structure.

I found that you can combine the answer from @géza-török with the -j option mentioned by @david-c-rankin (in the comment below the question). Which leads to unzip -j A-1.0.zip 'A-1.0/*' -d /the/output/dir. That would only process the files inside A-1.0/ and output them straight into the given output directory.

Source: https://linux.die.net/man/1/unzip (look at -j)

Upvotes: 0

Alexis LEGROS
Alexis LEGROS

Reputation: 559

I didn't find any specific unzip option to perform this automatically, but managed to achieve this goal by creating a temporary symbolic link in order to artificially redirect the extracted files this way

ln -s A A-1.0
unzip A-1.0.zip
rm A-1.0

Upvotes: 5

From the unzip man page it boils down to:

unzip A-1.0.zip 'A-1.0/*' -d /the/output/dir
      ^         ^
      |         |
      |         +- files to extract (note the quotes: unzip shall parse the wildcard instd of sh)
      +- The archive

Upvotes: 2

Related Questions