Sign in
← Terminal: getting oriented
3 min · easy · Lesson 1 of 5

How many lines in a file: wc -l

Open Linux in your browser, count the users in a prepared list, and save the number.

On Linux, you rarely solve "open the file and count by eye." More often you ask the system, and it answers with a number. How many users are in a list, how many errors in a log, how many lines in an export: there's one small command for all of this, wc. This is the first lesson of the track, so let's start with it.

Where to practice

The examples below work in a ubuntu:24.04 container. You can start the same one and follow along step by step:

docker run --rm -it ubuntu:24.04 bash

--rm removes the container after exit. On Linux and in WSL2 the commands work right in your own terminal too. On macOS as well, but the output looks slightly different, more on that below.

Create the file we'll practice on:

printf 'root:x:0\nadmin:x:1\nweb:x:2\nbot:x:3\n' > /tmp/users.lst

\n here means a newline. The file has four lines, one user per line.

What wc prints

wc (word count) with no flags prints three numbers and the file name:

wc /tmp/users.lst
 4  4 35 /tmp/users.lst

That's lines, words, and bytes. Usually you need just one number, and you pick it with a flag: -l for lines, -w for words, -c for bytes.

wc -l /tmp/users.lst
4 /tmp/users.lst

How to get a single number

If you pass the file as an argument, wc tacks its name onto the number. That's handy on screen. But in a script or in a file holding the answer, the name just gets in the way: you need 4, not 4 /tmp/users.lst.

To drop the name, feed the file through input redirection <:

wc -l < /tmp/users.lst
4

The difference is who opens the file. With an argument, wc opens it itself and knows the name. With <, the shell opens the file and feeds its content to the command's input, that is, to stdin. wc sees only a stream of lines with no name attached, so there is no name to print next to the number. cat /tmp/users.lst | wc -l gives the same result.

How to save the answer to a file

Output redirection > sends whatever the command would have printed to the screen into a file instead. If the file doesn't exist, it's created; if it does, it's overwritten.

wc -l < /tmp/users.lst > /tmp/answer
cat /tmp/answer
4

You can use both < and > in one command: the first says where to read from, the second where to write to.

If you need to append to the file instead of overwriting it, use >>:

echo 'guest:x:4' >> /tmp/users.lst
wc -l < /tmp/users.lst
5

What exactly -l counts

wc -l doesn't count "lines you can see", it counts newline characters \n. Usually that's the same thing, because every line of a text file ends with \n. But an empty line is also a \n, and it gets counted:

printf 'a\nb\nc\n\n' | wc -l
4

There are three visible lines, and wc counted four. Keep this in mind when a number looks off: look at the file itself, for example with cat -A, which marks the end of every line with $.

Common mistakes

What you'll need for the tasks

Practice in your browser

Linux runs right here. Solve the task and we'll save your progress automatically.

01

How many users?

We put a list of users in /tmp/users.lst (one per line). Count how many there are and save the number to /tmp/answer.

{ i }

Go through it with an AI agent

The agent explains anything unclear in plain words, gives you 2 labs for this lesson and drills you on interview questions. $49 one-time, lifetime access.

Get access for $49

Tell the agent: use MCP izi, give me lesson terminal-basics-01-count-users