I have a project directory that contains source code and subdirectories of source code. I want to use the Unix program find to search recursively for the names of files of certain extensions. The versions of find on Linux and Mac OS X behave differently.

# Works in Linux
find . -type f -regex ".*\.\(py\|html\)$"

# Neither of these works in Mac OS X
find . -type f -regex ".*\.\(py\|html\)$"
find . -type f -regex ".*\.(py|html)$"

How do I write this command so that it will run on Mac OS X (and hopefully on Linux too)?

  • It would be helpful if you told us in what way they behave differently. – Nifle Mar 10 '10 at 8:27
  • The command is supposed to find files. On OS X, the same command finds nothing (zero files). – hekevintran Mar 10 '10 at 8:29

Mac OS X uses BSD find and most Linux distributions come with GNU find. I believe you can install GNU findutils onto Mac OS X. I don't have a Mac handy, but I am sure it was available in MacPorts.

Looking at the BSD find man page I could make a wild guess and suggest you try looking at the -E option to enable the modern regular expression library.

  • 1
    Getting findutils from MacPorts did the trick. – hekevintran Mar 10 '10 at 9:02
  • Findutils is also available through Homebrew. The command after installing is gfind – Eric Hu Jun 19 '12 at 23:49

The following work with BSD find on OS X.

find -E . -type f -regex ".*\.(py|html)$"

find . -type f | grep -e ".*\.\(py\|html\)$"

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.