4

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?

3 Answers 3

0

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.

2
  • 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, 2011 at 23:16
  • 1
    @smokinguns: If you found a way to do it, post your own answer and accept it.
    – Wuffers
    May 25, 2011 at 0:32
0

Found a way to do it!

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

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .