Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create list of directories and use it in script

Status
Not open for further replies.

liondari

Technical User
Oct 4, 2004
7
GR
Hello everybody, how are you?

I hope someone can help me!

I would like to write a script which zips automatically
a complete directory structure.
For example, there is a directory dirA which contains, say,
5 subdirs sdirA1 .. sdirA5.
Another dir dirB which contains 3 subdirs sdirB1 .. sdirB3. And so on.
Now I would like to zip each subdir in dirA and dirB separately:

for i in list_of_directories <---dirA,dirB,..
do
cd $i
for j in list_of_subdirs <---sdirA1..sdirA5,sdirB1..
do
tar -cf $j.tar $j/*
bzip2 $j.tar
done
cd ..
done

Thank you in advance!
 
As a start point try:
find /path/to/dirA -type d

to produce a list of full path names to all sub-directories in dirA

I hope that helps.

Mike
 
Try
Code:
for i in *         #for each entry
do
  if [ -d $i ]     #if entry is a directory
  then
    cd $i          #cd to that directory
    for j in *     #for each entry
    if [ -d $j ]   #if entry is directory
    then
                   #tar and zip it up
      tar -cf - $j | bzip2 -c > ${j}.tar.bz
    fi
    cd ..          #back to parent directory
  fi
done
You might want to replace the for j in * with for j in $(ls) as I'm not quite sure where the script is going to expand the '*'
Please also note there is no error checking!!. If any of the cd $i fails then the script will run out of control. I've also used the -c glag for bzip2 on the grounds that it is correct for gzip and I believe they're compatible. This needs testing

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top