Track 1 · Lesson 3 of 7
Reading and searching text
Read files with cat, head, tail and less, then search inside them quickly with grep on your Termux phone.
Files on a phone are mostly text. The terminal is the fastest way to read them. Here you build a small log file, print it in several ways, and search it for the lines that matter. These search skills are the ones you will reach for most often.
What you'll be able to do
- Print a whole file with
cat, and number its lines. - Read the start or the end of a file with
headandtail. - Page through a long file with
less. - Count lines, words and characters with
wc. - Search a file with
grep, and search a whole tree withgrep -r.
Step 1: Make a file to work with
You need a file to read. This command writes a small log file. It uses redirection, which lesson four explains in full. For now, read it as "put the text between the markers into app.log". There is no output, and the prompt returns.
$ cd ~
$ cat > app.log << 'EOF'
2026-01-16 09:01:12 INFO server started on port 8080
2026-01-16 09:02:03 WARN slow query took 3200ms
2026-01-16 09:02:44 ERROR failed to send email to user 42
2026-01-16 09:03:10 INFO user 42 logged in
2026-01-16 09:03:55 ERROR failed to send email to user 91
2026-01-16 09:06:00 INFO nightly backup finished
2026-01-16 09:06:30 WARN error rate above threshold
EOF
The file now has seven lines. Every command in this lesson reads it.
Step 2: Print a file with cat
cat prints a file to the screen. The name is short for concatenate. Its everyday job, though, is showing you what is in a file.
$ cat app.log
2026-01-16 09:01:12 INFO server started on port 8080
2026-01-16 09:02:03 WARN slow query took 3200ms
2026-01-16 09:02:44 ERROR failed to send email to user 42
2026-01-16 09:03:10 INFO user 42 logged in
2026-01-16 09:03:55 ERROR failed to send email to user 91
2026-01-16 09:06:00 INFO nightly backup finished
2026-01-16 09:06:30 WARN error rate above threshold
Add -n to print a line number in front of every line.
$ cat -n app.log
1 2026-01-16 09:01:12 INFO server started on port 8080
2 2026-01-16 09:02:03 WARN slow query took 3200ms
3 2026-01-16 09:02:44 ERROR failed to send email to user 42
4 2026-01-16 09:03:10 INFO user 42 logged in
5 2026-01-16 09:03:55 ERROR failed to send email to user 91
6 2026-01-16 09:06:00 INFO nightly backup finished
7 2026-01-16 09:06:30 WARN error rate above threshold
cat dumps the whole file with no pause. On a file of thousands of lines it floods the screen. Use head, tail or less for anything long.
Step 3: Read the start and the end
head prints the first lines of a file. The -n flag sets how many.
$ head -n 3 app.log
2026-01-16 09:01:12 INFO server started on port 8080
2026-01-16 09:02:03 WARN slow query took 3200ms
2026-01-16 09:02:44 ERROR failed to send email to user 42
tail prints the last lines instead. Reach for it when you want the newest entries in a log.
$ tail -n 2 app.log
2026-01-16 09:06:00 INFO nightly backup finished
2026-01-16 09:06:30 WARN error rate above threshold
Step 4: Page through a file with less
less opens a file in a viewer you can scroll. It does not print and quit. It waits.
$ less app.log
2026-01-16 09:01:12 INFO server started on port 8080
2026-01-16 09:02:03 WARN slow query took 3200ms
:
The colon at the bottom left is the prompt from less, waiting for a key. Press Space to move down a screen. Press b to move back up, and q to quit. Type / then a word, then press Enter to jump to the next match.
Use less whenever a file is longer than a screen. You cannot damage a file by reading it, so explore freely and press q when you are done.
Step 5: Count lines, words and characters
wc counts. With -l it counts lines.
$ wc -l app.log
7 app.log
With -w it counts words, and with -c it counts characters.
$ wc -w app.log
55 app.log
$ wc -c app.log
366 app.log
The character count is larger than you might expect. It includes every space and line break.
Step 6: Search with grep
grep prints only the lines that match a pattern. Give it a pattern, then a file.
$ grep "ERROR" app.log
2026-01-16 09:02:44 ERROR failed to send email to user 42
2026-01-16 09:03:55 ERROR failed to send email to user 91
Add -n to include the line number, so you can find the match again.
$ grep -n "WARN" app.log
2:2026-01-16 09:02:03 WARN slow query took 3200ms
7:2026-01-16 09:06:30 WARN error rate above threshold
Add -c to count the matching lines instead of printing them.
$ grep -c "INFO" app.log
3
By default grep is case sensitive, so ERROR and error are different words to it. Add -i to ignore case.
$ grep -i "error" app.log
2026-01-16 09:02:44 ERROR failed to send email to user 42
2026-01-16 09:03:55 ERROR failed to send email to user 91
2026-01-16 09:06:30 WARN error rate above threshold
That found one extra line, the one with the lowercase error. Add -v to invert the match and print the lines that do not match.
$ grep -v "INFO" app.log
2026-01-16 09:02:03 WARN slow query took 3200ms
2026-01-16 09:02:44 ERROR failed to send email to user 42
2026-01-16 09:03:55 ERROR failed to send email to user 91
2026-01-16 09:06:30 WARN error rate above threshold
Add -r to search a whole directory tree. It prints the path in front of each match.
$ grep -r "ERROR" ~
/data/data/com.termux/files/home/app.log:2026-01-16 09:02:44 ERROR failed to send email to user 42
/data/data/com.termux/files/home/app.log:2026-01-16 09:03:55 ERROR failed to send email to user 91
Step 7: A realistic search
A user reports that confirmation emails are not arriving. Search the log for the word email.
$ grep -n "email" app.log
3:2026-01-16 09:02:44 ERROR failed to send email to user 42
5:2026-01-16 09:03:55 ERROR failed to send email to user 91
Both matches are errors, and both are about sending email. That points you straight at the email code. One search narrows the field, and the next confirms the pattern.
Quick reference
| Command | What it does |
|---|---|
cat FILE | Print a whole file |
cat -n FILE | Print a file with line numbers |
head -n N FILE | Print the first N lines |
tail -n N FILE | Print the last N lines |
less FILE | Open a file in a scrollable viewer |
wc -l FILE | Count lines |
wc -w FILE | Count words |
wc -c FILE | Count characters |
grep PATTERN FILE | Print matching lines |
grep -i PATTERN FILE | Match without regard to case |
grep -n PATTERN FILE | Show line numbers with matches |
grep -c PATTERN FILE | Count matching lines |
grep -v PATTERN FILE | Print lines that do not match |
grep -r PATTERN DIR | Search a whole directory tree |
Next steps
Now learn to link these commands into a single line in Pipes and redirection. For more practice finding things, revisit Navigating the filesystem. Then take on a real investigation in Logs Never Lie.
Test yourself on this
4 questions and 1 task where you run a real command, with the reasoning explained either way.