I'm trying to write an alias to list all the files in a directory ending in JP(E)G in its various forms. The ones I'm looking for it to recognise are .jpg .JPG .jpeg and .JPEG.

Is there a form of ls (or ls in combination with another command, such as find or grep) that will do this?


Note that it's not ls that expands wildcards, it's the shell.

bash can make all its globs case insensitive with the nocaseglob option, but contrary to ksh93 or zsh doesn't have a glob operator for having a single glob or part of a single glob case insensitive.

However, you can always do:

ls -ld -- *.[jJ][pP][gG]

Which also has the benefit of not leaving it up to the locale to decide what is the lower or upper case variant of a given letter (for instance, is the upper case variant of gif GIF or GİF?).

Or with the extglob option to also cover jpeg:

ls -ld -- *.[jJ][pP]?([eE)[gG]

With zsh, and with the extendedglob option (set -o extendedglob):

ls -ld -- *.(#i)jp(e|)g

With ksh93:

ls -ld -- *.~(i)jp?(e)g


ls -ld -- *.~(i:jp?(e)g)

Short answer

No. But then ls does not have a way to list file ending .jpg case sensitive or note. It is the shell that converts the *.jpg into a list of files, and passes this list to ls. Try echo *.jpg, to get some ideas of what the shell is doing.

Long answer

You can use find: e.g. find . -iname "*.jpg" -o -iname "*.jpeg"

or use grep e.g. ls | grep -iE "[.]jpe?g$"

or set your shell to have case insensitive globs: shopt -s nocaseglob (This works in Bash, see How to match case insensitive patterns with ls? for solutions for other shells.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.