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

fedoralinuxzip

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

Best Answer

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