Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: iterating through a hierarchy with a filter

On Thu, 10 Apr 2008 02:03:21 -0700
Alexy Khrabrov <deliverable@xxxxxxxxx> wrote:
> On Apr 10, 2008, at 1:51 AM, Peter Stephenson wrote:
> >
> > for file1 in source/**/*.xml; do
> >  file2=dest/${${file1##source/}:r}.txt
> >  destdir=${file2:h}
> >  [[ -d $destdir ]] || mkdir -p $destdir
> >  filter <$file1 >$file2
> > done
> >
> > If that doesn't work even with a few small tweaks, you'll probably  
> > have to tell us why before we can advise better.
> Well, my hierarchy is a million small files.  So I doubt globbing will  
> work -- should I try?  :)

You might get lucky, but it sounds like you need to do it the hard way.
You can use that code as the core, except you can move the directory
handling out of the way and end up with something like (again this is

handledir() {
  local sdir=$1 ddir=$2
  local dir file tail

  [[ -d $ddir ]] || mkdir -p $ddir

  for dir in $sdir/*(/N); do
     handledir $dir $ddir/${dir:t}

  for file in $sdir/*.xml(N)); do
     filter <$file >$ddir/{$file:t}

handledir sourcedir destdir

The only special zsh features are the globbing flags.  (N) forces
the expression to expand to nothing at all if no patterns matched.

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

Messages sorted by: Reverse Date, Date, Thread, Author