Skip to lesson

Linux Command Line · Lesson 20 of 43

Finding files with find

Search folder trees for files by name, type, size and age with find, combine tests, and run commands on the results safely.

Beginner to intermediate·14 min·Text, redirection and pipes·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will locate any file by its name, type, size or age, and act on whole groups of results. grep searches inside files; find searches for files themselves. Together they let you answer "where is it?" in almost any folder tree.

The basic shape

find takes a starting place, then tests, then an optional action. It walks through every file and folder below the starting place, and prints those where all tests are true:

bash
cd ~/project
find . -name "*.txt" | sort
result
./data/fruit.txt
./docs/notes.txt
./docs/tasks.txt

. is the current folder. The pattern *.txt is in quotes so that the shell does not expand it first; find must receive the star itself. Without a test, find . lists everything below the current folder, which is the quickest way to see a whole tree.

Tests by name and type

  • -name PATTERN matches the file name (case sensitive), -iname ignores case.
  • -type f matches regular files, -type d directories and -type l symbolic links.
  • -path matches against the whole path, and -maxdepth N limits how deep to look.
bash
find . -type d | sort
find . -maxdepth 1 -type f | sort
find . -iname "readme*"
result
.
./data
./docs
./logs
./src
./README.md
./README.md

-name looks only at the last part of the path, so a pattern containing a slash never matches. Put -maxdepth before other tests, or GNU find prints a warning.

Tests by size

-size takes a number with a unit (c bytes, k kilobytes, M megabytes, G gigabytes). A leading + means larger than, - means smaller than, and no sign means exactly that size (rounded up to the unit):

bash
find . -type f -size +80c | sort
find . -type f -size -50c | sort
result
./data/scores.csv
./docs/notes.txt
./logs/app.log
./data/fruit.txt
./src/app.py
./src/hello.sh
./src/style.css

Tests by time

-mtime tests the modification time in days: -mtime -1 means modified within the last day, +30 means more than 30 days ago. -mmin does the same in minutes, and -newer FILE means newer than another file. Our practice files all share one timestamp, so we test a freshly created one:

bash
touch fresh.txt
find . -mmin -5 -type f
find . -type f -newer docs/notes.txt
result
./fresh.txt
./fresh.txt

Combining tests

Tests are joined with "and" by default. Use -o for "or" and ! for "not". Parentheses group tests, and they must be escaped from the shell with backslashes:

bash
find . -type f \( -name "*.sh" -o -name "*.py" \) | sort
find . -type f ! -name "*.txt" -size -100c | sort
result
./src/app.py
./src/hello.sh
./README.md
./data/scores.csv
./src/app.py
./src/hello.sh
./src/style.css

Actions: -print, -exec and -delete

The default action is -print. To run a command on each match, use -exec, with {} standing for the file and \; or + ending the command. The + form passes many files to one command, which is faster:

bash
find . -name "*.txt" -exec wc -l {} + | sort -k2
find . -name "*.sh" -exec ls -l {} \;
result
  7 ./data/fruit.txt
  5 ./docs/notes.txt
  5 ./docs/tasks.txt
  0 ./fresh.txt
 17 total
-rwxr-xr-x 1 learner learner 39 Jan 15  2026 ./src/hello.sh

-delete removes the matches. It is convenient, but it is also how people delete the wrong files, so follow this routine:

  1. Run the command with no action and read the list.
  2. Check the starting folder with pwd.
  3. Only then add -delete at the end of the command.
bash
touch junk1.tmp junk2.tmp
find . -name "*.tmp" | sort
find . -name "*.tmp" -delete
find . -name "*.tmp"
result
./junk1.tmp
./junk2.tmp

The last command printed nothing, as the files are gone. Putting -delete before the tests would delete everything, because tests are evaluated from left to right.

Names with spaces

File names can contain spaces or even newlines. -exec {} + handles them correctly. When piping to another program, use -print0 together with xargs -0, which the next lesson explains.

The order of the results

find lists files in the order the filesystem stores them, which is not alphabetical and differs between machines. When the order matters, pipe the result through sort, as in the examples above.

Faster alternatives

find reads the disk every time. On systems with a search database, locate name is instant, though it may be a day out of date. For simple code searches, grep -r with --include is often easier.

Common mistakes

Forgetting the quotes around the pattern. If a matching file exists in the current folder, the shell expands it and find gets the wrong argument.

Putting -delete first. Order matters: tests first, actions last.

Searching from /. It takes a long time and prints permission errors. Add 2>/dev/null and a narrower starting folder.

Assuming -mtime +1 means older than one day. It means "at least two days", because the time is rounded down to whole days.

Exercise

Find all .log and .csv files in project. Find directories at the top level only. List files bigger than 60 bytes, then count the lines of every .txt file with -exec wc -l {} +. Finally, create three .bak files and remove them with a preview first, then -delete.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 Which command lists all .txt files below the current folder?
02 What does find . -type d list?
03 What does -size +1M select?
04 What is the safest first step before using find ... -delete?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses