Learn › Linux Foundations › Reading Files
Part 1 of 2: cat, cat -n, tac, head, tail, the less and more pagers, and wc (Sections 5.1 to 5.7). No machine test here; it completes on the final step. The chapter lab waits at the end of Part 2.
This chapter covers the following topics:
This is Part 1 of 2. It covers Sections 5.1 to 5.7, the tools that read and measure text. Part 2 covers Sections 5.8 to 5.13, the tools that identify, measure and dump any file, and it ends in the chapter lab, which covers all thirteen sections.
What you already know: Chapter 2 taught you to list a directory with ls and to move with cd, and Chapter 4 used cat to show the system files in /etc. This part is about the files themselves: how to print one in whole or in part, how to read a long one screen by screen, and how to measure one without reading it.
Objectives
After this part you can:
Before you start a machine, each exercise shows a rehearsal terminal. It checks the requested command against an example and does not change Linux files. Start one real practice machine and use its admin@server1 shell across these steps. Commands run there, and a wrong command still runs even if the lesson does not mark the exercise complete.
This part finishes at its final step. The separate final Test Room comes at the end of Part 2, uses a Debian 12 server prepared for this chapter, and checks its objectives automatically as you work.
Five questions before the material. A wrong answer costs nothing: the explanation states the fact and names the section that teaches it.
>>> A text file is defined by its contents, not by its name or its size: bytes that stand for characters, divided into lines by a line-break character. Configuration files, logs, scripts and web pages are all text. Section 5.1.
>>> Logs grow at the end, so the newest record is the last line. A tool that prints only the last lines answers the question without printing the rest. Section 5.4.
>>> The colon is the prompt of less, a pager: a program that shows a file one screen at a time and waits for a key. q quits it. Enter moves down one line, and neither Ctrl-C nor Esc leaves less. Section 5.5.
>>> tac is cat with the line order reversed. The lines themselves are printed unchanged; only their order turns around. Section 5.2.
>>> wc stands for word count and reports lines, words and bytes, in that order, then the file name. One option asks for one of the three alone. Section 5.7.
An ordinary file stores a sequence of bytes, such as the text in this lesson's log. Chapter 4 also showed virtual files under /proc and /sys: the kernel produces their contents when you read them. A byte is the unit a file is measured in: one byte holds a number from 0 to 255, and a file is a row of them. A text file has bytes that stand for characters, divided into lines by a line-break character you never see. Configuration files, logs, scripts, notes and web pages are often text files.
cat prints a file to the terminal from its first line to its last and then returns you to the prompt. The name is short for concatenate, which means to join end to end: given several files, cat prints them one after another as one stream. Given one file, it prints that file.
Print the file that describes this machine. You are in your home directory, and README.txt is in it:
The pattern is cat FILE, where FILE means a path you supply, not a word to type literally. The worked command below is cat README.txt. Use cat for a short text file; for a long file, this chapter will teach the less pager so you can read a screen at a time.
prompt: admin@server1:~$ answer: cat README.txt ||| cat ~/README.txt ||| cat ./README.txt output: server1: practice server for the Linux Foundations track ======================================================= You are logged in as admin, an ordinary user account. Your home directory is /home/admin. It holds the working files of a small organization's web server, laid out the way such a server usually is:
website/ the site: pages, assets/, releases/ and a current -> link logs/ web server and system logs data/ CSV exports backups/ compressed backups docs/ runbook, onboarding notes, meeting notes scripts/ maintenance scripts archive/ quarterly summaries for 2024 and 2025
The lesson panel records your completed steps. When account sync succeeds, it saves those records to your account. Files and shell state on this practice machine are disposable: each new machine starts with fresh copies of the teaching files. A separate chapter assessment reports its own result. hint: Section 5.1. The printing command, a space, then the file name README.txt (or ~/README.txt from anywhere).
The file is short, and the prompt is back after cat prints it. On a narrow terminal, its lines may wrap and take more than one screen. Use cat for short files you want to print all at once. Long file names are typed with the Tab key from Section 2.8: cat REA and Tab completes the rest.
This chapter adds one directory to the server, ~/reference, with material the administrator consults. You are in your home directory. List reference with ls from Chapter 2:
prompt: admin@server1:~$ answer: ls reference ||| ls reference/ ||| ls ~/reference ||| ls ~/reference/ output: changelog.txt http-status-codes.txt maintenance-log.txt samples hint: Section 2.2. The listing command from Chapter 2, then the directory name reference (or ~/reference from anywhere).
Three text files and one directory, samples, which Part 2 opens. changelog.txt is a list of the website releases, maintenance-log.txt is a numbered record of weekly maintenance, and http-status-codes.txt is a table of the codes a web server answers with. Every exercise in this part reads one of these or one of the files Chapter 2 listed.
You are in your home directory. Print the changelog at reference/changelog.txt. The path is relative to where you stand, as in Section 2.5, and ~/reference/changelog.txt names the same file from anywhere:
prompt: admin@server1:~$ answer: cat reference/changelog.txt ||| cat ~/reference/changelog.txt output: 1.2.0 2026-08-01 Dated release directories with a current link. 1.1.0 2026-07-15 Contact page with opening hours. 1.0.0 2026-06-01 First public release. 0.9.0 2026-05-20 Stylesheet moved to assets/css. 0.8.0 2026-05-06 Logo added as an SVG. 0.7.0 2026-04-22 robots.txt added. 0.6.0 2026-04-08 About page. 0.5.0 2026-03-25 Copyright line generated by app.js. 0.4.0 2026-03-11 Pages moved under a single index. 0.3.0 2026-02-25 Deploy script created. 0.2.0 2026-02-11 Site placed under version control. 0.1.0 2026-01-28 Project started. hint: Section 5.1. The printing command, then the path reference/changelog.txt (or ~/reference/changelog.txt from anywhere).
Twelve lines, one release per line: the version, the date, what changed. The newest release, 1.2.0, is the first line and the oldest, 0.1.0, is the last. Changelogs are usually written this way, newest first, so the current state is at the top. Section 5.2 prints the same file in time order.
Give cat two files and it prints the first, then the second, with nothing between them. This is the concatenation the name refers to. You are in your home directory. Print docs/style-guide.md and then docs/onboarding.md:
prompt: admin@server1:~$ answer: cat docs/style-guide.md docs/onboarding.md ||| cat ~/docs/style-guide.md ~/docs/onboarding.md output:
admin. It is an ordinary user; sudo -l lists its rules.latest link points at thebackup volume, which is mounted only on the backup host. hint: Section 5.1. The printing command, then both paths in that order: docs/style-guide.md then docs/onboarding.md (or ~/docs/style-guide.md ~/docs/onboarding.md from anywhere).
The style guide ends at its last item and the onboarding note begins on the next line. There is no separator and no file name: cat joins the contents and nothing else. Chapter 11 shows how to send such a stream into a new file instead of the screen, which is how files are combined.
People talk about files by line number: the stylesheet is loaded on line 6, the error is on line 42. The -n option, number, prints each line with its number in front. You are in your home directory. Print the home page of the website, website/index.html, with line numbers:
prompt: admin@server1:~$ answer: cat -n website/index.html ||| cat -n ~/website/index.html ||| cat website/index.html -n output: 1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Northwind Bakery</title> 6 <link rel="stylesheet" href="assets/css/site.css"> 7 </head> 8 <body> 9 <h1>Northwind Bakery</h1> 10 <p>Fresh bread and pastry, baked every morning since 2009.</p> 11 <p><a href="about.html">About</a> | <a href="contact.html">Contact</a></p> 12 <script src="assets/js/app.js"></script> 13 </body> 14 </html> hint: Section 5.1. The numbering option is -n, a dash and the letter n, and it goes between the command and the path website/index.html (or ~/website/index.html from anywhere).
Each line now starts with its number, right-aligned in a field six characters wide, then a tab, then the line as it is in the file. The numbers are not in the file; cat adds them as it prints. Line 6 loads the stylesheet, line 12 loads the script. When an error message names a line number, this is how you find the line.
cat prints everything, whatever the length. You are in your home directory. Print the maintenance log at reference/maintenance-log.txt, which has 120 lines:
prompt: admin@server1:~$ answer: cat reference/maintenance-log.txt ||| cat ~/reference/maintenance-log.txt output: Entry 001 2024-W01 Rotated the web server logs and compressed the previous set. Entry 002 2024-W02 Applied the pending security updates and rebooted. Entry 003 2024-W03 Verified the nightly backup by restoring one file from it. ... Entry 118 2026-W14 Removed release directories older than ninety days. Entry 119 2026-W15 Compared the cron schedule with the runbook. Entry 120 2026-W16 Reviewed the user accounts and groups. hint: Section 5.1. The printing command, then the path reference/maintenance-log.txt (or ~/reference/maintenance-log.txt from anywhere).
On the real machine 120 lines scroll past in an instant, and the last screen is what remains in view. The middle is shown here as a lone ... line. Every entry has a number, a week and the work done that week. The file is longer than a screen, and cat has no way to stop. The next sections are the tools for exactly this case: the first lines, the last lines, or one screen at a time.
tac is cat spelled backwards, and it prints a file with its last line first and its first line last. The lines themselves are unchanged; only their order turns around. It is the tool for a file stored newest first when you want to read it in time order. It is also the tool for a log when you want the newest line at the top of your screen.
You are in your home directory. Print the changelog from Exercise 5-3, reference/changelog.txt, oldest release first:
prompt: admin@server1:~$ answer: tac reference/changelog.txt ||| tac ~/reference/changelog.txt output: 0.1.0 2026-01-28 Project started. 0.2.0 2026-02-11 Site placed under version control. 0.3.0 2026-02-25 Deploy script created. 0.4.0 2026-03-11 Pages moved under a single index. 0.5.0 2026-03-25 Copyright line generated by app.js. 0.6.0 2026-04-08 About page. 0.7.0 2026-04-22 robots.txt added. 0.8.0 2026-05-06 Logo added as an SVG. 0.9.0 2026-05-20 Stylesheet moved to assets/css. 1.0.0 2026-06-01 First public release. 1.1.0 2026-07-15 Contact page with opening hours. 1.2.0 2026-08-01 Dated release directories with a current link. hint: Section 5.2. The reversing command, cat spelled backwards, then the path reference/changelog.txt (or ~/reference/changelog.txt from anywhere).
The project start, 0.1.0, is now the first line and the current release, 1.2.0, the last: the history of the site in the order it happened. The file on disk is unchanged. No tool in this chapter changes a file; every one of them only reads.
head prints the first lines of a file and stops. With no option it prints ten. The first ten lines tell you what a file is: its header, its format, how each line is laid out. Running head on an unknown file before running cat on it is the habit that keeps long files from filling your screen. You are in your home directory. Run it on reference/maintenance-log.txt:
prompt: admin@server1:~$ answer: head reference/maintenance-log.txt ||| head ~/reference/maintenance-log.txt output: Entry 001 2024-W01 Rotated the web server logs and compressed the previous set. Entry 002 2024-W02 Applied the pending security updates and rebooted. Entry 003 2024-W03 Verified the nightly backup by restoring one file from it. Entry 004 2024-W04 Checked disk usage on / and /home. Entry 005 2024-W05 Renewed the TLS certificate. Entry 006 2024-W06 Reviewed the failed logins in auth.log. Entry 007 2024-W07 Cleared the package cache. Entry 008 2024-W08 Ran the health check script by hand. Entry 009 2024-W09 Deployed the working copy to a new release directory. Entry 010 2024-W10 Removed release directories older than ninety days. hint: Section 5.3. The command for the start of a file, no option, then the path reference/maintenance-log.txt (or ~/reference/maintenance-log.txt from anywhere).
Entries 001 to 010, the first ten weeks of 2024, and the prompt returns. Ten is the default, the value head uses when no count is given; the next exercise gives one.
The -n option, followed by a number, sets how many lines head prints. You are in your home directory. Print the first three lines of reference/maintenance-log.txt:
prompt: admin@server1:~$ answer: head -n 3 reference/maintenance-log.txt ||| head -n3 reference/maintenance-log.txt ||| head -3 reference/maintenance-log.txt ||| head -n 3 ~/reference/maintenance-log.txt output: Entry 001 2024-W01 Rotated the web server logs and compressed the previous set. Entry 002 2024-W02 Applied the pending security updates and rebooted. Entry 003 2024-W03 Verified the nightly backup by restoring one file from it. hint: Section 5.3. The count option is -n followed by a number, and the number here is 3, then the path reference/maintenance-log.txt (or ~/reference/maintenance-log.txt from anywhere).
Three file lines: entries 001, 002 and 003. A narrow terminal may wrap one file line onto two screen rows, but head -n 3 still prints only three lines from the file. -n 3, -n 20, -n 100: any number works. The older spelling head -3 still works on Linux, and you will see it in scripts; -n 3 is the form to write.
The web server log in logs/access.log has ten thousand lines. Three are enough to learn its layout. You are in your home directory:
prompt: admin@server1:~$ answer: head -n 3 logs/access.log ||| head -n3 logs/access.log ||| head -3 logs/access.log ||| head -n 3 ~/logs/access.log output: 203.0.113.0 - - [15/Jan/2026:00:00:00 +0000] "GET /page/0 HTTP/1.1" 200 120 203.0.113.1 - - [15/Jan/2026:07:13:29 +0000] "GET /page/1 HTTP/1.1" 200 121 203.0.113.2 - - [15/Jan/2026:14:26:58 +0000] "GET /page/2 HTTP/1.1" 200 122 hint: Section 5.3. The same command and count option as the last exercise, the number 3, then the path logs/access.log (or ~/logs/access.log from anywhere).
One request per line, in the order they arrived. Each line holds the address the request came from, then two dashes for fields this server leaves empty, then the date and time in brackets. After that come the request in quotes, the status code the server answered with, and the number of bytes it sent. The code 200 means OK; reference/http-status-codes.txt lists the others, and Section 5.6 pages through it. Chapter 13 teaches the tools that pull one field out of lines like these.
tail is the mirror of head: it prints the last lines of a file, ten by default. Logs and records grow at the end, so the last lines are the newest, and tail is the fastest answer to the question of what happened most recently. You are in your home directory. Run it on reference/maintenance-log.txt:
prompt: admin@server1:~$ answer: tail reference/maintenance-log.txt ||| tail ~/reference/maintenance-log.txt output: Entry 111 2026-W07 Verified the nightly backup by
Practice Chapter 5, Part 1. Reading Files: cat, tac, head, tail, less, more and wc in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.