Searching for and deleting files with a certain extension or ending with a number

command linefind

I'd like my script to find a certain file type and delete it.

I know that find path/to/directory/ -type f -name '*.ext' -delete will find and delete all files with the extension.

Now I'd also like to have my script remove any file ending with a number regardless of the extension, as well as the files ending with the extension.

I tried the following commands with no success:

find path/to/directory/ -type f -name '*.ext' -name '*[0-9].* -delete
find path/to/directory/ -type f -name '*.ext' '*[0-9].* -delete
find path/to/directory/ -type f -name '*.ext,*[0-9].* -delete

None of these worked, I'm not sure what exactly I need to change or what I am doing wrong.

Best Answer

remove the files ending with extension as well as any file ending with a number regardless of the extension

That requires conditions joined by logical OR instead of the default logical AND (parentheses are required because OR has lower precedence than AND, and they must be escaped or quoted so that the shell passes them to find as literals):

find path/to/directory/ -type f \( -name '*.ext' -o -name '*[0-9].*' \) -print

Change -print to -delete once you are certain that it's doing the right thing. In the GNU implementation of find, you may use -or in place of -o if you prefer.