How to use locate and grep to find files on a Linux (or OS X) system

Become An Insider

Sign up now and get FREE access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content. Learn more.

Every year, hard drives get bigger and cheaper, a nice combination. With all the technical toys we have in our lives now, we certainly have plenty of stuff to fill those hard drives.

computer files stock

Every digital pack rat has to pay the price, however, and that too often tends to be an inability to find the files he wants. It can be hard to find that document you wrote somewhere in a folder with 600 other documents. Fortunately, Linux has powerful tools at your disposal that can help you quickly and efficiently retrieve a necessary file. And since both OS X has a command-line shell that runs many basic commands also found in Linux, most of these should also work on a Mac.

In this free PDF download excerpted from The Linux Phrasebook, Second Edition by Scott Granneman, you'll learn how to use the locate command to discover where a file resides on your system. This command works well if you know the name of a file, or even part of the name. The excerpt goes over various options for the command, as well as how to update the database used by locate.

You'll also learn how to use grep if you want to find files that contain a certain word or phrase. This including tricks like showing lines before and after your search results to provide some context, displaying the line numbers where your search term appears in files and highlighting search terms in your results to make it easier to spot them within a block of text. There's also an option to count the number of occurrences of a search term within files.

Editor's note: This excerpt is posted with permission of author Scot Granneman and publisher Pearson/Addison-Wesley Professional, Copyright 2016, all rights reserved.

To continue reading this article register now

Shop Tech Products at Amazon
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.