GrooveInShell
GrooveInShell

Reputation: 81

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

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?

Upvotes: 2

Views: 75

Answers (3)

TheAnalogyGuy
TheAnalogyGuy

Reputation: 497

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

Upvotes: 0

Greg A. Woods
Greg A. Woods

Reputation: 2792

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

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

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.

Upvotes: 2

Related Questions