Linux Basics Lesson 7 of 38
Finding files by name, age and size
10 minutes
Learning goals
- Search a whole tree with find by name, type, size and age.
- Run a command on every match with -exec, and always look at the matches first.
- Explain why find -size -1k only hits empty files.
You met find by name in Terminal-Start, lesson 10. The questions about size and age, and acting on
the matches, are what make it a tool for servers.
Name and type
find START CRITERIA: where to begin, then what has to apply. -name takes a pattern, and that pattern belongs in quotes.
Without them, the shell expands *.log first if a matching file happens to be in the current folder, and find gets a wrong
list. -type f restricts it to files, -type d to folders.
find projects -name "*.log"
projects/shop/build.log
projects/shop/test.log
find projects -type d -name "v*"
projects/shop/releases/v2.4.0
projects/shop/releases/v2.4.1
Size and age
-size +40M means larger than 40 MiB, and -mtime +30 means last changed more than 30 days ago. A minus means less: -mtime -1
is the last 24 hours. Criteria in a row all have to apply.
find . -size +40M
Purpose: Searches a whole tree by name, type, size or age.
Syntax: find START [CRITERIA]
Options:
Example:
find . -size +40M
./archive/shop-2026-08.tar.gz
./archive/shop-2026-09.tar.gz
find . -name "*.log" -mtime +30
./projects/shop/build.logActing on the matches
-exec COMMAND {} \; runs a command for every match. {} stands for the path of the match, and \; ends the command. The
backslash is there so that the shell passes the semicolon on to find instead of treating it as its own.
find projects -name "build.log"
projects/shop/build.log
find projects -name "build.log" -exec rm {} \;
find projects -name "build.log"
The first line is not decoration: it shows what the second one is about to delete. The third confirms it is gone.
Practice
Terminal task 1/3
Under projects, find all .log files that were last changed more than 30 days ago.
Task 1/3: Under projects, find all .log files that were last changed more than 30 days ago.
Under projects, find all .log files that were last changed more than 30 days ago.
Task complete
Solution
find projects -name "*.log" -mtime +30 Why: This line is how you find clean-up candidates. test.log is left out, because it was changed yesterday.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/3
Find all files larger than 40 MiB below your home folder.
Task 2/3: Find all files larger than 40 MiB below your home folder.
Find all files larger than 40 MiB below your home folder.
Task complete
Solution
find . -size +40M Why: When a disk fills up, a few big files are usually to blame. A search by size finds them, no matter how deep they are.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Delete build.log under projects with find and -exec. First run the same line without -exec. test.log stays.
Task 3/3: Delete build.log under projects with find and -exec. First run the same line without -exec. test.log stays.
Delete build.log under projects with find and -exec. First run the same line without -exec. test.log stays.
Task complete
Solution
find projects -name "build.log"
find projects -name "build.log" -exec rm {} \; Why: Between the two lines only the action differs. So what you saw is exactly what gets deleted. This habit pays off most on a tree you do not know.
Another way
find projects -name "build.log"
find projects -name "build.log" -delete -delete is shorter and needs no rm, but it is only safe at the very end of the line. -exec rm can be swapped for -exec ls -l to take another look.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
find archive -size +47MBefore the command, in your home folder ~
- ~/archive/
- ~/archive/shop-2026-08.tar.gz
- ~/archive/shop-2026-09.tar.gz
Check works once you have chosen an answer.
Build the command
Build the line that lists only the folders below logs.
Your command
Building blocks
Check works once your command has at least one block.
Fill the gap
In find -exec rm {} the last characters end the command.
In find -exec rm {} … the last characters end the command.
Check works once you have typed an answer.
Next up: the disk is at 94 percent, and the monitoring has woken you.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 6 exercises, 3 quiz questions
What you can do now: find . -size +40M
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: done
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
find <start> -name "*.log"by name, pattern in quotes -
find <start> -type donly folders, -type f for files -
find <start> -size +100Mlarger than 100 MiB -
find <start> -mtime +30changed more than 30 days ago -
find ... -exec rm {} \;delete every match, first without -exec
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 19 KB)
- View as image (PNG, 63 KB)