19 Feb 2025
Ways to Find Files in macOS Terminal
To find files in macOS Terminal, you can use several main commands:
find- This command is used to search for files and directories in a directory hierarchy based on a specified expression.- Example:
find /path/to/directory -name "filename"
Use find to search for files or directories by name, type, size, etc.
locate- Utilizes a pre-built database to quickly find files. The database needs to be updated periodically usingsudo /usr/libexec/locate.updatedb.-
Example:
locate filename -
mdfind- This command uses macOS's Spotlight indexing to search for files = fastest. -
Example:
mdfind "search term" -
grep- While primarily used to search within files, it can be combined with other commands to search for files containing specific text. - Example:
grep -r "search text" /path/to/directory
grep stands for Global Regular Expression Print.
It is a command-line tool used in Unix-based systems (including macOS) to search for specific text patterns within files.
It searches for lines matching a pattern in files.
Use grep to search inside files for text.
ls- Basic command to list files in directories, can be combined with other commands or options to refine search.- Example:
ls -R | grep "filename"
Each of these commands can be modified with various options to refine searches, such as filtering by file type or modifying search depth.
| Goal | Best Tool | Why |
|---|---|---|
| By file content | grep |
Searches inside files for matching text |
| By file name (fast) | mdfind |
Uses macOS Spotlight index = very fast |
| By file name (portable) | find |
Works on all Unix systems, doesn't rely on index |
| By file name (if indexed) | locate |
Super fast, but needs sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist to enable DB |
| Just listing files | ls |
Only shows what's in the directory, no search at all |
Commands
find
- Basic Syntax:
-
find [path] [expression]- Searches for files and directories starting from the specified[path]and matches them against the given[expression]. -
Common Flags:
-name [name]: Searches for files by name.- Example:
find / -name "example.txt"
- Example:
-type [type]: Searches for a type of file.[type]can beffor files ordfor directories.- Example:
find /path/to/directory -type f
- Example:
-mtime [n]: Finds files modifiedndays ago.- Example:
find . -mtime -7(modified in the last 7 days)
- Example:
-size [n]: Finds files of a specific size.[n]can be in kilobytes (k), megabytes (M), etc.- Example:
find / -size +10M
- Example:
-
-exec [command] {} \;: Executes a command on each matching file.- Example:
find . -name "*.txt" -exec grep "searchtext" {} \;
- Example:
-
Logical Operators:
-and: Combines expressions (can be omitted, as it's the default).-or: If either expression is true, the file is a match.-
!: Negates an expression. -
Examples:
- Search for files owned by a specific user:
find / -user [username] - Find empty files:
find /path/to/directory -type f -empty - List files created within the last 24 hours:
find / -ctime -1
locate
The locate command in macOS is used to quickly find files by searching through a pre-built database. Here are the basic aspects and options associated with the command:
- Basic Usage:
-
locate [filename]: Searches for files matching the given[filename]within the system database. -
Update the Database:
-
sudo /usr/libexec/locate.updatedb: Updates the database used by thelocatecommand. This command may require superuser privileges. -
Options:
-i: Ignore case distinctions in patterns and data.- Example:
locate -i "example.txt"
- Example:
-d [path-to-database]: Specifies an alternative database forlocateto use.- Example:
locate -d /my/other/database "search_term"
- Example:
-r [expression]: Interprets the search term as a regular expression.- Example:
locate -r 'my.*file'
- Example:
The locate command is generally faster than find because it relies on an indexed database of system paths, which must be updated manually or may be scheduled to update periodically. This speed comes at the cost of potentially having less up-to-date results compared to real-time search methods.
mdfind
The mdfind command in macOS uses the Spotlight index to search for files. Here are the essential options and usage examples:
- Basic Usage:
-
mdfind "search text": Searches for files that match the specified query using Spotlight's index. -
Search by File Type:
-
mdfind "kMDItemContentType == 'public.text'": This example searches for all text files. You can replace'public.text'with the relevant content type for other file types. -
Search in Specified Directory:
-
mdfind -onlyin /path/to/directory "search text": Limits the search to a specific directory. -
Refining the Search:
-
mdfind "kind:image" && mdfind "date:today": You can use Spotlight attributes likekind,date, etc., for more refined searches. -
Search by Metadata:
-
mdfind -name "example.txt": Searches specifically by file name. -
Exclude System Files:
mdfind -0 "search text": Searches without descending into system files, useful for excluding hidden or system-specific files.
The mdfind command is very powerful when you need to perform quick searches across the entire system based on indexed metadata. It benefits users looking for a more efficient search than some of the more extensive real-time searches like find.
grep
The grep command is used for searching text within files in macOS Terminal. Here are the basic options and usage examples:
- Basic Usage:
grep "pattern" [file]: Searches for the specified "pattern" within the provided [file].-
Example:
grep "error" logfile.txt -
Recursive Search:
grep -r "pattern" [directory]: Searches recursively through all files in the specified directory.-
Example:
grep -r "TODO" ./project -
Ignore Case Sensitivity:
grep -i "pattern" [file]: Ignores case distinctions when searching.-
Example:
grep -i "warning" logfile.txt -
Display Line Numbers:
grep -n "pattern" [file]: Displays the line number along with each match.-
Example:
grep -n "error" logfile.txt -
Count Matches:
grep -c "pattern" [file]: Counts the number of lines that match the pattern.-
Example:
grep -c "error" logfile.txt -
Show only Matching Part:
grep -o "pattern" [file]: Prints only the matching part from each line.- Example:
grep -o "http://[a-zA-Z0-9./]*" logfile.txt
These options make grep a versatile tool for processing and extracting information from text files in a variety of contexts.
ls
The ls command in macOS Terminal is used to list directory contents. Here are the basic options and commands:
- Basic Listing:
-
ls: Lists files and directories in the current working directory. -
List with Details:
-
ls -l: Provides a detailed listing, showing file permissions, number of links, owner, group, size, and modification date. -
Include Hidden Files:
-
ls -a: Shows all files, including hidden ones (those starting with a dot.). -
List with Human-Readable Sizes:
-
ls -lh: Displays sizes in a more readable format such as KB, MB, rather than bytes. -
Sort by Modification Time:
-
ls -lt: Sorts files by modification time, most recently modified first. -
Recursive Listing:
-
ls -R: Lists all files and directories with their subdirectories recursively. -
Combining Options:
- You can combine multiple options. For example,
ls -lawill show detailed information including hidden files.
These options allow you to customize the display of directory and file listings according to your needs.
Faster tools
20 Sep 2026 the commands above work on any Unix box and are worth keeping for that reason. Everything below is faster, and most of it is already installed here.
Already on this Mac: fd, rg, fzf, zoxide, yazi, plus the built-in mdfind. That set (fd, ripgrep, fzf, zoxide, yazi) is also what the terminal crowd on X has converged on.
| Tool | Replaces | What it does |
|---|---|---|
fd |
find |
Filename search, skips hidden and ignored files by default |
rg |
grep -r |
Content search, parallel, .gitignore aware |
fzf |
nothing | Fuzzy filter for any list, pipes into anything |
zoxide |
cd |
Jumps to directories by how often you use them |
yazi |
browsing by ls |
File manager with previews, calls fd and rg inside |
mdfind |
find on macOS |
Spotlight index, whole disk, instant |
Measured here
30 Nov 2026 re-run: the same kinds of search, each run once to warm the cache, then timed 3 times. The chart shows the median.
| Search | Corpus | Command | Time | Found |
|---|---|---|---|---|
| Content | 99,893 files in ~/sites |
grep -ril |
33.64s | 89 |
| Content | Same | rg -li -uu |
10.77s | 89 |
| Content | Same | rg -li |
8.72s | 89 |
| Content | Same | mdfind -onlyin ~/sites |
0.19s | 80 |
| Content | Whole home directory | mdfind -onlyin ~ |
5.14s | 201,655 |
| Filename | 879,644 files and folders in ~/ai |
find -iname |
26.91s | 124 |
| Filename | Same | fd -HI |
9.46s | 124 |
| Filename | Same | mdfind -onlyin ~/ai -name |
0.87s | 15 |
| Filename | Same | fd |
0.39s | 4 |
| Filename | Whole home directory | mdfind -onlyin ~ -name |
1.04s | 1,799 |
What the numbers say:
- The index wins, but it isn't complete. Spotlight answered the content search 177 times faster than
grep. It also missed 12 files thatrgfound: 8 pages the site generator rewrote the day before, 3 HTML pages from April 2025 and 1 JSON file written that morning. And 3 of its 80 hits were folders with "kaltura" in the name, becausemdfindmatches names as well as text. A gap in the index looks exactly like "no match". - ripgrep's lead is the engine, not the skipping.
rg -uureads hidden and ignored files too, the same setgrepreads, and it is still 3 times faster. - fd's lead is mostly the skipping. Plain
fdtook 0.39s and found 4 files. With-HIit found all 124 in 9.46s, still 3 times faster thanfind. Most of~/aiis hidden or.gitignored: virtual environments,node_modules, caches. - A broad
mdfindpays for its output. The whole home directory took 5.14s for "kaltura", because it printed 201,655 paths. Scope it with-onlyin.
20 Sep 2026 first run, for reference: grep -ril 32.08s and rg -li 6.64s on ~/sites, fd 0.42s and fd -HI 1.34s on what was then 149,863 files in ~/ai.
Recipes
# filename, current directory down
fd invoice
# include hidden files and ignored paths
fd -HI invoice
# by extension, then run something on each hit
fd -e md -x wc -l
# content, case-insensitive
rg -i kaltura
# just the file names, not the matching lines
rg -l kaltura
# restrict to a language
rg -t py argparse
# pick a file interactively, then open it
fd -t f | fzf | xargs open
# Spotlight: content, anywhere, instant
mdfind -onlyin ~ "gap selling"
# Spotlight: files over 1GB
mdfind 'kMDItemFSSize > 1073741824' -onlyin ~
# Spotlight: filename only
mdfind -onlyin ~ -name 260909-digitalx
⚠️ WARNING:
mdfindonly knows what Spotlight indexed. External drives, network shares and privacy-excluded folders return nothing, which looks exactly like "no match". Check the volume first withmdutil -s /Volumes/NAME, and fall back tofdorrgwhen it is not indexed.
Worth knowing, not installed
| Tool | Why look |
|---|---|
| tgrep | Trigram-indexed grep from Microsoft, aimed at repos too big for rg |
| television | Fuzzy finder with ready-made channels: files, text, git, env |
| broot | Tree navigator that folds a huge directory onto one screen |
| ugrep | grep-compatible flags, searches inside archives and PDFs |
| skim | fzf, rewritten in Rust |
| fselect | SQL over the filesystem: select name from . where size gt 10mb |
| ast-grep | Searches code by syntax tree instead of text |
The indexed tools are the interesting direction. On a Mac the index already exists and is free, so mdfind covers most of that ground.
30 Nov 2026 each one in more detail. Star counts and install commands as of today.
tgrep
What it is: grep with an index, from Microsoft (MIT licence, 3,500 stars). It splits every file into trigrams, 3-character chunks, and stores which files hold which trigram. A search first asks the index which files could match, then runs the regex only on those. It already powers search in GitHub Copilot CLI.
The claim: in Microsoft's August 2026 benchmark it beat ripgrep in 17 of 18 repo and platform combinations, from 0.93x to 51.9x. That is search time with the index built and a server running. Building the index isn't counted.
# no Homebrew formula: build from source with Rust
git clone https://github.com/microsoft/tgrep.git && cd tgrep
cargo install --path tgrep-cli --locked
tgrep serve . # build the index, keep it fresh in the background
tgrep -- "fn main" . # search; uses the server when it runs
tgrep status . # indexing and refresh status
tgrep index . --exclude vendor # one-off index without a server, in .tgrep/
tgrep --no-index -- "TODO" . # skip the index when results must be current
The catch: the same one as mdfind. The index can lag the files, and a pattern with no useful trigrams still scans everything. Worth it on a monorepo searched 100s of times a day. Not for my scripts folders, where rg already answers in seconds.
television
What it is: a fuzzy finder, like fzf, that ships its own data sources. The command is tv (MIT, 6,500 stars). fzf filters whatever you pipe into it. tv has channels: files, file contents, git repos, environment variables, Docker containers, each with its own preview.

brew install television
tv # files in the current directory
tv text # file contents, with a preview of the matching line
tv git-repos # every git repo on the disk, previewing its log
tv env # environment variables
# Ctrl+T for files and Ctrl+R for history, in place of fzf's bindings
eval "$(tv init zsh)"
A channel is a small TOML file, so a new source takes 10 lines:
# ~/.config/television/cable/notes.toml
[metadata]
name = "notes"
description = "My notes"
[source]
command = "fd -e md . ~/notes/site/content/articles"
[preview]
command = "bat --color=always '{}'"
Versus fzf: the same job, less glue. If your .zshrc already holds 5 fzf functions with --preview, you have built television by hand.
broot
What it is: a tree view that fits any directory on 1 screen (MIT, 13,000 stars). Where tree prints pages, broot shows the first few entries of each folder and folds the rest into "51 unlisted". Type a few letters and the tree narrows to the matches while keeping their parents in view.

brew install broot
broot --install # adds the br shell function, so broot can cd for you
br -s # tree with sizes
br -sdp # sizes, dates and permissions
Inside broot:
| Type | Does |
|---|---|
invo |
Fuzzy match on names |
/inv.*26 |
Regex on names |
c/kaltura |
Search inside files |
Alt+Enter |
Quit and cd to the selected folder |
Alt+H |
Toggle hidden files |
Alt+I |
Toggle .gitignored files |

Versus yazi: yazi is the file manager I use, with image previews, see Terminal File Managers. broot is better at 1 job: seeing the shape of a big unknown folder and jumping into it.
ugrep
What it is: a drop-in replacement for grep (BSD licence, 3,300 stars). Same flags, so scripts keep working, plus what grep never had: an interactive search screen, fuzzy matching, and search inside archives, PDFs and Office files. Its authors say it is usually faster than ripgrep, on their own benchmarks.

brew install ugrep
ug -Q # interactive: results update as you type
ug -z "invoice" ~/Downloads # inside zip, tar and gz files too
ug+ "Kündigung" ~/Documents # inside PDFs and Office files, through filters
ug -Z2 "kaltrua" # fuzzy: up to 2 typos still match "kaltura"
ug -l "kaltura" ~/sites # same -l as grep
ug+ hands each PDF to pdftotext (from brew install poppler) before it searches. That is the gap rg leaves open: text locked inside documents. On this Mac, Spotlight already indexes PDF text, so mdfind covers the common case. ugrep is for folders Spotlight doesn't see, such as a NAS share.
skim
What it is: fzf rewritten in Rust, with the command sk (MIT, 7,000 stars). Most fzf flags carry over, and the shell bindings are the same, Ctrl+T and Ctrl+R.

brew install sk
fd -t f | sk --preview 'bat --color=always {}'
# interactive mode: every keystroke re-runs rg with the query
sk --ansi -i -c 'rg --color=always --line-number {q}'
The difference: interactive mode, where the query is fed to a command instead of filtering a fixed list. fzf does the same today with --disabled and a reload binding. No reason to switch if fzf is already set up, which it is here.
fselect
What it is: file search written as SQL (Apache 2.0, 4,500 stars). Not real SQL, a relaxed version: columns, from, where, order by, limit, even group by. It also reads what find can't: image size, EXIF, audio tags, MIME type.
brew install fselect
# the 10 biggest files under Downloads
fselect size, path from ~/Downloads order by size desc limit 10
# photos at least 4,000 pixels wide
fselect width, height, path from ~/Pictures where width gte 4000
# PDFs changed this week, as JSON
fselect path, modified from ~/Documents where ext = pdf and modified gt '2026-11-23' into json
# how much space each file type uses
fselect "ext, count(*), sum(size) from ~/Downloads group by ext"
Readable 6 months later, unlike find ~ -type f -size +100M -mtime -7 -exec ls -lh {} \;. Slow on a big tree, because it walks the disk like find.
ast-grep
What it is: search and replace on code structure, not text (MIT, 16,800 stars). The command is ast-grep, or sg. You write the pattern as code, with $A standing for any expression. It parses the files, so it ignores formatting, comments and strings that only look like code.

brew install ast-grep
# every call to print() with exactly 1 argument
ast-grep -p 'print($A)' -l py
# every requests.get without a timeout
ast-grep -p 'requests.get($URL)' -l py ~/ai
# rewrite: open() without encoding gets encoding="utf-8"
ast-grep -p 'open($F, "r")' -r 'open($F, "r", encoding="utf-8")' -l py -i
rg "requests.get" finds the calls with a timeout too. ast-grep finds only the ones without, because requests.get($URL) matches 1 argument and no more. It's a refactoring tool more than a search tool, and the one I'd install first from this list.
Which one
- Inside a project:
rgfor text,fdfor names. They respect.gitignore, so results stay clean. - Anywhere on the Mac:
mdfind. Nothing beats a warm index. - Don't know the name:
fd -t f | fzf, or openyaziand browse with previews. - On a server over SSH:
findandgrep. They are always there, which is the whole point of keeping the first half of this note.