6

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)?

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

2 Answers 2

9

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.

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

The following work with BSD find on OS X.

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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