3

I am trying to port a script from a Ubuntu Linux box to a macOS laptop and I'm struggling to get my find with a regex setup correctly.

In a directory, I have the following files.

/Users/kevin/Documents/Audio/arn/2400/Report2400.clip.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip1.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip2.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip3.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip4.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip5.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip6.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip7.mp3
/Users/kevin/Documents/Audio/arn/2400/Report2400.clip8.mp3

running this command:

find . -regex 'Report2400\.clip[0-9]+\.mp3'

I'm expecting to get 8 files, the 8 with a number after "clip", but nothing is returned.

What am I missing? I know it's basic, but I'm close to checking myself into a mental hospital.

New contributor
Kevin Davis is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • it's better to install GNU tools, they're much more functional. If you use brew they'll be prefixed with g like gfind, ggrep... and you can use an alias to replace the BSD tools
    – phuclv
    Commented 8 hours ago

1 Answer 1

3

I believe this should work:

find -E . -regex '.*/Report2400\.clip[0-9]+\.mp3'

According to man find, you -E to get extended regular expressions:

 -E      Interpret regular expressions followed by -regex and -iregex pri-
         maries as extended (modern) regular expressions rather than basic
         regular expressions (BRE's).  The re_format(7) manual page fully
         describes both formats.

Also, according to man find, you need .*/ to include the whole path:

 -regex pattern
         True if the whole path of the file matches pattern using regular
         expression.  To match a file named ``./foo/xyzzy'', you can use
         the regular expression ``.*/[xyz]*'' or ``.*/foo/.*'', but not
         ``xyzzy'' or ``/foo/''.
1
  • 1
    This did the trick! Thanks so much. Sounds like I need to educate myself on the differences in extended and basic regular expressions. I had played around with using the -E argument, but I was still missing the mark because of the full path being checked, not just the filename. Commented 18 hours ago

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .