Reading Files with cat

cat: show a file

cat prints the contents of a file straight into the terminal. It is the quickest way to look at a short file:

cat shopping.txt

In the terminal

student@vulntech:~$ cat shopping.txt
eggs
milk
bread
apples
cheese

The name comes from concatenate, which means joining things together: give cat several files and it prints them one after another:

cat shopping.txt poem.txt

In the terminal

student@vulntech:~$ cat shopping.txt poem.txt
eggs
milk
bread
apples
cheese
The terminal waits, a blinking light,
for words typed in by day or night.
A single line, a little key,
and all the files are there to see.

Line numbers with -n

cat -n poem.txt

In the terminal

student@vulntech:~$ cat -n poem.txt
     1	The terminal waits, a blinking light,
     2	for words typed in by day or night.
     3	A single line, a little key,
     4	and all the files are there to see.

Reading system files

Remember that Linux settings are plain text. cat lets you read them:

cat /etc/hostname
cat /etc/os-release

In the terminal

student@vulntech:~$ cat /etc/hostname
vulntech
student@vulntech:~$ cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.21.0
PRETTY_NAME="Alpine Linux v3.21"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

tac: backwards

tac (cat spelled backwards) prints the last line first:

tac shopping.txt

In the terminal

student@vulntech:~$ tac shopping.txt
cheese
apples
bread
milk
eggs

Common mistake: cat prints the whole file at once. On a file with thousands of lines, everything scrolls past and you only see the end. For long files, use less (next lesson).

Remember: cat file prints a file. cat -n adds line numbers.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…