macOS – How to search files with terminal

Are you tired of using Finder to search for files on your Mac? Did you know you can use Terminal commands to search for files quickly and easily? Here are a few commands you can use to search for files on your Mac using Terminal:

Use Find command

The “find” command is one of the most powerful for finding files. To use it, open Terminal and type “find” followed by the directory you want to search in and the search criteria.

– Search all files with the “.txt” extension in the Documents folder

find ~/Documents -iname "*.txt"

– Search for files containing “project123” in their name in the user folder

find ~ -iname "*project123"

– Search for files containing “project123” in their name in the user folder and export the results to a file on the desktop

find ~/Documents -iname "*project123*" > Desktop/find.txt

Use Locate command

The “locate” command is another powerful command for finding files quickly. It works by searching a database of file names on your Mac, so it’s much faster than the “find” command. To use it, simply type “locate” followed by the search criteria. Note for the first time using locate, you need to run this command to initialise the database.

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

– Search all files with the word “report” in the file name.

locate report

Use mdfind command

The “mdfind” command is similar to “locate”, but it searches the metadata of files instead of just the file names. This makes it useful for finding files based on their contents, tags, and other metadata. To use it, simply type “mdfind” followed by the search criteria.

– Search all files with the word “report” in the file name.

mdfind -name "report"

– Search all PDF files with the word “invoice” in the contents.

mdfind -name '*.pdf' -onlyin ~/Documents 'kMDItemTextContent=invoice'

– Search the above files and export the results to a file on the desktop

mdfind -name '*.pdf' -onlyin ~/Documents 'kMDItemTextContent=invoice' > Desktop/find.txt

These are just a few examples of the powerful Terminal commands you can use to search for files on your Mac. With a little practice, you can become a master of Terminal and save yourself a lot of time and frustration when searching for files.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top