Reading and searching files
cat, head, tail, less, wc, grep and find: how to read a file, count it, and hunt for the one line you need. Part 3 of the Terminal Basics series.
By the end of this tutorial you will know how to:
- print a whole file (
cat), or just its start or end (head,tail) - read a long file one screen at a time (
less) - count lines and words (
wc) - search inside files for a word or phrase (
grep) - find files by name (
find), and why that is a different job fromgrep
Prerequisites: you can move around with cd and list with ls (part 1). Part 2 helps but is not required.
How this was checked: every command was run on Ubuntu 24.04 with bash 5.2, GNU coreutils 9.4, GNU grep 3.11 and GNU findutils 4.9, in a real terminal session as an ordinary (non-admin) user, and the output shown is the real output. The one exception is less: it takes over the whole terminal window, so it cannot be captured, and its keys are described rather than shown. Your username will differ from ours (gerald).
To follow along: type each command and press Enter. Everything here only reads files, so nothing in this post can break the mansion.
1. You rarely "open" files
On a desktop you double-click a file and a program opens it. In the terminal you do something more direct: you print it, slice it, or search it, and the terminal shows you only the part you asked for.
That sounds like a downgrade until you meet a log file with two million lines. A window you can scroll is no use for that. A command that says "show me only the lines that mention the fridge screaming" is exactly what you want.
This post is the toolkit for that: printing, slicing, counting and searching. All of it only reads, so the dead stay dead and nothing gets worse.
2. Rebuild the mansion
Start from a fresh mansion with the setup script from part 1 (the whole script is in that post if you do not have it):
bash ~/mansion-setup.shThe mansion is built. Try not to wake anyone.cd ~/mansion3. cat: print a file
cat prints a file's contents to the screen. Its name comes from "concatenate", for reasons that will make sense in a moment:
cat readme.txtWelcome to the mansion.
Please do not wake the guests.Give it several files and it prints them one after another, joined together, which is where the name comes from:
cat readme.txt kitchen/recipe.txtWelcome to the mansion.
Please do not wake the guests.
Soup: water, onion, bad decisions.cat prints everything, all at once. That is perfect for a two-line file and unwise for a two-million-line one, so the next tools slice things up.
4. head and tail: start and end
head prints the first lines of a file and tail prints the last. Use -n to say how many lines. First, the diary:
cat library/diary.txtOct 29: The wallpaper is humming again.
Oct 30: Heard footsteps in the attic. I live alone.
Oct 31: Ghost in the kitchen. Ghost in the cellar. Ghost in the mirror.
Nov 01: Hired an exterminator. He left screaming.
Nov 02: The ghost left a review. One star.
Nov 03: The ghost wants the wifi password.
Nov 04: Gave the ghost the wifi password.
Nov 05: The wifi is slower now.Now just its first three lines, then its last two:
head -n 3 library/diary.txt
tail -n 2 library/diary.txtOct 29: The wallpaper is humming again.
Oct 30: Heard footsteps in the attic. I live alone.
Oct 31: Ghost in the kitchen. Ghost in the cellar. Ghost in the mirror.
Nov 04: Gave the ghost the wifi password.
Nov 05: The wifi is slower now.With no -n, both default to ten lines. The kitchen's complaint log has twelve, so head leaves two of them off:
head kitchen/complaints.log2026-10-30 21:04 INFO the kettle whistled a tune
2026-10-30 22:15 WARN the oven is warm and nobody turned it on
2026-10-31 00:02 ERROR the fridge is screaming
2026-10-31 01:47 WARN the spoons are rearranging themselves
2026-10-31 03:12 ERROR the stove door opened by itself
2026-10-31 03:13 ERROR the stove door closed by itself
2026-10-31 04:30 INFO breakfast was served to nobody
2026-11-01 02:20 WARN the toaster whispered a name
2026-11-01 02:21 ERROR the toaster named a name
2026-11-01 07:00 INFO the kettle whistled a tunetail is the one you will use most, because the newest entries in a log are at the bottom:
tail -n 3 kitchen/complaints.log2026-11-01 07:00 INFO the kettle whistled a tune
2026-11-02 03:00 ERROR the fridge is screaming
2026-11-02 03:05 INFO the fridge stopped screaming5. less: read a long file
For a file that is too long to read in one go, there is less. It shows one screenful at a time and lets you move around. You start it like this:
less kitchen/complaints.logless takes over your whole terminal window, so we cannot show its output here, but these are the keys to know:
| Key | What it does |
|---|---|
| Space | Down one screen |
b | Back up one screen |
| Up and down arrows | One line at a time |
/ then a word, then Enter | Search for the word |
n | Jump to the next match |
q | Quit, and get your terminal back |
The key to remember is q. If you ever run less and nothing seems to work, press q. It is the exit door.
6. wc: count things
wc stands for word count, though it counts lines and bytes too. With -l it counts lines:
wc -l library/diary.txt
wc -l kitchen/complaints.log8 library/diary.txt
12 kitchen/complaints.logWith no options it gives three numbers: lines, words and bytes.
wc library/diary.txt 8 71 374 library/diary.txtThe diary has 8 lines and the complaint log has 12. If your numbers differ, rebuild the mansion with bash ~/mansion-setup.sh and try again.
7. grep: search inside files
Now the star. grep prints only the lines that contain a word or phrase. Give it the thing to look for, then the file to look in. Find the diary entries that mention the ghost:
grep ghost library/diary.txtNov 02: The ghost left a review. One star.
Nov 03: The ghost wants the wifi password.
Nov 04: Gave the ghost the wifi password.Three lines. But the diary has another entry, on Oct 31, that says "Ghost" with a capital G. grep is case-sensitive, so it skipped that one. The -i option makes it ignore case:
grep -i ghost library/diary.txtOct 31: Ghost in the kitchen. Ghost in the cellar. Ghost in the mirror.
Nov 02: The ghost left a review. One star.
Nov 03: The ghost wants the wifi password.
Nov 04: Gave the ghost the wifi password.Four lines this time. Notice the Oct 31 entry joined the list.
There are a handful of options worth knowing. -n adds line numbers, so you can find the line again later:
grep -n -i ghost library/diary.txt3:Oct 31: Ghost in the kitchen. Ghost in the cellar. Ghost in the mirror.
5:Nov 02: The ghost left a review. One star.
6:Nov 03: The ghost wants the wifi password.
7:Nov 04: Gave the ghost the wifi password.-c counts matching lines instead of printing them:
grep -c ERROR kitchen/complaints.log5And -v inverts the search, printing every line that does not match. These are the diary lines with no ghost in them:
grep -v -i ghost library/diary.txtOct 29: The wallpaper is humming again.
Oct 30: Heard footsteps in the attic. I live alone.
Nov 01: Hired an exterminator. He left screaming.
Nov 05: The wifi is slower now.Phrases need quotes
If what you are looking for has a space in it, wrap it in quotes. Otherwise the terminal treats the words as separate things:
grep "stove door" kitchen/complaints.log2026-10-31 03:12 ERROR the stove door opened by itself
2026-10-31 03:13 ERROR the stove door closed by itselfSearching a whole folder
grep -r (for recursive) searches every file in a directory and everything below it. The dot at the end means "starting from here". Each match is shown with the file it came from:
grep -r screaming ../kitchen/complaints.log:2026-10-31 00:02 ERROR the fridge is screaming
./kitchen/complaints.log:2026-11-02 03:00 ERROR the fridge is screaming
./kitchen/complaints.log:2026-11-02 03:05 INFO the fridge stopped screaming
./library/diary.txt:Nov 01: Hired an exterminator. He left screaming.Now search for something that is not there:
grep -i zombie library/diary.txtThat printed nothing at all. When a search finds no match, grep does not say so. It stays silent, which is easy to mistake for the command not working. Silence means no match.
8. find: look for files by name
grep searches inside files. find searches for the files themselves, by name. If you know a file exists somewhere in the mansion but not where, ask find. Give it where to start (a dot means here) and what to match:
find . -name "love-letters.txt"./attic/trunk/love-letters.txtIt found the file and printed its path. You can use a wildcard in the name, so this finds anything starting with wine:
find . -name "wine*"./cellar/wine-list.txtAdd -type d to find only directories, or -type f for only files. find lists things in no particular order, so we add | sort at the end to put the list in alphabetical order. (The | is a pipe, which part 5 explains. We use it here only so your list matches ours.)
find . -type d | sort.
./attic
./attic/trunk
./cellar
./kitchen
./libraryAsk yourself what you are looking for. If you know what is written in the file but not where it is, use grep -r. If you know what the file is called but not where it is, use find. Searching contents is grep. Searching names is find.
9. Common errors
| What you see | What it usually means | Fix |
|---|---|---|
No such file or directory | The file name is wrong, or you are in the wrong directory | Check the spelling and pwd |
Nothing at all from grep | There was no match (not an error) | Check the spelling, and try -i |
A grep: ...: No such file or directory plus odd matches | A phrase with a space was not in quotes | Wrap the phrase in quotes |
Here are two of them in action. First, a file that does not exist:
cat nope.txtcat: nope.txt: No such file or directoryAnd a phrase left without quotes. grep treats stove as the thing to find, then treats door as the name of a file:
grep stove door kitchen/complaints.loggrep: door: No such file or directory
kitchen/complaints.log:2026-10-31 03:12 ERROR the stove door opened by itself
kitchen/complaints.log:2026-10-31 03:13 ERROR the stove door closed by itselfPractice
Predict the result before you run each one. Answers follow.
Exercise 1. How many lines in kitchen/complaints.log say WARN?
Exercise 2. Which line numbers in the diary mention the wifi, whatever the capitalisation?
Exercise 3. Find the file called vault.sh somewhere in the mansion.
Answers
Exercise 1. -c counts the matching lines:
grep -c WARN kitchen/complaints.log3Exercise 2. -n for line numbers, -i to ignore capitalisation:
grep -n -i wifi library/diary.txt6:Nov 03: The ghost wants the wifi password.
7:Nov 04: Gave the ghost the wifi password.
8:Nov 05: The wifi is slower now.Exercise 3. You are looking for a file by name, so reach for find:
find . -name "vault.sh"./cellar/vault.shWhat you learned
catprints a whole file,headandtailprint the start and end, andlessreads a long file one screen at a time (leave it withq).wc -lcounts lines.grepprints the lines that contain a word.-iignores case,-nadds line numbers,-ccounts,-vinverts, and-rsearches a whole directory. Phrases with spaces need quotes.findsearches for files by name.grepsearches inside them.- Silence from
grepmeans no match, not a failure.
Next in the series: Permissions and sudo. You will learn why the terminal sometimes says "Permission denied", who is allowed to do what, and what sudo really does.
Video walkthrough
YouTubeClick play to load video (YouTube)