6.5 KiB
Step 3 — Searching & Text Viewing (Ubuntu 24)
Type along exactly as shown. This step is written for absolute beginners and assumes no prior Linux experience.
Estimated time: ~10–15 minutes
What you’ll learn
- How to view text files (
cat,less,head,tail) - How to search inside files with
grep - How to find files/folders anywhere with
find - How to chain commands together with the pipe (
|) for quick analysis - How to avoid common mistakes (case, spaces, patterns)
This expands the original brief step to include practical variations, safety notes, and troubleshooting.
Setup: Continue using your practice area from previous steps (recommended):
mkdir -p ~/playground && cd ~/playground
0) Create some sample files to search
We’ll set up a few files so your searches have something to find.
cd ~/playground
echo "Hello Linux" > hello.txt
echo "hello again" >> hello.txt
echo "Linux is powerful" > notes.txt
echo "HELLO CAPS" > caps.txt
mkdir -p logs
printf "alpha\nbeta\ngamma\nBeta\nGamma\n" > logs/mixed.txt
Verify:
ls -l
ls -l logs
1) View files quickly (cat) and with scrolling (less)
A) View entire file now:
cat hello.txt
catprints the file to your screen immediately (great for short files).
B) Scroll through long files:
less notes.txt
- Keys: Up/Down or PageUp/PageDown to move;
/wordto search;nfor next match;qto quit. lessdoesn’t load the whole file at once — it’s ideal for big files and logfiles.
C) Peek at the top/bottom only (useful for logs):
head -n 3 hello.txt # first 3 lines
tail -n 3 hello.txt # last 3 lines
tail -f hello.txt # follow new lines live; press Ctrl+C to stop
Tip: Use
tail -fwhile another process writes to a log to watch updates in real time.
2) Search inside files with grep
grep finds lines containing a pattern.
A) Basic search (case-sensitive by default):
grep "Hello" hello.txt
- Matches
Hellobut nothello.
B) Case-insensitive search:
grep -i "hello" hello.txt
C) Show line numbers with matches:
grep -n "hello" hello.txt
D) Search multiple files at once:
grep -n "linux" *.txt
- Matches lines containing
linuxin any.txtfile (case-sensitive).
E) Recursive search (search through folders):
grep -Rni "beta" .
-R= recursive,-n= show line numbers,-i= ignore case
F) Show only the filenames that match:
grep -Rl "hello" .
G) Regex (advanced but useful):
Match Hello or HELLO or hello without -i, using an extended regex:
grep -En "(?i)hello" *.txt 2>/dev/null || grep -En "(H|h)ELLO|Hello|hello" *.txt 2>/dev/null
Note: The
(?i)flag is not supported in allgrepversions; the fallback demonstrates alternation with-E. Beginners can skip this for now.
H) Invert match (show lines that do not match):
grep -v "hello" hello.txt
3) Find files/folders anywhere with find
find walks directories and lets you filter by name, type, size, time, and more.
A) Find by name:
find . -name "hello.txt"
find . -name "*.txt"
B) Only files vs. only directories:
find . -type f -name "*.txt"
find . -type d -name "logs"
C) Limit depth to current folder only:
find . -maxdepth 1 -type f -name "*.txt"
D) Find by size (files larger than 1 megabyte):
find . -type f -size +1M
E) Find by modified time (edited within last 1 day):
find . -type f -mtime -1
F) Do something with each result (-exec):
Show file sizes for all .txt files:
find . -type f -name "*.txt" -exec wc -l {} \;
wc -lcounts lines;{}is replaced by each found file;\;ends the-execcommand.
G) Safer piping with null terminators (handles spaces in filenames):
find . -type f -name "*.txt" -print0 | xargs -0 grep -n "hello"
- This combination searches all
.txtfiles forhello, even if filenames contain spaces.
4) Combine tools with the pipe |
A) Quick counts (how many matches?):
grep -Rni "hello" . | wc -l
wc -lcounts how many matching lines were found.
B) Sort and unique (e.g., see unique matching filenames):
grep -Rl "hello" . | sort | uniq
C) Show the 5 most common words in a file (simple demo):
tr -cs '[:alnum:]' '\n' < hello.txt | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -nr | head -n 5
- Splits text into words, lowercases, counts, and shows top 5.
Note: The above is a mini data-processing pipeline — not required for beginners but fun to see what’s possible.
5) Practice tasks (do them now)
- Show the first 2 lines of
logs/mixed.txt. - Show only the lines that contain “Gamma” (any case) in
logs/mixed.txt. - Find all
.txtfiles in this folder only (not subfolders). - Count how many lines in all
.txtfiles mention “hello” (any case). - Show the filenames only that contain the word “Linux” (case-insensitive) anywhere under the current folder.
Hints:
head -n 2 logs/mixed.txt
grep -i "gamma" logs/mixed.txt
find . -maxdepth 1 -type f -name "*.txt"
grep -Rni "hello" . | wc -l
grep -Rli "linux" .
Troubleshooting
Q: grep: *.txt: No such file or directory
A: Wildcards expand before grep runs. If nothing matches *.txt, your shell might pass the literal string. Make sure you’re in the right folder and that .txt files exist.
Q: My search is case-sensitive, but I need any case.
A: Add -i to grep (e.g., grep -i "hello").
Q: find prints “Permission denied”.
A: You’re walking directories you don’t own (outside your home). For practice, stay inside ~/playground.
Q: less won’t quit.
A: Press q to exit less.
Quick Quiz (1 minute)
- Which command opens a scrollable viewer?
- How do you search recursively and ignore case with
grep? - How do you show only filenames that match?
- Which
findoption restricts search to current directory only? - What does
wc -ldo when piped aftergrep?
Answers: less; grep -Rni "term" .; grep -Rl "term" .; -maxdepth 1; counts matching lines.
Next Step
Continue to Step 4 — Process Management to learn how to view, sort, and control running programs.