O'Reilly logo

Learning Unix for OS X, 2nd Edition 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

Chapter 5. Finding Files and Information

One of the fundamental challenges of using modern computers is finding files and information with ever-larger storage systems. Whether you’re highly organized and use wonderfully mnemonic names for every file and directory you create or you have lots of letter1, letter2, and work files and directories scattered around your filesystem, there will undoubtedly come a time when you need to find something on your computer based on its contents, filename, or some other attribute.

It turns out that there are four different ways in Unix to search for—and hopefully find—what you seek. To look inside files, you need to use the grep command, introduced briefly in the previous chapter. To find files by filename, the fastest solution is the locate command. A more sophisticated filename and attribute search can be done with the Unix power user’s find command. And finally, OS X includes a search system called Spotlight that has a powerful command-line component that’s worth exploring.

Searching Inside Files with the grep Command

The grep program searches the contents of files for lines that match the specified pattern. The syntax is:

grep pattern [file(s)]

The simplest use of grep is to search for lines within files that contain a particular word by feeding grep a pattern and a list of files in which to search. For example, let’s search all the files in the working directory (using the wildcard *) for the word “Unix”:

$ grep "Unix" * ch01:Unix is a ...

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