O'Reilly logo

Learning Unix for Mac OS X Tiger by Dave Taylor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Find to Explore Your Filesystem

Reading about the limitations of the locate command undoubtedly caused you to wonder if there was a more powerful option, a command that could let you search through the actual, live filesystem to find what you seek. The find command not only lets you search for files by filename patterns, but a remarkable number of additional criteria, too.

find has a completely different syntax than any of the Unix commands examined to this point in the book, so the best place to start is with the find command syntax itself.

find flags pathname expression

Expressions are where the complexity shows up, because a typical expression is a "primary" followed by a relevant value, and there are dozens of different primaries that can be combined in thousands of different ways. An example is to match files that end with .html, you would use something like:

find -name "*.html"

To search for all HTML files on a Tiger system, here's how the command would look:

$ find . -name "*.html" -print
./Library/Preferences/Explorer/Favorites.html
./Sites/index.html
$

Notice that the pathname specified is the current working directory (.). Change this to the root directory (/), and the find command traverses the entire filesystem looking for matches. Rather than list all the matches, however, I'm going to feed the output of the command to the ever-helpful wc word count command to just get a count of matching entries:

$ find / -name "*.html" -print | wc -l find: /.Metadata: Permission denied ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required