From: Janis Papanagnou on
On 13/07/10 02:54, Ben Bacarisse wrote:
> Lao Ming <laomingliu(a)gmail.com> writes:
>
>> I want to list all the images in a directory but no other files. If I
>> try e.g.:
>>
>> ls -al *.jpg *.png *.gif

(You don't need option -a in ls, BTW.)

A simple way is to suppress the messages by redirection of stderr.

Another way is to use the globbing facilities of modern shells

ls @(*.jpg|*.png|*.gif)

(in bash you have to enable extended globbing to have that feature,
in ksh it's available per default).

Or use find, e.g.

find . -maxdepth 1 -name "*.jpg" -o -name "*.png" -o -name "*.gif"

>>
>> I can get a failure if there is, e.g. no GIF.
>
> Does *.{jpg,png,gif} meet your needs? It, too, fails if there is no
> match but this might be a good thing.

But that way you get exactly the same error message as with the OP's
command, and the OP explicitly said he wants to avoid the error message.

Janis

>
> <snip>

From: Icarus Sparry on
On Tue, 13 Jul 2010 03:19:25 +0200, Janis Papanagnou wrote:

> On 13/07/10 02:54, Ben Bacarisse wrote:
>> Lao Ming <laomingliu(a)gmail.com> writes:
>>
>>> I want to list all the images in a directory but no other files. If I
>>> try e.g.:
>>>
>>> ls -al *.jpg *.png *.gif
>
> (You don't need option -a in ls, BTW.)
>
> A simple way is to suppress the messages by redirection of stderr.
>
> Another way is to use the globbing facilities of modern shells
>
> ls @(*.jpg|*.png|*.gif)
>
> (in bash you have to enable extended globbing to have that feature, in
> ksh it's available per default).

Another, less portable, option is to do

shopt -s nullglob

in bash. This makes the shell convert '*.jpg' into nothing if there is no
match, and into a list of files if there is a match, rather than leaving
it as '*.jpg' if there is no match (and hence ls et al printing error
messages). Of course in this case, as in Janis's solution, you may need
to worry about the case where no files match at all, as "ls {nothing}" is
special cased to mean the same as "ls ." ....
From: Janis Papanagnou on
Icarus Sparry schrieb:
> On Tue, 13 Jul 2010 03:19:25 +0200, Janis Papanagnou wrote:
>
>> On 13/07/10 02:54, Ben Bacarisse wrote:
>>> Lao Ming <laomingliu(a)gmail.com> writes:
>>>
>>>> I want to list all the images in a directory but no other files. If I
>>>> try e.g.:
>>>>
>>>> ls -al *.jpg *.png *.gif
>> (You don't need option -a in ls, BTW.)
>>
>> A simple way is to suppress the messages by redirection of stderr.
>>
>> Another way is to use the globbing facilities of modern shells
>>
>> ls @(*.jpg|*.png|*.gif)
>>
>> (in bash you have to enable extended globbing to have that feature, in
>> ksh it's available per default).
>
> Another, less portable, option is to do
>
> shopt -s nullglob
>
> in bash.

Being a specific bash'ism is certainly one issue to consider.

Though, what I dislike most with such 'shopt' constructs is that - as
opposed to a syntactic construct like '@(...)' - you have to explicitly
turn it on and off, which is bulky to apply if you want to use it just
for few commands. OTOH, the solution you get with 'shopt extglob' can
(IMO) be left active generally, while the 'shopt nullglob' is likely a
lot more context dependent, and you'd occasinally have to switch mode;
switching modes across a script (and also during interactive sessions)
is certainly error-prone.

That all said from the view of a user who regularily uses @(...) in ksh
without having ever observed the need to turn off that globbing syntax.

Having the 'nullglob' feature available as a "per-use" operator would
probably fit better to the purpose of that operational semantics than
a fixed mode. (I'm unsure, and curious, how zsh handles that.)

Janis

> This makes the shell convert '*.jpg' into nothing if there is no
> match, and into a list of files if there is a match, rather than leaving
> it as '*.jpg' if there is no match (and hence ls et al printing error
> messages). Of course in this case, as in Janis's solution, you may need
> to worry about the case where no files match at all, as "ls {nothing}" is
> special cased to mean the same as "ls ." ....
From: Alan Curry on
In article <i1hvv2$62f$1(a)speranza.aioe.org>,
Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote:
>
>Having the 'nullglob' feature available as a "per-use" operator would
>probably fit better to the purpose of that operational semantics than
>a fixed mode. (I'm unsure, and curious, how zsh handles that.)

With the glob qualifier "N", as in:

echo *.jpg(N)

--
Alan Curry