Displaying Entire Files with cat
The cat (concatenate) command is the simplest way to display a file's
contents. It reads the entire file and prints it to your terminal in one go.
# Display the contents of a file
cat /etc/hostname
# Display with line numbers
cat -n /etc/hosts
# Display multiple files sequentially
cat file1.txt file2.txt
Example output with line numbers:
$ cat -n /etc/hosts
1 127.0.0.1 localhost
2 127.0.1.1 mycomputer
3
4 # IPv6
5 ::1 localhost ip6-localhost ip6-loopback
cat is best for small files -- configuration files, short scripts,
or quick checks. For anything longer than a screenful of text, use less
instead, because cat will flood your terminal and scroll past too fast
to read.
Useful cat Variations
Scrolling Through Files with less
The less command opens a file in a scrollable viewer. Unlike cat,
it does not dump everything to the screen at once. Instead, it shows one screenful at a
time and lets you navigate forward and backward.
# Open a file in less
less /var/log/syslog
# Open with line numbers
less -N /var/log/syslog
Once inside less, use these keys to navigate:
System administrators and developers use less constantly for reading
log files, configuration files, and source code. The search feature (/pattern)
is especially powerful -- you can search through thousands of lines in an instant.
Peeking at the Start with head
Sometimes you only need to see the beginning of a file. The head command
prints the first lines of a file -- by default, the first 10 lines.
# Show the first 10 lines (default)
head /etc/passwd
# Show the first 5 lines
head -n 5 /etc/passwd
# Show the first 20 lines
head -n 20 /var/log/syslog
head is commonly used to quickly check what a file contains without
loading the whole thing. For example, you might use it to verify a CSV file has the
right column headers, or to check the top of a log file for startup messages.
# Check the header of a CSV file
head -n 1 data.csv
# Peek at a configuration file
head -n 15 /etc/nginx/nginx.conf
Watching the End with tail
The tail command is the opposite of head: it prints the last
lines of a file. By default, it shows the last 10 lines.
# Show the last 10 lines (default)
tail /var/log/syslog
# Show the last 20 lines
tail -n 20 /var/log/syslog
# Show the last 50 lines
tail -n 50 /var/log/auth.log
Live Log Monitoring with tail -f
The most powerful feature of tail is the -f (follow) flag.
It keeps the file open and prints new lines as they are added in real time. This is
essential for monitoring log files while troubleshooting.
# Follow a log file in real time
tail -f /var/log/syslog
# Follow the last 50 lines and continue watching
tail -n 50 -f /var/log/auth.log
While tail -f is running, every new line written to the file appears on
your screen instantly. Press Ctrl + C to stop following and
return to the terminal prompt.
System log files like /var/log/auth.log or /var/log/syslog
may not be readable by regular users. If you get a "Permission denied" error, prepend
the command with sudo: sudo tail -f /var/log/auth.log.
Counting with wc
The wc (word count) command tells you how many lines, words, and bytes
are in a file. It is a quick way to gauge the size or length of text content.
# Full count: lines, words, bytes
wc /etc/passwd
45 68 2467 /etc/passwd
# Count only lines
wc -l /etc/passwd
45 /etc/passwd
# Count only words
wc -w readme.txt
# Count only characters
wc -c data.csv
A common use case is counting lines in a log file to see how many events were recorded, or counting lines of code in a project:
# How many lines in the system log?
wc -l /var/log/syslog
# How many users on the system?
wc -l /etc/passwd
Summary
You now have a toolkit of commands for reading and inspecting files:
- cat -- Display entire files (best for short files)
- less -- Scroll through files interactively (best for large files)
- head -- View the first N lines of a file
- tail -- View the last N lines; use
-fto follow live updates - wc -- Count lines, words, and characters in a file
Choosing the right tool depends on the situation: cat for quick peeks,
less for reading, head / tail for targeted
views, and wc for measuring.
You can now read and inspect files without opening a text editor. Next up,
you will learn about Linux file permissions -- what rwx means and
how to control who can read, write, and execute your files.