LESSON · 1 OF 6

cat, head, tail, and wc

cat, head, tail, and wc

Four commands do most of the work when you need to look at a text file.

cat: print the whole thing

cat prints a file's contents to the terminal.

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

Great for short files. For anything long, use one of the tools below, otherwise the terminal fills up faster than you can scroll.

head and tail: peek at either end

head prints the first 10 lines by default. tail prints the last 10. Both take -n to change the count.

bash
head /etc/os-release
head -n 3 /etc/os-release      # first 3 lines only
tail -n 5 /etc/passwd          # last 5 lines

These are perfect for log files where the interesting bit is usually near the top (startup) or bottom (most recent).

wc: count lines, words, bytes

wc counts. Most of the time you'll use -l for line count.

bash
wc -l /etc/passwd

Piped from another command, wc -l becomes "how many things did that produce":

bash
ls /etc | wc -l           # how many entries live in /etc

You already used that trick in the previous topic.

Spin up a fresh environment and practice live.
linux-devops-basic · fresh machine · ready in under a minute