THZ
THZ

Reputation: 143

Find files starting with a specific letter using find utility with -regex option

Was trying around to use the regex option on find command, without any result.

Considering this example directory "dir" that contains:

file1.txt 

file2.txt
Linux.txt
LInux.txt

So, I want for example, find all the files in dir that start with "L", and do:

find  dir -type f -regex ".*/^L"

Although this does not produce any output. What's wrong with this?

Upvotes: 1

Views: 1668

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

The .*/^L pattern is an example of a regex that will never match any string. ^ matches start of a string position, and after a / there can't be any start of string.

The find regex pattern should match the whole string, it is anchored by default. So, the pattern you need is

.*/L[^/]*

It is equivalent to ^.*/L[^/]*$: matches start of string, then any 0+ chars, up to the last / followed with 0 or more characters other than / up to the end of string. You can't use .* after L because it could match directories that start with L as . matches any char, and the [^/] negated bracket expression matches all chars but / and thus can't match /, directory separators.

Upvotes: 1

Related Questions