Why check a file’s size before reading it?
cat prints a whole file at once. For a short note that’s perfect. For a log with 50,000 lines it floods the terminal, scrolls everything useful out of sight and can take a while to finish. A quick look at the size first lets you pick the right tool.
wc -l counts lines: wc -l sightings.txt prints 97 sightings.txt. Under about 40 lines, cat is fine. A few hundred, use less or read part with head and tail. Thousands, go straight to head, tail or the search tools later in the course. ls -lh gives the file’s size when line count doesn’t tell the whole story, such as a file with one enormous line.
$ wc -l sightings.txt
97 sightings.txtWhat does wc count?
wc is short for word count. On its own it prints three numbers: lines, words and bytes, then the name. -l asks for lines only, -w for words, -c for bytes. Give it several files and it adds a total row at the bottom, so wc -l *.txt counts every text file in a folder at once.
A line, to wc, is anything ending in a newline character. If the last line of a file has no newline, wc -l doesn’t count it, so a file with three visible lines can report 2. Most editors add the final newline for you; files made by some scripts and spreadsheets don’t. If a count looks one short, that’s the usual reason.
$ wc -l members.txt sightings.txt
4 members.txt
97 sightings.txt
101 totalWhen is cat the right choice?
Use cat for short files you want to see in full: a config file, a list of members, today’s specials. The output stays in the terminal, so you can scroll back to it or copy from it. cat -n adds line numbers, which helps when an error message says “line 12”.
The name comes from concatenate. cat a.txt b.txt prints both files one after the other, which becomes useful once you can save output to a file in the redirection lesson. If cat prints strange symbols and makes your terminal beep, you’ve opened a binary file such as a photo. Nothing is harmed. Type reset and press Enter on a real terminal if the display stays garbled.
$ cat -n members.txt
1 Ann Price
2 Dev Shah
3 Mo Ellis
4 Ruth OkaforHow do you scroll through a long file with less?
less sightings.txt opens the file in a viewer that fills the screen and waits. Space or Page Down moves a screen forward, b moves back, the arrow keys move a line at a time. g jumps to the top and G to the bottom. Type / then a word and Enter to search, n for the next match. q quits and puts you back at the prompt, with the screen as it was.
less doesn’t load the whole file before showing it, so it opens huge logs instantly. It never changes the file, which makes it the safe way to look at anything important. more is the older version that only scrolls forward. The practice terminal can’t show a scrolling view, so less and more print the file and add a note; on a real system, expect the full-screen viewer and remember q.
How do head and tail work?
head prints the first 10 lines of a file and tail prints the last 10. -n sets a different number: head -n 5 sightings.txt, tail -n 3 sightings.txt. The short form head -5 works too. The number must come straight after -n; tail -n sightings.txt fails with tail: invalid number of lines, because sightings.txt isn’t a number.
head is the quick way to see what a file looks like: its header row, how values are separated, whether it’s the file you think it is. tail is for anything that grows at the end, such as logs and sightings lists, where the newest lines are the interesting ones. tail -n +2 starts from line 2 and prints the rest, a neat way to skip a header row.
$ head -n 3 sightings.txt
date bird place
2026-01-03 heron Mill Pond
2026-01-03 goldfinch River Walk
$ tail -n 3 sightings.txt
2026-03-11 great tit Old Orchard
2026-03-12 robin Weir Steps
2026-03-13 coot Mill Pond

How do you watch a log as it grows?
On a real server, tail -f error.log prints the last lines and then keeps the file open, printing each new line as it’s written. It’s how people watch a website’s errors appear while they reload the page. Press Ctrl and C together to stop watching; the log itself carries on.
The practice terminal has no files that change by themselves, so it doesn’t offer -f. Remember it for when you connect to a real server later in the course.
What do the error messages mean?
cat: walks: Is a directory means you gave a folder to a command that reads files. Run ls walks and pick a file inside. No such file or directory means the name isn’t there from where you are: check spelling, capitals and the ending with ls, and your location with pwd. Permission denied means the file exists but you’re not allowed to read it, which the Permissions lesson explains.
Read the first word of the error: it names the command that complained. In tail: invalid number of lines: 'sightings.txt', it’s tail objecting to what followed -n, and it quotes the value it couldn’t use.
What do AI tools get wrong here?
Assistants often suggest cat for every file, including large logs and database dumps. If it says cat access.log, check wc -l access.log first and use tail -n 50 or less if it’s large. They also mix option styles from other systems: head -n5 works, but some suggest tail -5f or head --lines variants that differ between GNU on Linux and BSD on a Mac. When an option fails, head --help on that machine is the authority.
Watch for “open the file” instructions that assume a desktop editor on a server. Reading with less and editing with nano, covered later, work over any connection.
Field notes
- Check how long a file is with
wc -lbefore printing all of it. catsuits short files you want to see in full.headshows the start of a file andtailshows the end, 10 lines each by default.-nsets how many linesheadortailprint.
Easy to get wrong
Thousands of lines scroll past too fast to read. Count first, then read the part you need.
-n needs a number straight after it. Without one, tail reads the file name as the number and fails.
less waits for q. Ctrl+C doesn’t close it on most systems, which makes people think the terminal has frozen.
Questions people ask
Can cat damage a file?
No. cat, less, head, tail and wc only read. They never change the file, so they’re safe on anything, including live website files.
Why does less print the whole file here?
The practice terminal can’t show a full-screen viewer. On a real system less opens a scrolling view; use Space, b, / and q to move, search and quit.
How do I see line 50 of a file?
head -n 50 file | tail -n 1 takes the first 50 lines and keeps the last of those. The Pipes lesson explains the |. sed -n 50p file does it in one step and comes up in the sed lesson.
Is this the same on a Mac?
Yes for everything here. Mac head, tail, wc and less take the same everyday options. Mac wc pads its numbers with more spaces, which changes nothing.