Find filenames with uppercase extension

2

How can I find all the files with their extension in uppercase in recursively in a directory.

I've tried :

$ find -name "*.[A-Z][A-Z][A-Z]"

Seems to work but of course this does not check files with more than 3 letters after the last dot.

40detectives

Posted 2018-10-06T15:15:22.163

Reputation: 45

Answers

5

The following works for simple cases:

find -name "*.*[A-Z]*" ! -name "*.*[^A-Z]*"

It is liable to fail for files with two or more dots in the name. For this case you need to use Regular Expressions, eg with grep:

find | grep '\.[A-Z][A-Z]*$'

or egrep:

find | egrep '\.[A-Z]+$'

Following Kamil Maciorowski's comment, the answer could be made locale-independent by using [:upper:] in place of A-Z, as in:

find | egrep '\.[[:upper:]]+$'

I'm afraid that we native-English speakers can easily forget such matters.

AFH

Posted 2018-10-06T15:15:22.163

Reputation: 15 470

1

Avoid [A-Z]. How to make grep '[A-Z]' independent of locale?

– Kamil Maciorowski – 2018-10-06T21:17:08.103

@KamilMaciorowski - A valid point, but it was the questioner who originally suggested that [A-Z] was what he wanted. – AFH – 2018-10-06T21:55:52.363

2

You can use RE with -regex option

find -regextype posix-egrep -regex '.*\.[A-Z]{3}'

There are some points using RE with find, I learned that in a pt_BR book.

-regex option wants to match the whole path, so the .* matches everything before the actual RE you want to match. If the RE can be in the middle, another .* must be place at the end.

The -regextype tells which meta-characters must be escaped or not.

What I know is that in Unix/BSD find -E allows all meta-characters unescaped, and in GNU/Linux the same is -regextype posix-extended, -regextype posix-awk or -regextype posix-egrep.

Paulo

Posted 2018-10-06T15:15:22.163

Reputation: 606

More info on the manual https://www.gnu.org/software/findutils/manual/html_mono/find.html#Regular-Expressions

– Paulo – 2018-10-06T21:31:32.500