LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 713 min read

Finding Files and Text: find and grep

Locate files by name, type, size and age with find, search inside files with grep, and find where commands live with which and type.

On this page 11 sections

Introduction

Where is that config file? Which pages still contain a TODO? Which files are eating the disk? The answers come from two of the most useful commands in Linux: find, which searches for files, and grep, which searches for text inside them. Priya will use them on a small website project:

tree site
Output

Click Run to see what this code prints.

What You Will Learn
  • How to find files by name, type, size and modification time.
  • How to search text inside one file or a whole folder with grep.
  • The grep options you will use most: -r, -n, -i, -l, -c and -v.
  • How to see where a command is installed.

Finding Files by Name

The pattern is find <where> <tests>. Quote the name pattern so the shell passes the wildcard to find instead of expanding it itself:

find site -name "*.html"
Output

Click Run to see what this code prints.

-name is case-sensitive; -iname ignores case:

find site -iname "LOGO*"
Output

Click Run to see what this code prints.

By Type, Size and Age

-type d finds only directories (-type f only files):

find site -type d
Output

Click Run to see what this code prints.

-size +1M finds files larger than 1 megabyte — great for tracking down what is filling a disk:

find site -type f -size +1M
Output

Click Run to see what this code prints.

-mtime -7 finds files modified in the last 7 days. The blog post and stylesheet were last changed in January, so they are not listed:

find site -type f -mtime -7
Output

Click Run to see what this code prints.

TestFinds
-name "*.log"Names matching a pattern (case-sensitive)
-iname "readme*"Names matching, ignoring case
-type f / -type dOnly files / only directories
-size +100M / -size -1kLarger than 100 MB / smaller than 1 KB
-mtime -7 / -mtime +30Modified within 7 days / more than 30 days ago
-emptyEmpty files and folders
find Can Also Delete

Adding -delete to a find command deletes everything it matches. Always run the command without -delete first and read the list.

Searching Inside Files: grep

grep <pattern> <file> prints every line that contains the pattern:

grep "Contact" site/index.html
Output

Click Run to see what this code prints.

Searching a Whole Project

-r searches every file in a folder and its subfolders, and -n adds line numbers — the classic way to find every TODO:

grep -rn "TODO" site
Output

Click Run to see what this code prints.

That missed one: the JavaScript file says “todo” in lower case. Add -i to ignore case:

grep -rni "todo" site
Output

Click Run to see what this code prints.

Mini Challenge · Fix the Command

You need every line mentioning “error” in the logs folder, whether it is written Error, ERROR or error, with the file name and line number. Which command works?

Choose one answer

Useful grep Options

-l lists only the names of matching files:

grep -rli "todo" site
Output

Click Run to see what this code prints.

-c counts matching lines per file, and -v inverts the search, printing lines that do not match:

grep -c "<h1>" site/index.html site/contact.html
grep -v "Contact" site/index.html
Output

Click Run to see what this code prints.

OptionEffect
-rSearch folders recursively
-nShow line numbers
-iIgnore upper/lower case
-lList matching file names only
-cCount matching lines
-vShow lines that do not match
-wMatch whole words only
-EUse extended patterns, e.g. grep -E "error|warning"

Where Is That Command?

which shows the program a command runs, and type also explains shell built-ins — commands such as cd that are part of the shell itself rather than separate programs:

which ls python3 bash
type cd ls
Output

Click Run to see what this code prints.

Common Mistakes

Avoid These Mistakes
  • Not quoting patterns like "*.html", so the shell expands them before find sees them.
  • Using find to search text inside files (that is grep’s job) or grep to search file names (find’s job).
  • Forgetting -i and missing matches that differ only in case.
  • Running find from / without sudo and drowning in “Permission denied” messages — add 2>/dev/null (next lesson).

Frequently Asked Questions

locate searches a pre-built index, so it is much faster than find, but it can be out of date and is not always installed. find always searches the real files.

ripgrep (rg) is a popular, very fast alternative that automatically skips files ignored by .gitignore. grep is still worth knowing because it is installed everywhere.

Yes — zgrep searches .gz files directly, which is handy because old logs are usually compressed.

Key Takeaways

  • find <where> -name "pattern" finds files by name; -iname ignores case.
  • -type, -size and -mtime filter by kind, size and age.
  • grep pattern file finds lines of text; -r searches whole folders.
  • Combine grep options: -n line numbers, -i ignore case, -l file names, -c counts, -v invert.
  • which and type show where a command comes from.

Summary

You can now find any file and any line of text. In the next lesson you will connect commands together with pipes, so small tools like grep, sort and wc combine into powerful one-line data processors.

Next Lesson →

Pipes, Redirection and Text Processing