Li haonan
Li haonan

Reputation: 630

regex in command line

I want to find the file with name like ...(1-9)...

find . -regex '.*/.*\([1-9]\).*'

I first use this one, but it seems give me all file with 1-9 in their path. And I try

find . -regex '.*/.*([1-9]).*'

and it works. But I don't know why. I remember in regex the parenthesis mean a expression? or you want to quote it later using sth like \1, \2? That's the reason I use backslash to escape it.

Any one can help me out? Thx

sorry am I making confusing here? I want Sth like book(1).pdf instead of book1.pdf .[1-9]. is not workable?

Upvotes: 2

Views: 401

Answers (2)

anubhava
anubhava

Reputation: 784968

You don't even need find for this. Just use glob expression to match ([1-9) pattern:

printf "%s\n" *\([1-9]\)*

Note that this will list all the files and sub-directories in current path with name ([1-9]).

If you only want to list files with this pattern then use:

find . -maxdepth 1 -name '*([1-9])*' -type f

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

You need to use negated char class.

find . -regex '.*/[^/]*\([1-9]\)[^/]*$'

Upvotes: 0

Related Questions