Search the Current Filesystem with find
Search the Current Filesystem with find
You remember saving an attendance list somewhere under the school's folder, but not which class subfolder contains it. Instead of opening folders one at a time, you can ask find to walk that part of the filesystem and select matching entries. You choose both the starting point and the conditions.
Let's search for regular text files. The distinction between what the shell does and what find does is important: we want find to receive the wildcard pattern and test it against names it discovers, rather than have the shell expand it before the search starts.
What you’ll learn
- Search a chosen directory tree by filename and file type.
- Quote a wildcard so find receives the pattern instead of shell-expanded names.
Search a live tree by type and name
Starting point
Only the chosen school tree is searched.
school/Tests
The file must be a regular file and its name must match the quoted pattern.
-type f -name '*.txt'Match
attendance.txt passes; marks.csv does not match the suffix.
school/attendance.txt
Find attendance files in the school tree
Prepare the example
Use a new practice folder. The first two lines create a school directory and two empty files with different suffixes. That gives the search one match and one non-match to explain, without relying on files elsewhere on your machine.
Bash · Linux terminal
mkdir school
touch school/attendance.txt school/marks.csv
find school -type f -name '*.txt'Expected result
school/attendance.txtIn find school -type f -name '*.txt', school is the starting directory. -type f selects regular files, and -name compares the entry's name with the supplied pattern. The single quotes keep the shell from expanding *.txt in the current directory first.
The output names school/attendance.txt; the CSV file does not match the suffix. Each result includes its path from the chosen starting point, which helps you locate it. If a search is too broad, narrow the starting directory or conditions before adding actions. Searching is a useful first step before any batch change or deletion.
Combine name and entry-type conditions
The find command searches the live filesystem starting from a directory you choose. You can narrow results by filename, item type, size, modification time and many other conditions.
Basic Syntax of find
The basic syntax of the find command looks like this:
find [starting_directory] [conditions]
By default, if you don’t provide a starting directory, it uses the current directory (.) as the root for the search.
Example 1: Find files by name
find . -name "file.txt"
./docs/file.txt
./backup/file.txt
This command will search for any file named file.txt starting from the current directory. It will return the path to each matching file.
Tip: Case-Insensitive Search
find . -iname "file.txt"
Using -iname instead of -name makes the search case-insensitive.
Example 2: Find only directories
find . -type d -name "config"
This will search for directories (not files!) named config.
Example 3: Find only files
find . -type f -name "*.log"
Here, we’re looking for files ending in .log. The -type f ensures we only get files, not directories.
Size, age and actions on matched entries
Example 4: Find files by size
find /var/log -type f -size +10M
This finds files larger than 10 megabytes in the /var/log directory. You can also use:
-size -10Mfor less than 10 MB-size 10Mfor exactly 10 MB
Example 5: Find files modified recently
find . -type f -mtime -1
This finds files modified in the last 1 day. You can change the number to any number of days.
Example 6: Delete found files (Be Careful!)
find . -name "*.tmp" -type f -delete
This will delete all .tmp files it finds. Make sure to double-check your path and condition before using -delete!
Example 7: Execute a command on each result
find . -name "*.sh" -exec chmod +x {} \;
This finds all shell scripts and makes them executable using chmod +x. The {} is replaced with each file path, and \; ends the command.
Use find patterns to locate files precisely
-nameto match file names-type ffor files and-type dfor directories-sizefor size filters-mtimefor modification time-execand-deletefor actions
And that’s it! You’ve just learned how to find files confidently using the find command in Linux.
Practice these on your system and try customizing the options. You’ll get comfortable in no time!
Try it, then check the worked answer
Your task: Match text files beneath school without shell glob expansion.
Use a new practice folder. The first two lines create a school directory and two empty files with different suffixes. That gives the search one match and one non-match to explain, without relying on files elsewhere on your machine.
Bash · complete example for this question
mkdir school
touch school/attendance.txt school/marks.csv
find school -type f -name '*.txt'Expected result
school/attendance.txtWorked answer
find school -type f -name '*.txt'
Quote *.txt so the pattern reaches find intact. Without quotes, any matches in the invoking shell's current directory can change the arguments before find begins. Also retain -type f when the task asks for files rather than directories with a matching suffix.
Check your decision
Give find the search pattern as a single argument
Read a find command as starting point plus tests. Keep patterns intended for the tool quoted, inspect the returned paths, and distinguish a search from an action applied to its results. A narrow, checked search is much easier to trust than an unexplained scan of everything.
Reaching the recap and completing the quizzes are separate milestones.