Find out the name of a folder in a root directory inside .zip file

3

1

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..

Jan Hrcek

Posted 2012-11-14T07:12:03.200

Reputation: 135

the -l option is used to list the files rather than unpack the zip file. see man unzip. – l1zard – 2012-11-14T09:18:49.137

Answers

3

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}'

Thor

Posted 2012-11-14T07:12:03.200

Reputation: 5 178

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 – 2012-11-14T10:33:11.877

@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 – 2012-11-14T11:53:49.373

Yes, you're right :) Sorry. – Jan Hrcek – 2012-11-14T12:40:41.210

3

unzip -Z invokes Zipinfo mode, which means you could call unzip -Z -1 myzip.zip | head -1 for the same result, but it's a lot more terse

jaygooby

Posted 2012-11-14T07:12:03.200

Reputation: 141

1

You can do it with the following shell command:

unzip -qql myzip.zip | head -n1 | awk '{print $4}'

It is a unix command pipeline. unzip -qql lists the files in the zip file. head -n1 cuts its first line, while awk '{print $4}' prints its fourth word.

Note, it doesn't work correctly it the first entry in the zip file contains a space. Typically, it is a bad practice to process the output of such commands, but it might be okay for a quick & dirty solution.

Mike Perez Ontiveros

Posted 2012-11-14T07:12:03.200

Reputation: 11