[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to rename multiple files



On Sat, Jan 04, 2003 at 09:55:42PM -0700, Bob Proulx wrote:
> Colin Watson <cjwatson@debian.org> [2003-01-05 04:04:19 +0000]:
> > > And I've bumped into this. How *DOES* one test for the existence of
> > > ANY file with a given extension without getting a "too many arguments"
> > > error when there are multiple files?
> > 
> > How about:
> > 
> >   find . -maxdepth 1 -name '*.jpg' -print | grep -q .
> 
> Very nice.  But if there _were_ thousands and thousands you spend cpu
> time grep'ing through all of them.  As long as you have one you can
> quit early.  How about this tweak?  Of course you spawn one more
> process.  But it is small and goes away quick.
> 
>   find . -maxdepth 1 -name '*.jpg' -print | head -n 1 | grep -q .

-q already stops at the first match, so there is no need to do this. To
be honest I'm more concerned about find spending ages looking for more
than one file (which involves time-consuming system calls, I/O, etc.)
than about grep looking at the output (which doesn't) anyway.

> Doing it this way instead of using the exit code I am trying to avoid
> the pipeline exit code problem.  In older shells it was the return
> code of the last program to exit and not necessary the last program in
> the pipeline.  Perhaps I am too paranoid these days.  Does anyone know
> what the standards (SUSv3?) say about this?

SUSv3:

  If the reserved word ! does not precede the pipeline, the exit status
  shall be the exit status of the last command specified in the
  pipeline. Otherwise, the exit status shall be the logical NOT of the
  exit status of the last command. That is, if the last command returns
  zero, the exit status shall be 1; if the last command returns greater
  than zero, the exit status shall be zero.

SUSv2's text differs only in having "will" instead of "shall".

> > (The first '.' and the '-print' are redundant with GNU find, but useful
> > on other systems.)
> 
> But '-maxdepth 1' is a GNU find extension and so this won't work on
> other systems.

D'oh! Thanks.

-- 
Colin Watson                                  [cjwatson@flatline.org.uk]



Reply to: