A project that grows quickly has hundreds of files, and you often need to find two things: where a file is, and which line mentions something. Picture a library. If you're looking for a book by the title on its cover, you use the catalogue. In the terminal, that catalogue is find, the command that searches by file name. If you're looking for the page that mentions one particular word, you have to open the books. In the terminal, the one that opens the books is grep, the command that searches the text inside files.
The examples below run in Budi's folder ~/web-project. It holds error.log, index.html, script.js, style.css, a logs folder holding app.log, and a src folder holding main.js and utils.js.
find: searching for files by name
budi@laptop:~/web-project$ find . -name "*.log"
./error.log
./logs/app.log
budi@laptop:~/web-project$ find . -name "*.js"
./script.js
./src/main.js
./src/utils.js
budi@laptop:~/web-project$ find . -type d
.
./logs
./src
find .starts in the current folder (.) and goes down into every subfolder (a folder inside a folder) in it.-name "*.log"filters by name."*.log"is a pattern: a text that file names are matched against.*means "any characters, any number of them", so*.logmatches every name ending in.log. Without*,-name "app"only matches a file called exactlyapp.-inameis like-name, but ignores upper and lower case.-type fonly looks for files, and-type donly looks for folders (directories).- The order
findprints in follows how things are stored on disk, so on your laptop the order may differ.
Why find's pattern needs quotes
This is the same command, but with the pattern written without quotes:
budi@laptop:~/web-project$ find . -name *.log
./error.log
Without quotes, app.log is missing from the result. The shell causes it, not find. Before the command runs, bash replaces *.log with the names that match in the current folder. ~/web-project holds only one match, error.log, so what reaches find is find . -name error.log. With two or more matching files, find gets several names and fails with an error. With no match, bash passes *.log on unchanged, but zsh (the default shell on macOS) refuses the command with zsh: no matches found: *.log. With quotes, the shell leaves *.log as it is, and find is the one matching it in every folder below.
grep: searching the text inside files
logs/app.log has three lines labelled ERROR, and two of them also contain the word error in lowercase. Before you look at the output, guess: how many lines does grep "error" logs/app.log print?
budi@laptop:~/web-project$ grep "error" logs/app.log
2026-10-08 09:02:03 ERROR database connection error
2026-10-08 09:06:45 ERROR email service error: connection refused
budi@laptop:~/web-project$ grep -i "error" logs/app.log
2026-10-08 09:02:03 ERROR database connection error
2026-10-08 09:04:02 ERROR payment timeout
2026-10-08 09:06:45 ERROR email service error: connection refused
grep "pattern" fileprints every line that contains the pattern (the text you search for), whole. On Ubuntu, the matching part is coloured red.- grep treats upper and lower case as different. The line
ERROR payment timeoutdoesn't contain a lowercaseerror, sogrep "error"prints only 2 lines, not 3. -i(ignore case) treats upper and lower case as the same, so all threeERRORlines are printed.
Flags you'll use often:
| Flag | Meaning | Example result |
|---|---|---|
-i |
Ignore upper and lower case | 3 lines, not 2 |
-n |
Show line numbers | 7:2026-10-08 09:02:03 ERROR ... |
-c |
Only count the matching lines | grep -c -i "error" logs/app.log prints 3 |
-v |
The opposite: lines that don't match | |
-w |
Match whole words only | -w "log" doesn't match login |
grep -r: searching every file with one command
In this example, Budi searches for TODO (a note in code for work that isn't finished):
budi@laptop:~/web-project$ grep -r "TODO" .
./script.js:// TODO: add the order button
./src/main.js:// TODO: fetch the cake list from the API
budi@laptop:~/web-project$ grep -rl "TODO" .
./script.js
./src/main.js
-r(recursive) goes into every file in the folder.(the current folder) and its subfolders. Because the results come from many files, each line starts with the file name and a colon.-lonly prints the names of files that contain the pattern, without the lines. This flag is handy for "which files still have a TODO?".- Without the
.,grep -r "TODO"also searches the current folder. On Ubuntu, it writes the file names without a leading./. On macOS, the file names still start with./. - In a real project,
grep -ralso goes intonode_modulesand.git.node_modulesholds the libraries (ready-made code written by other people) that npm installed, often thousands of files..gitis the folder where Git keeps its records. Your screen fills with results that are not your code.--exclude-dirskips the folders you name:grep -r --exclude-dir=node_modules --exclude-dir=.git "TODO" ..
💡 In brief: ripgrep
Many developers install rg (ripgrep), a search tool like grep -r but much faster on big projects. ripgrep also automatically skips files listed in .gitignore (the list of files Git ignores), such as node_modules. The search in VS Code uses ripgrep too. You write it in a similar way: rg "TODO", rg -i "error" logs.
Here are ripgrep and two other tools. This lesson still uses grep -r and find, because both are already on Linux and macOS without installing anything:
| Tool | When to pick it |
|---|---|
rg "TODO" |
Big projects. Faster, and it skips what is listed in .gitignore. |
git grep "TODO" |
Your project uses Git. It only searches files that Git tracks, so node_modules is left out. |
fd -e log |
A replacement for find when you search files by extension (the end of the name, such as .log): shorter, and it also skips what is listed in .gitignore. On Ubuntu, you install it under the name fd-find, then run it as fdfind. |
⚠️ When the search misses or hangs
- A find pattern without quotes. If a matching file is in the current folder, the shell replaces the pattern first. Then find looks for the wrong name, or fails with an error when more than one file matches. Always write
find . -name "*.log". - Forgetting grep treats case as different.
grep "error"skipsERROR. Use-iwhen unsure. - grep without a file name.
grep "TODO"with no file and no-rwaits for you to type, which looks like it's stuck. PressCtrl+C, then add a file name or-r .. - grep on a folder without -r.
grep "TODO" srcanswersgrep: src: Is a directory.
TL;DR: find searches for files by name. find . -name "*.log" finds every file whose name ends in .log in the current folder (.) and every subfolder (folder inside it). -iname ignores upper and lower case, -type f finds only files, and -type d only folders. The pattern *.log (* means any characters) must be quoted, so the shell doesn't replace it with file names first. grep searches the text inside files and prints every line that contains it: -i ignores upper and lower case, -n line numbers, -c counts, -v lines that don't match, -r every file in a folder, -l file names only.
⚡ Search by name: find
Write:
find . -name "*.log"
Meaning: find searches for files by name. find . starts from the current folder (.) and goes down into every subfolder (folder inside it). * means any characters, so *.log matches every name ending in .log. The quotes are a must, so the shell doesn't replace *.log with file names from the current folder before find runs.
⚡ Search by content: grep
Write:
grep "error" logs/app.log
Guess first: grep prints the lines that contain the text you search for. logs/app.log has three lines labelled ERROR (capital letters), and two of them also contain a lowercase error. How many lines get printed?
Two lines. grep treats upper and lower case as different, so ERROR payment timeout, which has no lowercase error, is left out. Use -i to get all three.
Meaning: grep searches the text inside a file, then prints every line that contains that text, whole.
⚡ Search every file
Write:
grep -rl "TODO" .
Meaning: -r goes into every file in the folder . (the current folder) and its subfolders. -l prints only the names of files that contain TODO (a note for work that isn't finished), without the lines. In short: to search by file name, use find. To search by file content, use grep.