LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   running a zip command in a bash script (http://www.linuxquestions.org/questions/linux-software-2/running-a-zip-command-in-a-bash-script-818412/)

onuhwt 07-07-2010 01:00 AM

running a zip command in a bash script
 
Hello all, I'm very new to writing bash scripts (wrote my first one this Sunday) and I'm trying to zip a group of files. It has to be in zip format so alternatives like tar won't work here. I have my script in a folder which has a bunch of Sub-directories in the format "Lab\ 3" "Lab\ 5" "Lab\ 6" etc

What I'd like to be able to do is take all of the files (just the contents not the folder itself) in the "Lab\ 3" Folder and put them in Lab3.zip. I'm really close but no matter what I try I keep getting the folders put into the zip file instead of the Folders contents

Code:

#!/bin/bash

LabNum=(3 5 6)

for ARG in ${LabNum[@]};
    do
        ` zip -r Lab$ARG Lab\ $ARG/*`
    done

Thanks in advance

Elv13 07-07-2010 01:11 AM

you could add a second level of loop and use zip -g (grow/append) to add each files one after the other. You can "cd" into the directory just before and use
Code:

for FILE in `ls`;do
  zip -g Lab$ARG.zip $FILE
done

I did not tested (you may have to create the archive before?) but it might help you.

onuhwt 07-07-2010 01:45 AM

Thanks
 
Quote:

Originally Posted by Elv13 (Post 4025822)
you could add a second level of loop and use zip -g (grow/append) to add each files one after the other. You can "cd" into the directory just before and use
Code:

for FILE in `ls`;do
  zip -g Lab$ARG.zip $FILE
done

I did not tested (you may have to create the archive before?) but it might help you.

That did exactly what I wanted Thank you very much Elv13. If anyone else stumbles on this thread looking for what I did I'll include the code below. Thanks again

Code:

#!/bin/bash

LabNum=(3 5 6)

for ARG in ${LabNum[@]};
    do
        #zip -r Lab$ARG "Lab\ $ARG/*"
        cd Lab\ $ARG       
        for FILE in `ls`;do
          zip -g ../Lab$ARG.zip $FILE
        done
        cd ..


    done



All times are GMT -5. The time now is 10:06 PM.