Command: mdfind
What it does: This command allows you to search for files, folders, or metadata across your entire MacOS system.
Example:
Let’s say you want to find all PDF files on your machine that contain the word “example” in their name. You can use the mdfind
command:
- Open Terminal on your MacOS machine.
- Type in the following command:
mdfind 'kMDItemKind == "pdf" AND kMDItemFSName CONTAINS "example"'
- Press Enter to run the command.
Result: The mdfind
command will search across your entire system, including disk drives, network shares, and other volumes, and
display a list of PDF files that match the specified criteria (in this case, having the word “example” in their name).
Tips:
- To search for files by content instead of metadata, use the
kMDItemTextContent
attribute instead ofkMDItemFSName
. - To search for files recursively within a specific directory or volume, use the
-onlyThisScope
option. For example:mdfind -onlyThisScope "kMDItemKind == \"pdf\" AND kMDItemFSName CONTAINS 'example'" /Users/yourusername/Documents
- To get more information about a specific file, use the
-il
(inline) option to pipe the output to theopen
command. For example:mdfind "kMDItemKind == \"pdf\" AND kMDItemFSName CONTAINS 'example'" | xargs -I {} open {}
Note: Be aware that this command can take some time to run, depending on the size of your system and the search criteria.
This trick uses the mdfind
command in combination with various metadata attributes (such as kMDItemKind
, kMDItemFSName
, and
kMDItemTextContent
) to perform advanced searches across your MacOS system.