📖 Scenario: You work on a busy computer with many files. Sometimes, you need to find a file quickly to finish your work. Searching manually takes too long. Using commands to find files saves time and helps you work faster.
🎯 Goal: You will learn how to use the find command to search for files by name in a folder. This will help you find files quickly without looking through many folders.
📋 What You'll Learn
Create a folder with some files
Use a variable to store the folder path
Use the find command to search files by name
Print the list of found files
💡 Why This Matters
🌍 Real World
Finding files quickly helps you manage documents, code, or data on your computer without wasting time searching manually.
💼 Career
Many IT and developer jobs require fast file searching to debug, update, or organize projects efficiently.
Progress0 / 4 steps
1
Create a folder with files
Create a folder called mydocs and inside it create three files named report.txt, notes.txt, and summary.txt using the mkdir and touch commands.
Linux CLI
Hint
Use mkdir mydocs to make the folder and touch to create files inside it.
2
Set the folder path variable
Create a variable called folder and set it to the path ./mydocs.
Linux CLI
Hint
Use folder=./mydocs to store the folder path in a variable.
3
Use find command to search files
Use the find command with the variable folder to find files with the name notes.txt. Use find $folder -name notes.txt.
Linux CLI
Hint
Use find $folder -name notes.txt to search for the file named notes.txt inside the folder.
4
Print the found file path
Run the command to print the full path of the file notes.txt found inside the folder stored in folder.
Linux CLI
Hint
The output should show the path ./mydocs/notes.txt.
Practice
(1/5)
1. Why is using the find command helpful when working with many files?
easy
A. It deletes all files automatically.
B. It compresses files to save disk space.
C. It opens files in a text editor.
D. It quickly locates files based on criteria, saving time.
Solution
Step 1: Understand the purpose of find
The find command searches for files matching specific criteria like name or size.
Step 2: Recognize time-saving benefit
By locating files quickly, it avoids manual searching through many folders.
Final Answer:
It quickly locates files based on criteria, saving time. -> Option D
Quick Check:
Finding files fast = It quickly locates files based on criteria, saving time. [OK]
Hint: Remember: find locates files fast by criteria [OK]
Common Mistakes:
Thinking find deletes files automatically
Confusing find with file editors
Assuming find compresses files
2. Which of these is the correct syntax to find files named report.txt in the current directory?
easy
A. find -name report.txt .
B. find . -name report.txt
C. find report.txt -name .
D. find . report.txt -name
Solution
Step 1: Recall find command syntax
The correct syntax is find [path] -name [filename].
Step 2: Match syntax to options
find . -name report.txt uses find . -name report.txt, which is correct.
Final Answer:
find . -name report.txt -> Option B
Quick Check:
Correct syntax = find . -name report.txt [OK]
Hint: Use: find [path] -name [filename] [OK]
Common Mistakes:
Placing -name after filename
Swapping path and name arguments
Omitting the dot for current directory
3. What will be the output of this command?
find /tmp -name '*.log' -size +1M
Assuming there are two files: /tmp/error.log (2MB) and /tmp/debug.log (500KB).
medium
A. /tmp/error.log
B. /tmp/debug.log
C. /tmp/error.log
/tmp/debug.log
D. No output
Solution
Step 1: Understand the command filters
The command finds files in /tmp with names ending in .log and size greater than 1MB.
Step 2: Check file sizes against criteria
/tmp/error.log is 2MB (matches), /tmp/debug.log is 500KB (too small).
Final Answer:
/tmp/error.log -> Option A
Quick Check:
Only large .log files listed = /tmp/error.log [OK]
Hint: Size +1M means files larger than 1MB [OK]
Common Mistakes:
Including files smaller than 1MB
Ignoring the size filter
Assuming all .log files show
4. You run find /home -name '*.txt' -print but get an error: find: missing argument to `-name'. What is the likely cause?
medium
A. You typed -name without a pattern.
B. You used single quotes instead of double quotes.
C. You placed the path after the options.
D. You forgot to quote the pattern '*.txt'.
Solution
Step 1: Analyze the error message
The error says "missing argument to -name", meaning no pattern was given after -name.
Step 2: Identify the cause
This happens if -name is used without a following pattern string.
Final Answer:
You typed -name without a pattern. -> Option A
Quick Check:
Missing pattern after -name = You typed -name without a pattern. [OK]
Hint: Always provide a pattern after -name [OK]
Common Mistakes:
Assuming quotes cause the error
Thinking path order matters here
Ignoring the missing pattern
5. You want to find all files modified in the last 2 days and larger than 100KB in your Documents folder. Which command will save you the most time?
hard
A. find ~/Documents -mtime +2 -size -100k
B. find ~/Documents -mtime 2 -size 100k
C. find ~/Documents -mtime -2 -size +100k
D. find ~/Documents -mtime 2 -size +100k
Solution
Step 1: Understand the time and size filters
-mtime -2 means modified within last 2 days; -size +100k means larger than 100KB.