Visit Our UNIX and Linux User Community


assign zip directory to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign zip directory to variable
# 1  
Old 10-15-2007
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
# 2  
Old 10-16-2007
What have you tried to do so far ? Can you provide an example ?
# 3  
Old 10-16-2007
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
# 4  
Old 10-16-2007
One possible way, if I understand your question...

Quote:
Originally Posted by skwyer
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
Say you have a directory structure that looks like this:

Code:
.
`-- 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

Code:
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:

Code:
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:

Code:
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:

Code:
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?
# 5  
Old 10-16-2007
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:

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

Thank for your help.

Mike
# 6  
Old 10-16-2007
You're welcome...

No problem. Glad to help.

Previous Thread | Next Thread
Test Your Knowledge in Computers #202
Difficulty: Easy
At compile time, the interpreter parses Perl code into a recursive flat-space module.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. UNIX for Advanced & Expert Users

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (3 Replies)
Discussion started by: kiran425
3 Replies

4. UNIX for Dummies Questions & Answers

How can i assign directory path to a variable in perl?

Hai how can I assign directory path to a variable in perl Thanks&Regards kiran (2 Replies)
Discussion started by: kiran425
2 Replies

5. Shell Programming and Scripting

Command to zip a directory

Hi , How to compress the entire directory . Rajesh (1 Reply)
Discussion started by: guddu_12
1 Replies

6. UNIX for Dummies Questions & Answers

Zip all files in a directory and move to another directory

Hi, need to zip all files in a directory and move to another directory after the zip.. i am using this one but didnt help me... zip -r my_proj_`date +%Y%m%d%H%MS`.zip /path/my_proj mv in_proj_`date +%Y%m%d%H%M%S`.zip /path/source/ i am trying to zip all the files in my_proj... (0 Replies)
Discussion started by: dssyadav
0 Replies

7. Shell Programming and Scripting

Shell assign variable to another variable

How can I assign a variable to an variable. IE $car=honda One way I can do it is export $car=honda or let $car=2323 Is there any other ways to preform this task (3 Replies)
Discussion started by: 3junior
3 Replies

8. UNIX for Dummies Questions & Answers

Zip files inside the directory, but not the directory itself

Hi, Im facing a problem that im stucked, I have the following structure: thales@pereirtc-vbox:/home/VfARM$ ls code config doc lib manifest.bak manifest.rel manifest.v3 ns pub if i try to execute zip -q -o arm.zip VfARM/* it will create a zip file with the folder VfARM.... (2 Replies)
Discussion started by: Thales.Claro
2 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Zip all files in a directory

I have two files in a directory: xxxx.txt xxxx.csv I need to zip both files up but the command I am doing below is only zipping xxx.txt file????: cd ../$CUSTOMER_DIRECTORY zip -q ${name}${FILE_OUT_NAME}.zip ls -lrt *.csv *.txt can anyone help?:confused: (3 Replies)
Discussion started by: Pablo_beezo
3 Replies

Featured Tech Videos