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

Re: shell game



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

Bob

Attachment: pgpHGlosp75se.pgp
Description: PGP signature


Reply to: