On 08:21 18 Dec 2002, Rick Johnson <[EMAIL PROTECTED]> wrote:
| John French (CIMS) wrote:
| | I have a large number of directories in a single directory.  All of the
| | subdirectories have a single tar file.  Is there a way to go through each
| | directory and extract the files from the tar file into the directory where
| | the tar file is located?
| 
| How deep are the directories? Sounds like shell script time.
| 
| In top level dir:
| 
| # Look for everyting
| for x in *
|       # If directory (can't 'cd' to file)
|       do if [ -d $x ]
|       then
|               # Change to directory
|               cd $x
|               # Look for all tar files
|               for y in *.tar
|                       # For each tar file, extract
|                       do tar xvf $y
|               done
|               # Change back up 1 level
|               cd ..
|       # End if
|       fi
| done

Just a remark about something I often see in scripts, this sequence:

        cd somewhere
        ... do stuff ...
        cd ..

If for _any_ reason your flow of control in "do stuff" bails you past
the second cd your script will get seriously confused.

Usually it's safer to do this:

        ( cd somewhere
          ... do stuff ...
        )

There are occasions when that's won't do, but it's application most of
the time. And more readable to boot, more clearly marking the scope of
the work-in-the-subdir.

I try not to do cds in the top level shell: amongst other issues it
means that any relative pathnames supplied as script argument either
need path hacking or don't work after the cd, being relative to where
you used to be.

Just something to keep in mind.

So if I were writing the above job I'd go:

        for dir in */.  # won't match files - saving some wasted effort in the loop
        do  ( cd "$dir" || exit 1
              for tarf in *.tar
              do  [ ! -s "$tarf" ] || tar xf "$tarf"
              done
            )
        done

Cheers,
-- 
Cameron Simpson, DoD#743        [EMAIL PROTECTED]    http://www.zip.com.au/~cs/

To understand recursion, you must first understand recursion.



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to