Try find command with its -regex
switch or -iregex
(case insensitive), change your regex to .*__[a-zA-z0-9_]*\.(png|jpg|jpeg)
and use the -delete
option to delete the matched file:
find . -type f -regextype "posix-egrep" -iregex '.*__[a-zA-z0-9_]*\.(png|jpg|jpeg)$'
You can use other types of -regextype
(which is worked) in place of "posix-egrep" like:
"posix-extended" or "posix-awk".
-regex pattern
File name matches regular expression pattern. This is a match on the whole path, not a search. For
example, to match a file named './fubar3', you can use the regular expression '.*bar.' or '.*b.*3',
but not 'f.*r3'. The regular expressions understood by find are by default Emacs Regular Expres‐
sions, but this can be changed with the -regextype option.
-iregex pattern
Like -regex, but the match is case insensitive.
-regextype type
Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the
command line. Currently-implemented types are emacs (this is the default), posix-awk, posix-basic,
posix-egrep and posix-extended.
-delete
Delete files
At the end add -delete
switch to command to deleting matched files:
find . -type f -regextype "posix-egrep" -iregex '.*__[a-zA-z0-9_]*\.(png|jpg|jpeg)$' -delete
.*__[a-zA-z0-9_]*\.(png|jpg|jpeg)
- The
.*
matches everything before __
- The
__
matches double underscores.
- The
[a-zA-z0-9_]*
matches all alphanumeric and a single underscore _
characters. The *
means this class of characters can be zero length.
- The
\.
matches a single dot. To match a dot (.
) literally you need to escape it within the regular expression using a backslash; without escaping, it matches any single character.
- In the
(png|jpg|jpeg)
, pair of parentheses makes it as a group of matches. Will match png
or (|
) jpg
or jpeg
.
- The
$
anchor, matches the end of string (or file names in this case)

Debuggex Demo and test on files