Bird
Raised Fist0
Linux CLIscripting~10 mins

find command basics in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - find command basics
Start: specify directory
↓
Check each file/dir
↓
Apply conditions
↓
Print/Action
↓
More files?
No→End
↩Back to Check each file/dir
The find command starts at a directory, checks each file or folder, applies conditions, and prints or acts on matches until all are checked.
Execution Sample
Linux CLI
find /tmp -name "*.txt"
Searches the /tmp directory and its subdirectories for files ending with .txt and lists them.
Execution Table
StepCurrent File/DirCondition (-name "*.txt")ResultAction
1/tmp/file1.txtMatches *.txtYesPrint /tmp/file1.txt
2/tmp/file2.logDoes not match *.txtNoSkip
3/tmp/docsIs directoryN/ADescend into /tmp/docs
4/tmp/docs/readme.txtMatches *.txtYesPrint /tmp/docs/readme.txt
5/tmp/docs/image.pngDoes not match *.txtNoSkip
6/tmp/docs/archiveIs directoryN/ADescend into /tmp/docs/archive
7/tmp/docs/archive/notes.txtMatches *.txtYesPrint /tmp/docs/archive/notes.txt
8No more filesN/AN/AEnd search
💡 All files and directories under /tmp checked, search ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
Current File/Dir/tmp/tmp/file1.txt/tmp/file2.log/tmp/docs/tmp/docs/readme.txt/tmp/docs/image.png/tmp/docs/archive/tmp/docs/archive/notes.txtNo more files
Match FoundNoYesNoN/AYesNoN/AYesNo
Key Moments - 3 Insights
Why does find descend into directories even if they don't match the name condition?
Because find checks directories to look inside them for matching files. The condition applies to files, but directories are explored to find matches deeper (see rows 3 and 6 in execution_table).
What happens when a file does not match the condition?
The file is skipped and not printed or acted on (see rows 2 and 5 in execution_table).
How does find know when to stop?
It stops after checking all files and directories under the starting point (see row 8 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the action taken at step 4?
ASkip the file
BPrint /tmp/docs/readme.txt
CDescend into directory
DEnd search
💡 Hint
Check the 'Action' column at step 4 in the execution_table.
At which step does find descend into the /tmp/docs/archive directory?
AStep 5
BStep 6
CStep 7
DStep 3
💡 Hint
Look for 'Descend into /tmp/docs/archive' in the Action column.
If the condition was changed to -name "*.log", what would happen at step 2?
APrint /tmp/file2.log
BSkip /tmp/file2.log
CDescend into /tmp/file2.log
DEnd search
💡 Hint
Check how the condition affects the Result and Action columns for matching files.
Concept Snapshot
find [path] [condition]
- Starts at path, checks each file/dir
- Applies condition (e.g., -name "*.txt")
- Prints or acts on matches
- Descends into directories to search deeper
- Stops after all checked
Full Transcript
The find command starts at a given directory and looks at each file and folder inside it. For each item, it checks if it matches the condition, like a name pattern. If it matches, find prints the file path or does an action. If the item is a directory, find goes inside it to check more files. This continues until all files and folders under the starting directory are checked. For example, searching for '*.txt' files in /tmp will print all text files found there and in its subfolders.

Practice

(1/5)
1. What does the find command do in Linux?
easy
A. Copies files from one folder to another
B. Deletes files permanently
C. Searches for files and directories based on criteria
D. Displays the contents of a file

Solution

  1. Step 1: Understand the purpose of the find command

    The find command is used to locate files and directories by searching through folders and subfolders.
  2. Step 2: Compare options

    Options B, C, and D describe other commands like rm, cp, and cat, not find.
  3. Final Answer:

    Searches for files and directories based on criteria -> Option C
  4. Quick Check:

    find = search files [OK]
Hint: Remember: find means locate files/folders fast [OK]
Common Mistakes:
  • Confusing find with delete or copy commands
  • Thinking find edits files
  • Assuming find shows file contents
2. Which of the following is the correct syntax to find all files named notes.txt in the current directory and its subdirectories?
easy
A. find . -name notes.txt
B. find -name notes.txt .
C. find notes.txt -name .
D. find . notes.txt -name

Solution

  1. Step 1: Recall find command syntax

    The correct syntax is: find [path] [options]. Here, '.' means current directory, '-name' specifies the filename.
  2. Step 2: Check each option

    find . -name notes.txt matches the correct syntax. Options A, B, and D have misplaced arguments or options.
  3. Final Answer:

    find . -name notes.txt -> Option A
  4. Quick Check:

    find . -name filename [OK]
Hint: Path first, then options like -name [OK]
Common Mistakes:
  • Putting filename before path
  • Missing dot for current directory
  • Wrong order of options and arguments
3. What will be the output of the command find /tmp -type d -name "test" if there is a directory named test inside /tmp?
medium
A. It will list the full path of the directory named test inside /tmp
B. It will list all files named test inside /tmp
C. It will delete the directory named test inside /tmp
D. It will show an error because of incorrect syntax

Solution

  1. Step 1: Understand the command options

    /tmp is the search path, -type d means only directories, -name "test" looks for name 'test'.
  2. Step 2: Predict output

    The command will print the full path of any directory named 'test' inside /tmp and its subfolders.
  3. Final Answer:

    It will list the full path of the directory named test inside /tmp -> Option A
  4. Quick Check:

    find with -type d lists directories [OK]
Hint: -type d means directories only [OK]
Common Mistakes:
  • Confusing -type d with files
  • Expecting deletion instead of listing
  • Assuming syntax error without checking
4. You run find /home/user -name '*.txt' but get no results even though there are .txt files. What is the most likely problem?
medium
A. The quotes prevent the shell from expanding the wildcard
B. The path /home/user does not exist
C. The command should use -type d instead of -name
D. The wildcard * is not supported by find

Solution

  1. Step 1: Understand wildcard usage in find

    Using quotes around '*.txt' prevents the shell from expanding the wildcard, which is correct because find handles the pattern matching itself.
  2. Step 2: Identify common mistake

    With correct syntax, no results despite .txt files existing suggests the path /home/user does not exist or is inaccessible.
  3. Final Answer:

    The path /home/user does not exist -> Option B
  4. Quick Check:

    ls /home/user [OK]
Hint: Verify path exists before searching [OK]
Common Mistakes:
  • Removing quotes and letting shell expand
  • Using -type d for files
  • Wrong path assumption
5. How can you use the find command to locate all files larger than 1MB in the /var/log directory and delete them safely after confirmation?
hard
A. find /var/log -type d -size +1M -ok rm {} \;
B. find /var/log -type f -size +1M -delete
C. find /var/log -size 1M -exec rm {} \;
D. find /var/log -type f -size +1M -ok rm {} \;

Solution

  1. Step 1: Identify criteria for files larger than 1MB

    -type f means files, -size +1M means size greater than 1 megabyte.
  2. Step 2: Choose safe deletion method

    -ok rm {} \; asks for confirmation before deleting each file, which is safer than -delete or -exec rm without confirmation.
  3. Step 3: Check options

    find /var/log -type f -size +1M -ok rm {} \; uses -ok for confirmation, correct. find /var/log -type f -size +1M -delete deletes without confirmation. find /var/log -size 1M -exec rm {} \; uses wrong size syntax and no confirmation. find /var/log -type d -size +1M -ok rm {} \; searches directories, not files.
  4. Final Answer:

    find /var/log -type f -size +1M -ok rm {} \; -> Option D
  5. Quick Check:

    -ok asks confirmation before delete [OK]
Hint: Use -ok for safe delete with confirmation [OK]
Common Mistakes:
  • Using -delete without confirmation
  • Wrong size syntax
  • Searching directories instead of files
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard