Technical skill

Linux command: find

Search through a set of folders

If you need a log from last week or cannot remember where a file is, checking folders one by one takes time. find starts at a folder and checks the paths below it. You can ask it to keep files with a particular name, type, age, size, or owner. Think of it as walking through a folder tree and testing each item against your question.

For example, this prints regular log files in an application log folder that are more than a week old:

find /var/log/myapp -type f -name '*.log' -mtime +7 -print

The starting path is /var/log/myapp. -type f keeps regular files, -name checks their names, and -print displays the matches. The quoted pattern *.log reaches find unchanged. Without the quotes, the shell might expand it against names in the current folder before find runs. This pattern is a simple shell-style glob, not a regular expression.

Combine search conditions

GNU find measures -mtime in whole 24-hour periods, rounding down. As a result, -mtime +7 matches files only after at least eight complete periods have passed. Other tests, such as -type, -name, -mtime, and -size, can be combined. Adjacent tests are joined with AND, so all of them must match. -o means OR. When grouping a longer condition with -o, use parentheses and quote or escape them so the shell passes them to find. Print results before relying on a complex expression; its precedence can be easy to misread.

The -exec action runs another command for each group of matches. With -exec command {} +, find batches matched paths as separate arguments, reducing the number of times the other program starts. For example, find /srv/app -type f -name '*.conf' -exec stat -- {} + inspects matching files without splitting names at spaces. GNU -print0 can be paired with GNU xargs -0 to handle names containing spaces, quotes, or newlines. A plain find ... -print | xargs ... pipeline can split those names.

Keep actions safe

Some find actions change files. GNU -delete removes each match. First preview the exact paths with -print, keep the starting folder narrow, then use deletion only after checking the preview. -delete implies depth-first traversal and does not work usefully with -prune. A broad command such as find /some/path -delete can erase everything below that path. Check the local manual for GNU-specific tests and differences on other Unix systems.

Permission or filesystem errors can make a search incomplete and give the command a nonzero status. For cleanup, build and test the expression in a safe directory, compare the results with known files, then check the status when running it on the intended folder. Try a mix of names and ages before relying on an automated action.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful