Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using Fedora 17 and bash as my shell. I have a specific zip file, which has just one folder in it's root. I.e. upon unpacking the zip file i see the following:

> unzip myzip.zip
> ls
myzip.zip folderThatWasInsideZip

Supposing you know, that there is only 1 folder packed in the zip file, how do I find out the name of the main folder inside the zip file, without actually unpacking the zip file?

I'm looking for a one-liner, that would enable me to do something like this:

> <command> myzip.zip
folderThatWasInsideZip

I know there are ways to list all the files in the zip with less, but that lists all the files in the subdirectories etc. I just want to know the name of the one folder. I know I'm missing something basic..

share|improve this question
    
the -l option is used to list the files rather than unpack the zip file. see man unzip. –  l1zard Nov 14 '12 at 9:18

1 Answer 1

up vote 2 down vote accepted

This command seems to do what you want:

unzip -qql myzip.zip | head -n1 | tr -s ' ' | cut -d' ' -f5-

Or with GNU sed:

unzip -qql myzip.zip | sed -r '1 {s/([ ]+[^ ]+){3}\s+//;q}'
share|improve this answer
    
That would give me the answer, but it unpacks the zip file. I need to do it without unpacking the zip as I'm writing in the question.. Is there a way? –  Jan Hrcek Nov 14 '12 at 10:33
    
@JanHrcek: looking at gdb traces for unzip and unzip -l shows that inflate_block() function is not called with -l, i.e. the file is not decompressed, only meta data is read. –  Thor Nov 14 '12 at 11:53
    
Yes, you're right :) Sorry. –  Jan Hrcek Nov 14 '12 at 12:40

Your Answer

 
discard

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.