LearnLinux FoundationsReading Files

wc

wc - a hands-on Linux lab on a real virtual machine.

Measure any file with wc: lines, words, and bytes in one shot, or isolate one with -l, -w, -c, or -m. Meet the missing-newline trap where wc -l returns 0, all in the practice terminal on the intercept files.

You already have commands that read a file's contents. cat prints the whole thing, head shows the top, tail shows the bottom. Each is its own lesson in this module. This lesson is about a different question. Not *what does the file say*, but *how big is it*: how many lines, how many words, how many bytes.

That measuring job belongs to one command: wc, short for word count. Despite the name it counts more than words. Point it at a file and it reports three numbers at once, and with a flag it reports just the one you asked for. That is the whole command, and this lesson is all of it.

The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. At the end of the module one real Linux machine boots just for you, with this same intercept folder, and you run the full mission there.

You are inside a folder called intercept, a pile of captured logs and intel notes. Start by pointing plain wc at the boot log. Type this exact command and press Enter:

wc ~/intercept/logs/messages.log

prompt: student@linuxcamp:~$ answer: wc ~/intercept/logs/messages.log output: 8 35 208 /home/student/intercept/logs/messages.log hint: Type wc, a space, then the path to the log: wc ~/intercept/logs/messages.log

Three numbers and a filename. That is wc in its plainest form. The next step names each number, because the order never changes and that order is worth burning into memory.

The output you just saw was 8 35 208. Read left to right, those are always lines, then words, then bytes:

The filename comes last so you can tell files apart when you measure several at once. Lock in the order lines, words, bytes; it is the same on every file wc will ever touch.

If you only ever remember one thing about wc, remember the column order: lines, words, bytes. Every flag you are about to learn just picks one of those three columns to show on its own.

Often you do not want all three numbers, just one. Each column has a flag that shows only it. The first is -l, short for lines. wc -l <file> prints the line count and nothing else.

There is a longer file here, report.txt, which runs to twelve lines. Ask wc for just its line count:

wc -l ~/intercept/logs/report.txt

prompt: student@linuxcamp:~$ answer: wc -l ~/intercept/logs/report.txt output: 12 /home/student/intercept/logs/report.txt hint: Add -l between wc and the path: wc -l ~/intercept/logs/report.txt

One number, 12, then the filename. No word count, no byte count, just the lines. -l is the flag you will reach for most, because "how many lines is this?" is the most common thing you ask a file. Counting lines is how you check how many records are in a list, how many entries a log has, or how many results a search returned.

The other two columns have their own flags, and they work exactly like -l. -w is short for words and prints only the word count. -c is short for count of bytes and prints only the byte count.

There is a small roster file, roster.txt, in the intel folder. Ask how many words are in it:

wc -w ~/intercept/intel/roster.txt

prompt: student@linuxcamp:~$ answer: wc -w ~/intercept/intel/roster.txt output: 12 /home/student/intercept/intel/roster.txt hint: Use -w for words: wc -w ~/intercept/intel/roster.txt

Twelve words. That same roster.txt has only 5 lines, so wc -l on it would print 5, not 12. Lines and words are different measurements, which is exactly why wc keeps them in separate columns. Swap -w for -c and you would get the byte count of the file instead. Same command, same file, you just pick the column.

There is a fourth flag worth knowing: -m, short for characters. It sounds identical to -c for bytes, and on plain English text the two match exactly, because each letter is stored as a single byte. They only split apart when a file holds characters that take more than one byte to store, like an accented letter or an emoji.

The file pangram.txt is one line of pure ASCII text. Count its characters with -m:

wc -m ~/intercept/intel/pangram.txt

prompt: student@linuxcamp:~$ answer: wc -m ~/intercept/intel/pangram.txt output: 44 /home/student/intercept/intel/pangram.txt hint: Use -m for characters: wc -m ~/intercept/intel/pangram.txt

Forty-four characters. Run wc -c on the very same file and you also get 44, because every character in this ASCII file is exactly one byte. That equality is the normal case for plain English. The moment a file contains a multi-byte character, -m (the human count of characters) and -c (the raw count of bytes) would disagree, and knowing which one you asked for saves confusion. For everyday ASCII, reach for whichever you like; they agree.

This is the surprise every wc user meets sooner or later, and it is better to meet it here in the sandbox. To wc, a line is not "a row of text" but "a run of text that ends in a newline character." The counting happens on the newline, not on the text. If the very last line of a file has no newline after it, wc never counts it.

The file notes.md holds one line of readable text, but it was saved with no trailing newline. Ask wc -l how many lines it sees:

wc -l ~/intercept/intel/notes.md

prompt: student@linuxcamp:~$ answer: wc -l ~/intercept/intel/notes.md output: 0 /home/student/intercept/intel/notes.md hint: Type wc, a space, -l, a space, then the path: wc -l ~/intercept/intel/notes.md

Zero lines, even though you can plainly read text in the file. That is not a bug and not an empty file. There is one line of text, but because it never ends in a newline, wc counts zero completed lines. The other counts are untouched. Run plain wc ~/intercept/intel/notes.md and it reports 0 7 35: zero lines, but seven words and thirty-five bytes. Remember this the day a line count comes back one lower than you expect. The culprit is almost always a missing final newline.

Scaffolding off. No command is shown this time. You have every piece you need.

A moment ago you counted the characters in pangram.txt with -m and got 44. Now you want a different number from the same file: its word count, and nothing else, not lines, not characters. Pick the flag that isolates words, and point wc at pangram.txt in the intel folder.

prompt: student@linuxcamp:~$ answer: wc -w ~/intercept/intel/pangram.txt output: 9 /home/student/intercept/intel/pangram.txt hint: Words only means the -w flag. Point wc -w at the pangram: wc -w ~/intercept/intel/pangram.txt

Nine words, from memory. Same file you just measured with -m, but by swapping to -w you pulled a completely different number out of it: 44 characters, 9 words. That is the whole skill: know which of the three columns you need, pick its flag, and type it without a prompt. It is exactly the move the real machine will ask of you.

You earned this cheat sheet. Every row is a form of wc you have already run:

The one rule to carry away: wc counts newlines, not rows of text. That is why a final line with no newline does not add to the -l count. The order lines, words, bytes never changes, and each flag just picks one of those columns to show alone.

wc is not only for whole files. Later in the camp you will feed it the output of other commands, and wc -l becomes the standard way to answer "how many?" How many files match, how many lines a search found, how many entries a list holds. The counting skill you built here is the same one.

The practice terminal has shown you every form of wc: the three-count default, the single-column flags -l, -w, -c, and -m, and the missing-newline trap that makes a line count come back zero. The Reading Files module ends with one real machine, and that is where you count for real.

In the reading-files capstone mission, a Linux VM boots just for you on this same intercept folder. Its objectives use exactly what you just ran: plain wc for all three counts, and the flags to isolate one. Alongside wc you will use the module's other reading commands, cat, head, tail, and the rest, each of which has its own lesson. One difference from here: the mission shows no commands. You read the objective, recall the flag, and type it. That recall is what makes it stick.

Finish the other Reading Files lessons, then go take the tally for real.

Practice wc in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.