terminal.cab

Linux Basics Lesson 7 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Finding files by name, age and size

10 minutes

Learning goals

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:

  • -name PATTERN by name, with the pattern in quotes
  • -size +100M larger than 100 MiB
  • -mtime +30 last changed more than 30 days ago

Example:

find . -size +40M
./archive/shop-2026-08.tar.gz
./archive/shop-2026-09.tar.gz
find . -name "*.log" -mtime +30
./projects/shop/build.log

in the glossary: find

Acting 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

find archive -size +47M

Before the command, in your home folder ~

  • ~/archive/
  • ~/archive/shop-2026-08.tar.gz
  • ~/archive/shop-2026-09.tar.gz
What does find archive -size +47M print?

Check works once you have chosen an answer.

Build the command

Build the line that lists only the folders below logs.

Your command

    Tap the blocks below to build the 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

    1. Question 1 of 3Why does the pattern in find -name "*.log" belong in quotes?

      Check works once you have chosen an answer.

    2. Question 2 of 3What does find -size -1k hit?

      Check works once you have chosen an answer.

    3. Question 3 of 3You want the files changed in the last 24 hours. Which criterion is it?

      Check works once you have chosen an answer.

    Cards

    1. Card 1 of 3

      Tap the card or press Space to turn it over.

    2. Card 2 of 3

      Tap the card or press Space to turn it over.

    3. 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

    Next: lesson 8, Disk usage 94% at 6:50 (8 min)

    What you can do now: find . -size +40M

    +0 XP in this lesson

    1. Lesson 1: open
    2. Lesson 2: open
    3. Lesson 3: open
    4. Lesson 4: open
    5. Lesson 5: open
    6. Lesson 6: open
    7. Lesson 7: done
    8. Lesson 8: open
    9. Lesson 9: open
    10. Lesson 10: open
    11. Lesson 11: open
    12. Lesson 12: open
    13. Lesson 13: open
    14. Lesson 14: open
    15. Lesson 15: open
    16. Lesson 16: open
    17. Lesson 17: open
    18. Lesson 18: open
    19. Lesson 19: open
    20. Lesson 20: open
    21. Lesson 21: open
    22. Lesson 22: open
    23. Lesson 23: open
    24. Lesson 24: open
    25. Lesson 25: open
    26. Lesson 26: open
    27. Lesson 27: open
    28. Lesson 28: open
    29. Lesson 29: open
    30. Lesson 30: open
    31. Lesson 31: open
    32. Lesson 32: open
    33. Lesson 33: open
    34. Lesson 34: open
    35. Lesson 35: open
    36. Lesson 36: open
    37. Lesson 37: open
    38. Lesson 38: open
    1 of 38

    Next: lesson 8, Disk usage 94% at 6:50 (8 min) That's enough for today

    Path complete

    You finished all 38 lessons of Linux Basics.

    Back to the path

    Cheat sheet

    The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.