How can I delete all files that have names not ending on exactly 2 digits?

Solution 1:

Please try the different methods below:

1

find . -regextype posix-egrep -regex '(.*[a-zA-Z][0-9]{3,}.[a-zA-Z]{3})|(.*[a-zA-Z][0-9].[a-zA-Z]{3})' | xargs rm

2

rm -fr `ls | grep -E -v '([0-9])*([a-zA-Z]+[0-9]{2})\.txt'`

3

rm -fr `ls | grep -E '([0-9])*([a-zA-Z]+[0-9]{3,})\..*|([0-9])*([a-zA-Z]+[0-9]{1})\..*'`

By the way, thee is this tool that is very helpful to learn regex: https://regexr.com/

You can also install txt2regex (Regex build assistant): https://aurelio.net/projects/txt2regex/