Re: shell game
On Sat, Dec 07, 2002 at 11:47:54AM -0700, Bob Proulx wrote:
> Drew Cohan <drewcohan@drewcohan.com> [2002-12-07 01:00:29 -0500]:
> >
> > Using a bash shell script (/bin/sh), I need to know how to check to see
> > if certain files exist in a directory, as in, "Are there any jpegs in
> > this directory?". I've tried various things (like using -s, -f with
> > test and a do/for loop) but nothing seems to work right. The closest I
> > can come is
> >
> > if test `ls /opt/images/*.jpg | wc -l` -gt 0 then ...
>
> Ugh.
>
> > So what am I missing here?
>
> You are trying to find files in a directory. Therefore I recommend
> you use the 'find' command.
>
> find . -name '*.jpg' -print
>
> I assume you will want to operate on those files once you have found
> them. List them and save the list and then operate on the list.
> (Slightly possible that you will exceed ARG_MAX. File names with
> spaces and newlines will cause problems.)
>
> mp3playlist=$(find . -name '*.jpg' -print)
>
> for mp3 in $mp3playlist; do
> mpg123 $mp3
> done
Why not just
find . -name \*.jpg -exec mpg123 {} \;
?
Much shorter, and no problems with number of arguments...
However, be careful with find. If you do not want to search
subdirectories, it is probably not what you want.
Frank
>
> Bob
Reply to: