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.
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:
cd ~/project
find . -name "*.txt" | sort./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 PATTERNmatches the file name (case sensitive),-inameignores case.-type fmatches regular files,-type ddirectories and-type lsymbolic links.-pathmatches against the whole path, and-maxdepth Nlimits how deep to look.
find . -type d | sort
find . -maxdepth 1 -type f | sort
find . -iname "readme*".
./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):
find . -type f -size +80c | sort
find . -type f -size -50c | sort./data/scores.csv
./docs/notes.txt
./logs/app.log
./data/fruit.txt
./src/app.py
./src/hello.sh
./src/style.cssTests 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:
touch fresh.txt
find . -mmin -5 -type f
find . -type f -newer docs/notes.txt./fresh.txt
./fresh.txtCombining 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:
find . -type f \( -name "*.sh" -o -name "*.py" \) | sort
find . -type f ! -name "*.txt" -size -100c | sort./src/app.py
./src/hello.sh
./README.md
./data/scores.csv
./src/app.py
./src/hello.sh
./src/style.cssActions: -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:
find . -name "*.txt" -exec wc -l {} + | sort -k2
find . -name "*.sh" -exec ls -l {} \; 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:
- Run the command with no action and read the list.
- Check the starting folder with
pwd. - Only then add
-deleteat the end of the command.
touch junk1.tmp junk2.tmp
find . -name "*.tmp" | sort
find . -name "*.tmp" -delete
find . -name "*.tmp"./junk1.tmp
./junk2.tmpThe 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.
Ready for the next small win?
Mark the lesson complete when you are ready.