Find Lost Files on Your Mac Using Terminal

Ever saved a file somewhere on your Mac and then completely forgotten where you put it? Before you start opening every folder in Finder, there's a rather clever Terminal command worth knowing.

We've all done it.

You create a document, download a PDF or save something important, fully intending to organise it later. A few weeks pass, and suddenly you're trying to remember whether you put it in Documents, Downloads, Desktop or that mysterious folder called "Stuff" that really should have been sorted out years ago.

Of course, macOS already has Spotlight, which is brilliant for searching your Mac. But what you might not know is that you can access the same search technology directly from Terminal.

And it's surprisingly useful.

Meet mdfind

The command is called mdfind, and it uses the Spotlight index to search for files, folders and indexed information on your Mac.

Unlike some traditional Terminal search commands, it doesn't need to work its way through every directory looking at files individually. Instead, it asks Spotlight for information that's already been indexed.

Let's give it a try.

Open Terminal from Applications > Utilities and enter:

mdfind -name "invoice"

Press Return, and your Mac will display the paths of indexed files whose names match the word "invoice".

Rather than just showing the filename, Terminal tells you exactly where each matching file lives.

That's particularly useful when you know what a document is called but haven't the faintest idea where you saved it.

Search for words inside documents

Here's where things become even more interesting.

Let's say you remember writing something about a project proposal, but you can't remember what the document was called.

Try:

mdfind "project proposal"

This searches Spotlight's indexed information, which can include the contents of compatible documents, not just their filenames.

It's a useful trick when hunting down old notes, PDFs or documents containing particular words.

One thing to remember is that a normal text query isn't necessarily an exact-phrase search, so you may see results containing related or matching terms.

Narrow down your search

Sometimes searching your entire Mac produces rather too many results.

Fortunately, you can tell mdfind to concentrate on a particular location.

For example:

mdfind -onlyin ~/Documents -name "report"

This searches for filenames matching "report" within your Documents folder.

You could change ~/Documents to another folder if you wanted to search somewhere else.

It's particularly handy when you've got a large collection of project files and want to avoid searching the rest of your Mac.

Want to know how many files match?

There's another handy option:

mdfind -count -name "invoice"

Rather than displaying a long list of file paths, Terminal tells you how many matching results Spotlight has found.

And if you want to copy a list of results for later, you can even combine mdfind with our previous Quick Win:

mdfind -name "invoice" | pbcopy

Now you've got a list of matching file paths ready to paste into Notes or another application.

A little word of warning

Because mdfind relies on Spotlight, it can only find information that Spotlight has indexed. Files on excluded drives, folders outside the index or documents that Spotlight hasn't finished processing might not appear. So an empty result doesn't necessarily mean the file doesn't exist.

I love little connections like this. Spotlight is something most Mac owners already use, but discovering that you can access it through Terminal opens up a few more possibilities.

It's particularly useful when you're troubleshooting, managing projects or trying to find that one document you know you saved somewhere.

Next
Next

Convert Word Documents Without Microsoft Word