I have folder with sub-folders with same files, for example:


Suffix - is different sizes of images.

How to delete all files with suffix like this:__[a-zA-z0-9].(png|jpg|jpeg)?
And keep only original images without suffix as dsc_9660.jpg in my example

3 Answers 3


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 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

  • 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)

Regular expression visualization

Debuggex Demo and test on files

cd <dir>
find . -type f -iname \*__*.jpg -delete

Try the below command,

cd <dir>
find . -type f -iname \*.jpg -delete

-type----> enter the type of the file ( __[a-zA-z0-9].(png|jpg|jpeg) )and make use of the command.

Hope this helps.

  • In your command you are deleting All .jpg files!! Jan 9, 2015 at 13:14
  • The code in the code box appears to delete all the jpg files in the directory - I'm not sure that's actually helpful! Also, I'm not quite clear what the following line actually means (& I do use find...) as -type f indicates the "type of file" as a regular file, you can't just replace it with a regex or add one to it.. Jan 9, 2015 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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