0
0
Linux CLIscripting~10 mins

find with -exec for actions in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - find with -exec for actions
Start: find command
Search files matching criteria
For each file found
Execute action with -exec
Repeat for next file
End when no more files
The find command searches files, then runs the specified action on each file found using -exec, repeating until all files are processed.
Execution Sample
Linux CLI
find . -type f -name '*.txt' -exec echo Found: {} \;
This command finds all .txt files in the current directory and prints 'Found: filename' for each.
Execution Table
StepFile FoundAction ExecutedOutput
1./notes.txtecho Found: ./notes.txtFound: ./notes.txt
2./docs/readme.txtecho Found: ./docs/readme.txtFound: ./docs/readme.txt
3./logs/log1.txtecho Found: ./logs/log1.txtFound: ./logs/log1.txt
4No more filesNo actionEnd of execution
💡 No more files matching '*.txt' found, so find stops.
Variable Tracker
VariableStartAfter 1After 2After 3Final
Current FileNone./notes.txt./docs/readme.txt./logs/log1.txtNone
Key Moments - 3 Insights
Why do we use '{}' in the -exec action?
The '{}' is a placeholder replaced by the current file found, as shown in execution_table rows 1-3 where the file path appears in the output.
Why is there a '\;' at the end of the -exec command?
The '\;' tells find where the -exec command ends. Without it, find won't know when to stop reading the action, as seen in the execution_sample code.
Does the action run once or multiple times?
The action runs once for each file found, shown by multiple rows in execution_table where each file triggers the action.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when the second file is found?
AFound: ./docs/readme.txt
BFound: ./notes.txt
CNo action
DEnd of execution
💡 Hint
Check row 2 in the execution_table under Output column.
At which step does find stop executing actions?
AStep 3
BStep 4
CStep 1
DNever stops
💡 Hint
Look at the exit_note and the last row in execution_table.
If we change '*.txt' to '*.log', how would the execution_table change?
AOutput would be empty
BNo files would be found
CFiles with .log extension would appear instead of .txt
DThe command would fail
💡 Hint
The search pattern controls which files are found, as shown in the Current File variable_tracker.
Concept Snapshot
find [path] [criteria] -exec [command] {} \;
- Finds files matching criteria
- '{}' is replaced by each file found
- '\;' ends the -exec command
- Action runs once per file
- Useful for batch file operations
Full Transcript
The find command searches for files matching given criteria. When using -exec, it runs a specified action on each file found. The '{}' is a placeholder replaced by the current file path. The '\;' marks the end of the action command. The process repeats for every matching file until none remain, then find stops. For example, 'find . -type f -name '*.txt' -exec echo Found: {} \;' prints the name of each .txt file found. This helps automate tasks on many files easily.