assign zip directory to variable

I'm running an sh shell that is unzipping some zip files that have a directory structure on them.

Is there a way I can find the top level directory in the zip file and assign that to a variable?

mike

What have you tried to do so far ? Can you provide an example ?

I tried piping an unzip test to a find but that didn't work. I also tried grep but am basically at a loss as to how to search through the zip file.

Mike

Say you have a directory structure that looks like this:

.
`-- test1
    |-- file1
    |-- file2
    |-- file3
    `-- test2
        |-- file4
        |-- file5
        |-- file6
        `-- test3
            |-- file10
            |-- file7
            |-- file9
            `-- fle8

and you tarball it up into test.tar.gz.

Then one way to find assign the top level of the directory structure (test1) to a variable is to do the following

DIRECTORY_VARIABLE=$(tar -tzvf test.tar.gz | cut -d" " -f6 | cut -d"/" -f1 | head -1)

We assign the value to the DIRECTORY_VARIABLE using command substitution, $(). The commands to get the answer take care of splitting the parts we need to the final name.

First, tar -tzvf test.tar.gz will list the table of contents of the tar file. You need -z to take care of the decompression. The output of that alone looks like this:

drwxr-xr-x username/username 0 2007-10-16 09:33 test1/
drwxr-xr-x username/username 0 2007-10-16 09:33 test1/test2/
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/test2/file4
drwxr-xr-x username/username 0 2007-10-16 09:33 test1/test2/test3/
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/test2/test3/file10
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/test2/test3/fle8
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/test2/test3/file9
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/test2/test3/file7
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/test2/file5
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/test2/file6
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/file1
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/file3
-rw-r--r-- username/username 0 2007-10-16 09:33 test1/file2

That's piped to the first cut command that splits the output above into fields separated by spaces (-d" "). If you count the output above like that, you can see that the information that we need is held in field six (-f6). What we end up with is this:

test1/
test1/test2/
test1/test2/file4
test1/test2/test3/
test1/test2/test3/file10
test1/test2/test3/fle8
test1/test2/test3/file9
test1/test2/test3/file7
test1/test2/file5
test1/test2/file6
test1/file1
test1/file3
test1/file2

Closer, but not quite there.

The second cut statement will trim off the fat. It separates on fields delimited by slashes (-d"/") and we want the first field (-f1). It ends up looking like this:

test1
test1
test1
test1
test1
test1
test1
test1
test1
test1
test1
test1
test1

Closer still, but we don't want all of those results stuffed into our variable, so the final head statement just pulls the top value. We could have used other methods like piping to sort and then uniq or using "tail -1," but this works.

Is this what you are looking for?

BRILLIANT!

Actually it wasn't a tarball but a regular .zip file so I had to modify your advice accordingly. I went with this and it worked:

VARIABLE=`unzip -tlv $PATH | cut -d"/" -f1 | cut -d":" -f2 | cut -d" " -f2 | head -2`

Thank for your help.

Mike

No problem. Glad to help.