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, operating with a remote machine, or perhaps just would like to use an alternative search function.

You’re in luck, we’ll show you how to find files and folders directly from the command line using two different tricks. The first method is operating system agnostic, meaning you’ll be able to use the search function with Mac OS X, Linux, BSD, and many other unix variations, whereas the second trick for searching utilizes a Mac-only model of finding files from the command line. Let’s read on and learn how to use these great abilities.

Searching For Files from Command Line 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 Mac 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 for Files at 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.

Know of any other great search tricks or file location methods for the command line? Share with us in the comments.

.

Related articles:

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

3 Comments

» Comments RSS Feed

  1. 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”

  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. 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!

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site