Search code examples
regexterminalfind

OSX Terminal find regex for jpg JPG and jpeg


find ./ -name '*.JPG' -type f | wc -l

gives me 1231 and

find ./ -name '*.jpg' -type f | wc -l

gives 1377

How can I do a regex to find all JPGs ?

I've tried

find ./ -type f -regex ".*\.[JPGjpg]$" | wc -l

but nothing also similar works


Solution

  • Your regex is hosed. But by using a different option, it can be simplified to be a lot less complex / intimidating.

    find . -E -type f -iregex '.*\.jpe?g'
    

    Square brackets create a character class -- [JPG|jpg] matches a single character which is one of J, P, G, or vertical bar (yes, everything between the square brackets is taken literally) in upper or lower case. Use round parentheses for grouping; although this is simple enough to not require any grouping. Note also the use of -iregex to make a case-insensitive regex match.

    As noted in comments, on e.g. MacOS / BSD the -E option is necessary to enable ERE constructs like ? for marking an expression as optional.