Learn by reading through in order

Viewing Contents — cat / head / tail / wc

Show the whole file with cat, the start and end with head and tail, and count lines with wc -l — plus the log-checking flow, hands-on in a browser terminal.

Show the Whole File — cat

cat is the command that prints a file's entire contents to the screen.

Use it when you want to see the whole thing at a glance, like a short config file or memo.

Just pass the filename and it outputs from the first line to the last as-is.

For very long files, on Ubuntu and similar you'd use less to scroll one screen at a time and q to quit.

In this course's console you display the whole file with cat, and for long files you take just the part you need with the head and tail you'll learn next.

Ways to view a file's contents
cat file.txtShow every line as-ishead / tailTake just the start / endwc -l file.txtCount the lines
Use cat for the whole, head / tail for parts, and wc -l for the amount.
echo one > memo.txt      # write line 1
echo two >> memo.txt     # append line 2
echo three >> memo.txt   # append line 3
cat memo.txt             # show every line

① Write line 1 with echo a > sample.txt, then append with echo b >> sample.txt and echo c >> sample.txt to make a 3-line file.

② Show the whole contents with cat sample.txt and confirm the three lines a b c appear. (Run it correctly and an explanation will appear.)

Linux console
0 / 4 completed
Loading Linux Terminal...

View the Start — head

head takes just the given number of lines from the start of a file.

With no option it shows the first 10 lines, and writing a number after -n limits it to that many.

It's handy for checking the beginning of a long log or the first few records of data.

UsageMeaning
head fShow the first 10 lines (default)
head -n N fShow the first N lines
How head works
head fShow first 10 lines(default)head -n 2 fShow just thefirst 2 linesBeginning of a long logCheck just the startwithout reading all
head shows from the start. Use -n to set the number of lines.
echo l1 > log.txt     # create material
echo l2 >> log.txt
echo l3 >> log.txt
head -n 2 log.txt     # first 2 lines (l1 l2)

① Create a 3-line file with echo a > data.txt, echo b >> data.txt, and echo c >> data.txt.

② Run head on data.txt with no -n and confirm it shows the first 10 lines by default (all 3 lines, since the file has 3).

③ Then show just the first 2 lines with head (write the line count after -n, with a space).

Linux console
0 / 4 completed
Loading Linux Terminal...

View the End — tail

tail takes just the given number of lines from the end of a file.

With no option it shows the last 10 lines, and -n sets the number of lines.

It's often used to check the latest lines of a log.

UsageMeaning
tail fShow the last 10 lines (default)
tail -n N fShow the last N lines
How tail works
tail fShow last 10 lines(default)tail -n 2 fShow just thelast 2 linesLatest lines of a logQuickly checkjust the end
tail shows from the end. It's good for checking the latest lines.
echo l1 > log.txt     # create material
echo l2 >> log.txt
echo l3 >> log.txt
tail -n 2 log.txt     # last 2 lines (l2 l3)

① Create a 3-line file with echo a > data.txt, echo b >> data.txt, and echo c >> data.txt.

② Show just the last 2 lines with tail (write the line count after -n, with a space).

Linux console
0 / 4 completed
Loading Linux Terminal...

Count Lines — wc

wc is the command that counts words and lines.

Add -l to show just the line count of a file.

You can quickly see how many lines a log has piled up or how many records there are without reading the whole thing.

UsageMeaning
wc fShow line, word, and character counts
wc -l fShow just the line count
wc -w fShow just the word count
How wc works
wc fLines, words, characterswc -l fShow just the line countGrasping the countCount withoutreading the contents
wc counts. Use -l to take just the line count.
echo a > data.txt     # create material
echo b >> data.txt
echo c >> data.txt
wc -l data.txt        # line count (3)

① Create a 3-line file with echo a > data.txt, echo b >> data.txt, and echo c >> data.txt.

② Use wc with the line option to show how many lines data.txt has (add the option that means 'line').

Linux console
0 / 4 completed
Loading Linux Terminal...

Specify the line count with `-n`

When you know how many lines you want with head or tail, specifying it with -n takes exactly the part you want.

QUIZ

Knowledge Check

Answer each question one by one.

Q1Which command shows a file's contents from start to end all at once?

Q2Which do you use to show just the last few lines of a file?

Q3What does running wc -l data.txt tell you?