Re: shell game
"Drew" == Drew Cohan <drewcohan@drewcohan.com> writes:
Drew> Hi, Using a bash shell script (/bin/sh), I need to know how
Drew> to check to see if certain files exist in a directory, as
Drew> in, "Are there any jpegs in this directory?". I've tried
Drew> various things (like using -s, -f with test and a do/for
Drew> loop) but nothing seems to work right. The closest I can
Drew> come is
Drew> if test `ls /opt/images/*.jpg | wc -l` -gt 0 then ...
Drew> Unfortunately, this gives me the error message "ls:
Drew> /opt/images/*.jpg: No such file or directory" when there are
Drew> no jpegs in /opt/images.
Drew> So what am I missing here?
Try this instead:
if [ "`find . -maxdepth 1 -name '*.jpg' -print`" != "" ]
then
# do what you want
fi
The -maxdepth 1 restricts find to the current directory. Take it out
if you want to find anything in subdirectories. If you want to operate
on each file you want to write
for i in `find . -maxdepth 1 -name '*.jpg' -print`
do
# do something with $i
done
Cheers!
Shyamal
Reply to:
- References:
- shell game
- From: "Drew Cohan" <drewcohan@drewcohan.com>