Finding Files from the Command Line

Oct 29, 2012 - 3 Comments

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Mac OS X, Tips & Tricks

3 Comments

» Comments RSS Feed

  1. Parker says:

    I love learning about the command line, I don’t know much about but it’s sure nice to have walkthroughs like this. Keep them coming!

  2. Perry says:

    I often plug grep into a find when I’m looking for a file that has something specific inside. For example,

    find / -name ‘*.*’ -exec grep -il “foobar” {} \;

    will list all files that have the string ‘foobar’ inside (case insensitively). The -exec flag lets you run arbitrary commands on the output of find.

    Perry
    KidPub.com

  3. Cara says:

    > For example, you can locate anything within the user home directory containing “screen” in it’s name with the following:

    > find ~ -iname “screen*”

    (1) Grammar police alert: “in it’s name” —> “in its name”

    (2) In that example, it will only find files *starting with* “screen”, not files *containing* “screen”

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates