I have a shell script where I unzip a zip file and its contents will always be one folder.

In my code I was assuming that the name of the zip file will be reflective of the folder name (e.g. folder named test will always be zipped as test.zip). Hence I was extracting the folder-name from the zip file.

I hit a roadblock where a folder called snaps was zipped as photos.zip. Now, if I extract the folder name from the zip file, it will be the wrong folder name.

How I can get the correct folder name snaps after unzipping photos.zip?

link|improve this question

71% accept rate
feedback

3 Answers

You're better off creating your own directory and using unzip -j so that all the files in the archive are extracted to that one directory.

link|improve this answer
I dont want to do that. I guess my option is to use unzip -l so that the correct folder name can be accessed – smokinguns May 24 '11 at 23:16
1  
@smokinguns: If you found a way to do it, post your own answer and accept it. – Wuffers May 25 '11 at 0:32
feedback
up vote 0 down vote accepted

Found a way to do it!

unzip -l photos.zip | awk 'NR==4{print $4}' | sed 's/\/// '

link|improve this answer
feedback

I always use dtrx (Do The Right Extraction) for all my unzipping needs. The script detects the compression format and calls the right program with the right parameters. I strongly recommand it, and there is a flag to unzip to the a folder named as per the zip file.

dtrx is most likely in your package manager, and will save a lot of headaches.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.