Using mdfind Command to Search and Locate Files on macOS (with examples)
- Osx
- November 5, 2023
Introduction
mdfind is a powerful command-line tool available on macOS that allows users to locate files based on their names or content. This tool leverages the Spotlight indexing system, which makes the search process speedy and efficient. In this article, we will explore three different use cases of the mdfind command with code examples.
1: Finding a File by Name
The first use case demonstrates how to find a file by its name. This is particularly useful when you know the filename but are not sure of its location. The following code shows how to use the mdfind command to achieve this:
mdfind -name "filename"
Motivation: Suppose you have several files named “report.docx” scattered throughout your macOS system, and you need to quickly find all of them. Instead of manually searching for each file, you can use the mdfind command to locate all the files named “report.docx” in no time.
Explanation: The -name
option specifies that we want to search for a file based on its name. The subsequent argument, “filename,” should be replaced with the actual filename or a pattern to match a group of files.
Example Output:
/Users/username/Documents/report.docx
/Users/username/Desktop/reports/report.docx
2: Finding a File by Content
Sometimes, you may need to search for a specific file based on its content rather than its name. The mdfind command allows you to do precisely that. Here’s an example of how to use mdfind to search for files containing a given query:
mdfind "query"
Motivation: Imagine you worked on a project where you prepared several text documents, and now you need to find all the files containing the word “important” within their content. Instead of manually opening each file and performing a manual search, you can use the mdfind command to quickly locate all the relevant files.
Explanation: With mdfind, we don’t need to specify any additional options to search for a file’s content. We can directly provide the search query as an argument to the command. The search query can be a single word or even a phrase.
Example Output:
/Users/username/Documents/report.txt
/Users/username/Documents/important_notes.txt
3: Finding a File Containing a String in a Given Directory
The mdfind command is capable of searching for files containing a specific string within a particular directory. The following code illustrates how to use the -onlyin
option with mdfind to search in a specific directory:
mdfind -onlyin directory "query"
Motivation: Suppose you want to search for files that contain the word “error” within a specific folder called “logs.” By using the mdfind command with the appropriate options, you can quickly locate all the files matching your search criteria.
Explanation: The -onlyin
option tells mdfind to restrict the search to the specified directory (“directory” in the example). This ensures that the search is performed only within the specified directory and its subdirectories. The subsequent argument, “query,” should be replaced with the search string you want to find within the files.
Example Output:
/Users/username/Documents/logs/error_logs.txt
/Users/username/Desktop/files/logs/errors/error_2021.txt
Conclusion
The mdfind command is a powerful tool that allows macOS users to search for files based on their names or content. By understanding and utilizing the various options provided by mdfind, users can efficiently locate specific files and save valuable time. In this article, we explored three different use cases of the mdfind command, emphasizing the code examples, motivations, explanations, and example outputs for each use case. With mdfind, finding files on macOS becomes hassle-free and convenient.