Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Hence I was extracting the folder-name from the zip file.

I hit a roadblock where a folder called snaps was zipped as 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

share|improve this question

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.

share|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
@smokinguns: If you found a way to do it, post your own answer and accept it. – Wuffers May 25 '11 at 0:32
up vote 0 down vote accepted

Found a way to do it!

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

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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