Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 882
Spidey Avatar asked Nov 06 '14 06:11

Spidey


People also ask

Can you Rename a file within a zip folder?

Click the file or folder, then press F2. Click the file or folder twice, slowly. Right-click the file or folder and choose Rename from the shortcut menu.

How do I Rename a Compressed zipped folder?

A new zipped folder with the same name is created in the same location. To rename it, press and hold (or right-click) the folder, select Rename, and then type the new name.

How do I extract data from a zip file?

Right-click the file you want to zip, and then select Send to > Compressed (zipped) folder. Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it.


2 Answers

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
like image 85
Alexis LEGROS Avatar answered Oct 22 '22 07:10

Alexis LEGROS


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
like image 39
Géza Török Avatar answered Oct 22 '22 07:10

Géza Török