How to find files via the OS X Terminal

If you need to search for files in OS X, one option it is to use the OS X Terminal application and some of its services.

There several ways to find files in OS X, the most popular of which are GUI-based routines such as the Spotlight menu and the Finder search, both of which offer quick access to the system's metadata search index. However, there are other options for accessing this index and otherwise finding files you might be interested in locating.

Of course besides Apple's GUI options there are some third-party searching tools available; however, without these the other option in OS X is to use the Terminal, which can benefit both Terminal and GUI-based routines.

The first option in the Terminal is the classic "find" command that is common to many Unix systems, which will recursively walk a specified folder hierarchy and search items for a given name pattern. The find command has a number of options you can use to narrow down search results, and these can be looked up on its manual page, but the basics for finding a file are to specify the starting path and the name, such as the example here to locate a file called "test.txt" starting in the Users directory:

find command results in OS X
The find command will try to enter any folder in the specified path, which can result in "Permission denied" errors, but for files that your account has default access, this command should reveal them properly. Screenshot by Topher Kessler/CNET

find /Users -name "test.txt"

Since the find command walks through the filesystem hierarchy, it may take a long time to complete, and if you specify the root folder only (without using flags to prevent recursion), it may recurse through the /Volumes/Macintosh HD/ mount point directory repeatedly and never end the search.

Beyond the find command are two that offer indexed search results. The first of these is the "locate" command, which will build a database of system resources and then allow you to find them by simply typing the following command:

locate NAME

In this command, NAME can be any partial file name and the command will output the full path to any item that includes this name. The locate command does require a properly built "locate" database, which Apple blocks by default in OS X; however, you can enable the launch daemon that regularly builds and updates the locate database with the following command:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

locate command results in OS X
The locate command will show system files either by full or partial name. Screenshot by Topher Kessler/CNET

After this command is run, after a while the locate database will be assembled and be ready to use, but once it's completed you can use the locate command to search for numerous system files that you might know of by name. Do keep in mind that this command will only search for system files and will not index the user directories for finding personal files.

The final command is the "mdfind" command, which will locate files on your system that have been indexed for use with Spotlight. This command is arguably the more thorough of the two prior commands, as it will search both user and system files by default, and also offer options to search by file name and by file content, just like Spotlight searches.

To use this command, simply run it as follows; it will output a list of full paths to the files that include the search term:

mdfind command results in OS X
The mdfind command will locate files in your user directory, as well as those in the system. In this case, the same file was found as part of an developer application installation in the User directory, as well as in the system directory. Screenshot by Topher Kessler/CNET

mdfind NAME

As with all of these commands, the mdfind command has a number of additional options that can be implemented for limiting and customizing its search scope, which can be looked up in the mdfind manual page.

With these commands, you can easily output the full path to files you may be interested in locating, and then be able to access them either in the Finder or use the full path in subsequent Terminal commands. To reveal items in the finder, simply triple-click one of the paths to select it, and then right-click the selection and choose "Reveal in Finder" from the Services contextual submenu.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    HOT ON CNET

    Last minute back to school shopping?

    Whether you're looking for headphones to study with or music-streaming gear, CNET rounds up a shopping guide just for you.