← Linux and the Command Line Lesson 16 of 51 · 10 XP

find — finding files

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

find searches for files by many conditions and walks through all the nested folders:

find .                          # everything in the current folder, recursively
find . -name "*.py"             # by name (the pattern goes IN QUOTES)
find . -iname "readme*"         # ignoring case
find . -type f                  # only files (d — folders, l — links)
find /var/log -size +10M        # bigger than 10 megabytes
find . -mtime -1                # changed during the last 24 hours
find . -name "*.tmp" -delete    # find and delete
find . -name "*.c" -exec wc -l {} \;   # run a command for each file found

The quotes around the pattern are a must. Without them the shell itself expands *.py in the current folder before find runs, and the search uses the wrong pattern.

The order in which find lists files depends on the file system. If you need a stable order, add | sort.

Task. Files are scattered around a project. Print a sorted list of all the files (not folders) with the extension .c, and on the last line — how many there are.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.