Home > Mobile >  How to find multiple files with different ending in LInux using regex?
How to find multiple files with different ending in LInux using regex?

Time:11-07

Let's say that I have multiple files such as:

root.file991
root.file81
root.file77
root.file989

If I want to delete all of them, I would need to use a regex first, so I have tried:

find ./ - regex '\.\/root'

...which would find everything in root file, but how do I filter all these specific files?

CodePudding user response:

I'm not quite sure what you mean by "files in root file" but if I understand correctly regular POSIX glob(7) pattern matching should be sufficient:

rm root.file[0-9]*

CodePudding user response:

You can use

find ./ -regextype posix-extended -regex '\./root\.file[0-9] '

The regex will match paths like

  • \. - a dot
  • /root\.file - a /root.file text
  • [0-9] - ending with one or more digits.

CodePudding user response:

Depending on how complex the other files are, you may have to build up the regex more. $ man find has useful help as well. Try the following:

$ find ./ -regex '\.\/root.file[0-9].*'

# if that works to find what you are looking for, add the -delete
$ find ./ -regex '\.\/root.file[0-9].*' -delete
  • Related