Ads 468x60px

Finding Files from the Command Line


Search for files from the command line
Searching the file system for a specific document or file is easy and very fast. Though most users are best served using Spotlight from the menubar, there are times when either Spotlight isn’t working, you need more paramaters, you’re working in the Terminal, or

Searching For Files with find

The find command is very fast and easy to use, it’s straight from the unix world and as such works in Linux as well as OS X. If you’re looking to learn things that are consistent across platforms, find is a good choice.
At it’s most basic, find can be used like so:
find path parameters
For example, you can locate anything within the user home directory containing “screen” in it’s name with the following:
find ~ -iname "screen*"
You may want to pipe the results to more if you’re expecting a large return, like so:
find ~ -iname "screen*" | more
Of course you can also find specific files that are buried somewhere in a directory. For example, looking in the user library folder for a specific plist file:
find ~/Library/ -iname "com.apple.syncedpreferences.plist"
You will need to prefix find with ‘sudo’ to search root directories and outside of the current user privileges. find also supports expressions, allowing you to search for very specific matches, wildcards, sequences, and other advanced options.
find is very powerful but it’s power quickly brings with it some complexity, as a result the classic mdfind command is probably better for more novice command line users.

Searching the Command Line with mdfind

mdfind is Spotlight’s terminal interface, meaning it won’t work if Spotlight itself is disabled, not functioning for another reason, or rebuilding it’s index. Assuming Spotlight is working as intended though, mdfind is very fast, efficient, and a bit more user friendly.
At it’s most basic level, mdfind is used as follows:
mdfind -name FileName
For example, to find all appearances of “Photo 1.PNG” the command would be:
mdfind -name "Photo 1.PNG"
Because mdfind is like Spotlight, it can also be used to search the content of files and folders for a specific file. To find all documents containing someones name could be done as follows:
mdfind "Will Pearson"
Much like the find command, sending results to more may be useful when sorting through a lot of files, like so:
mdfind "Sent from my" | more
mdfind can also be limited to specific directories with the -onlyin flag:
mdfind -onlyin ~/Library plist
Finally, there is also the ‘locate’ command, which is also very powerful and can be tied to find, but it has to be enabled separately.