How to correctly use find with regular expression?
Note that -regex
/-iregex
(GNU extensions), like the standard -path
match on the full path, not just the file name.
If you want to find files whose name (as opposed to path) starts with 2 decimal digits, following by something that is not a decimal digit and ending in .flac
, you could do portably:
find . -name '[0-9][0-9][!0-9]*.[fF][lL][aA][cC]'
Note that it wouldn't match on 01.flac
because the [!0-9]
can't match there. Instead you could write it:
find . -name '[0-9][0-9][!0-9]*' -name '*.[fF][lL][aA][cC]'
Those use wildcard patterns, not regexps.
To use GNU find
's -iregex
, the equivalents would be:
find . -regextype egrep -iregex '.*/[0-9]{2}[^0-9/][^/]*\.flac'
find . -regextype egrep -iregex '.*/[0-9]{2}([^0-9/][^/]*)?\.flac'
That is, we anchor the two digits at the start, as we make sure the rest of the regexp doesn't span a /
.
Related videos on Youtube
Daniele
Updated on September 18, 2022Comments
-
Daniele almost 2 years
I cannot figure out the correct use of find with
-regex
option:For example, given this example:
Morcheeba/Big Calm/02 Shoulder Holster.flac Morcheeba/Big Calm/02. Shoulder Holster.flac
I only want to match the string in the form
path/to/file/## filename.ext
, without the dot.I've tried with:
find Morcheeba/ -regextype egrep -iregex ".*[0-9]{2}*[a-z]*flac$"
and some variation, but I always get all files either none.
What's wrong?
Thanks in advance.
-
jamesbtate about 7 yearsI think you are mixing
.
and*
. In your example, the second*
could be a space and the third*
should be a.
or\.
. You could use[^\.]
to match any character that is not a period. Put a*
after it to match 0 or more non-period characters. -
RomanPerekhrest about 7 yearscan't reproduce, seems to work (even with that strange pattern)
-
-
Stéphane Chazelas about 7 yearsThat would match on
./path/to/foo123.flac
or./foo/1234/whatever.flac
though. -
Daniele about 7 yearsYes, I had to fix it a bit.