8ToThePowerOfMol
8ToThePowerOfMol

Reputation: 91

Regex: Find files not ending with numeral suffix

I need to make a command which returns all files without numeral suffix (*.0, *.123, ...)

Have for example three files:

gg.p  qqq.449  rtr55

I want to find only these:

./rtr55
./gg.p

I tried to find them using grep. However I got only results with no effect.

find -type f | grep -v '\.[0-9]+$'

(This command returned:)

./qqq.449
./rtr55
./gg.p

So there is probably some regex format error. Do you know, how to fix it?

Upvotes: 0

Views: 107

Answers (2)

tripleee
tripleee

Reputation: 189377

Why would you use grep at all?

find -regex '.*\.[0-9][0-9]*' -prune -o -type f

If your expressions are simple enough (or your find doesn't support -regex), you could use -name instead of -regex but a glob wildcard can't capture an arbitrary amount of numbers after the dot. Here's one or two:

find -name '*.[0-9]' -prune -o -name '*.[0-9][0-9]' -prune -o -type f

Notice that this isn't purely an efficiency question; grep would simply not do the right thing if you ever come across file names with newlines in them.

Upvotes: 0

logi-kal
logi-kal

Reputation: 7880

The + operator belongs to the extended regular expressions. There are many workarounds:

find -type f | grep -v '\.[0-9]\+$'
find -type f | egrep -v '\.[0-9]+$'
find -type f | grep -E -v '\.[0-9]+$'
find -type f | grep -v '\.[0-9][0-9]*$'

Upvotes: 1

Related Questions