Linux Command Basics — Day 4: Search, Inspect, and Automate
Objectives
- Search for files with find
- Search inside files with grep and summarize
- Use
xargsand –execto batch edit files - Write small scripts and make them executable
Materials
- Computers with terminal access
Activities
Mission: Search, Inspect, and Automate
Your Goal
Discover how to search for files and content, inspect multiple files quickly, and combine commands into small scripts to automate repetitive tasks. Learn by experimenting: run short experiments, capture command + output evidence, and explain what you discovered.
Duration
45 minutes
Allowed Tools (today)
find, grep, sort
head, tail, less, cat
xargs
echo, >, >>, |
bash # (small one-line scripts), chmod
pwd, ls, cd, cp, mv # (from earlier days)Rules
- Work in pairs; switch Driver/Navigator every 8 minutes.
- Record each task with: command used, exact output (or screenshot), and 1–2 sentence explanation.
- Work inside your
~/mission-day2/workspace created earlier. - Use hint ladder only when stuck.
Starter checks
cd ~/mission-day2
ls -la
pwdRecord outputs for your notebook.
Tasks — Inquiry Challenges
Try each task, then record commands and outputs as evidence.
Task A — Locate the files
- Use
findto list all .txt files under your workspace:find . -name "*.txt". - Limit results to depth 2:
find . -maxdepth 2 -name "*.txt". - Evidence: commands and outputs; note how
finddiffers fromlsand when it’s useful.
Task B — Search inside files
- Use
grep -n "pattern"to find lines containing a word (e.g.,idea) across files:grep -n "idea" -R notes/. - Use
grep -ito ignore case andgrep -rto search recursively. - Evidence: the commands and matched lines with filenames.
Task C — Summarize and sort
- Combine commands to count matches:
grep -r "idea" notes/ | wc -l. - Produce a list of files containing matches and sort by count:
grep -r "idea" notes/ | cut -d: -f1 | sort | uniq -c | sort -nr. - Evidence: the pipeline commands and outputs; explain each step.
Task D — Batch edits with xargs
- Use
find+xargsor-execto perform an action on multiple files (e.g., append a line to every .txt). - Safer example (use sh -c so redirection happens per-file):
find notes/ -name “*.txt” -print0 | xargs -0 -I {} sh -c ‘printf “checked %s\n” “$(date)” >> “$1″‘ — {}
- Evidence: before/after samples from one edited file.
Task E — Small script challenge
- Write a one-file script
scripts/report.shthat prints a short report: number of .txt files and top 3 files with most matches for the wordidea. Make it executable withchmod +xand run it. - Example script body (one-file, portable — runs from the unit workspace):
#!/bin/bash
cd ~/mission-day2 || exit 1
echo "TXT files: $(find . -name '*.txt' | wc -l)"
echo "Top idea files:"
grep -r "idea" notes/ | cut -d: -f1 | sort | uniq -c | sort -nr | head -n 3- Evidence: script contents, chmod output (ls -l), and script run output.
Exit Ticket (short answers)
1. How does find differ from ls? Give one example.
2. What does xargs help you do?
3. Why might you prefer a small script over a long one-line pipeline?
4. How do you make a script executable?
Assessment (performance)
Students must submit a single text file mission-day4-evidence.txt in ~/mission-day2/ containing:
- For Tasks A–E: each task header, the commands you ran, captured outputs (or pasted snippets), and a one-line conclusion.
- Minimum: 5 command+output pairs demonstrating find, grep, a pipeline with sort/uniq, an xargs or -exec action, and a small executable script run.
Student Handout (printable)
Cheat-sheet (quick commands)
- Find files:
find . -name "*.txt" - Search text:
grep -n "pattern" -R notes/ - Count matches:
grep -r "pattern" notes/ | wc -l - Summarize by file:
grep -r "pattern" notes/ | cut -d: -f1 | sort | uniq -c | sort -nr - Run command on many files:
find . -name "*.txt" -print0 | xargs -0 -I {} <cmd> {} - Small script: create file, add shebang
#!/bin/bash,chmod +x file, ./file
Tasks (short)
- A: Use find to locate
.txtfiles and note differences with ls. - B: Use grep to search inside files for a keyword.
- C: Build a pipeline to count and rank files by matches.
- D: Use
xargsor-execto append a line to many files and show before/after for one file. - E: Create a small executable script report.sh that summarizes workspace state and run it.
Submission: save mission-day4-evidence.txt in ~/mission-day2/ with your evidence.
Hint Ladder
Hint 1 — gentle
findsearches directories recursively;lslists a directory.greplooks for text inside files;-nshows line numbers.xargsconverts input lines into arguments for another command.
Hint 2 — specific
- Try:
find . -name "*.txt" - To count:
grep -r "idea" notes/ | wc -l - To append safely:
find notes/ -name "*.txt" -print0 | xargs -0 -I {} sh -c 'printf "checked %s\n" "$(date)" >> "$1"' -- {}
Hint 3 — explicit commands
find . -maxdepth 2 -name "*.txt"grep -n "idea" -R notes/grep -r "idea" notes/ | cut -d: -f1 | sort | uniq -c | sort -nrfind notes/ -name "*.txt" -print0 | xargs -0 -I {} sh -c 'printf "checked %s\n" "$(date)" >> "$1"' -- {}
Differentiation
- Struggling students: focus on Tasks A and B only (finding + searching). Submit 3 command+output pairs.
- Advanced students: extend the script to accept a search term and output a CSV with filename,count.
Final check
Ensure ~/mission-day2/mission-day4-evidence.txt exists and contains all required evidence.
Exit Ticket
1) How does find differ from ls?
2) What does xargs help you do?
3) Why prefer a script over a long pipeline?
4) How do you make a script executable?