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

Re: how to rename multiple files



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 .

Because I have now guarded the amount of data this produces I would
probably use this following construct to test the existence of output.
I would never do this without making sure the output is bounded to
something reasonably small.  In this case the maximum length of one
filename.

  [ -n "$(find . -maxdepth 1 -name '*.jpg' -print | head -n 1)" ]

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?

> (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.

Bob

Attachment: pgp0jklDR1gKt.pgp
Description: PGP signature


Reply to: