From: Lao Ming on
I want to list all the images in a directory but no other files. If I
try e.g.:

ls -al *.jpg *.png *.gif

I can get a failure if there is, e.g. no GIF.

In a script, how would I test for this before sending the file
extensions to a while loop?

This does not work:


ft=""
[ -f *.jpg ] && ft="${ft} *.jpg"
[ -f *.png ] && ft="${ft} *.png"
[ -f *.gif ] && ft="${ft} *.gif"

i=0
ls "$ft" |
{
while IF="
" read -r file ; do
....
From: Greg Russell on
"Lao Ming" <laomingliu(a)gmail.com> wrote in message
news:d7285527-2dbd-4456-a6a1-3a1c33417a76(a)x2g2000prk.googlegroups.com...

> I want to list all the images in a directory but no other files. If I
> try e.g.:
> ls -al *.jpg *.png *.gif
> I can get a failure if there is, e.g. no GIF.

You're not testing for file types, you're testing for file names. Try:

file * | grep "image data" | cut -d: -f1


From: Lao Ming on
On Jul 13, 2:13 pm, "Greg Russell" <gruss...(a)example.con> wrote:
> "Lao Ming" <laoming...(a)gmail.com> wrote in message
>
> news:d7285527-2dbd-4456-a6a1-3a1c33417a76(a)x2g2000prk.googlegroups.com...
>
> > I want to list all the images in a directory but no other files.  If I
> > try e.g.:
> >     ls -al *.jpg *.png *.gif
> > I can get a failure if there is, e.g. no GIF.
>
> You're not testing for file types, you're testing for file names. Try:
>
>     file * | grep "image data" | cut -d: -f1

This and Janis's solution both worked. I used this just because /usr/
bin/file eliminated using the file extension as you mentioned. Thanks
a bunch. This is the script which provides the dimensions of all
images in a directory (on OSX):

#!/bin/bash
i=0; file * |grep "image data" |cut -f 1 -d : |
{
while IFS="
" read -r f ; do
i=$(( $i + 1 ))
w=$( mdls -name kMDItemPixelWidth "$f" |cut -d" " -f3 )
h=$( mdls -name kMDItemPixelHeight "$f" |cut -d" " -f3 )
printf "%4s %4s %s\n" "$w" "$h" "${f##*/}"
done
}

It would be nice to be a bit faster but it's not that important.
Comments & criticisms welcome.