Introduction
Welcome to the world of Linux file viewing. In the previous module, you learned how to organize files. Now, it is time to open them and read their contents.
In this lab, you will learn the three fundamental ways to look inside a file:
cat: Reads the entire file at once. Best for small notes.head: Reads just the beginning (the "headline"). Best for checking what a file is about.tail: Reads just the end. Best for checking the latest updates in a log file.
You will also learn a special trick to watch a file update in real-time.
This is an important step in Linux literacy. In earlier modules, you learned how files are organized. Now you are starting to interact with their contents. On real systems, much of administration is really the skill of reading the right text file in the right way.
View Full File (cat)
The command cat is the standard way to say "Show me everything in this file."
It works best for small files. If you use it on a massive file, the text will fly by too fast to read.
The name cat is short for concatenate, because the command was originally designed to join and print file contents. Many beginners first meet it as a viewer, but historically it was part of a broader text-processing style.
We have prepared a small file named sample.txt.
First, go to your project folder:
cd ~/project
Now, dump the file content to your screen:
cat sample.txt
You will see the entire content printed instantly:
Line 1: This is a sample file.
Line 2: It is used to demonstrate the cat command.
Line 3: The cat command displays the full content.
Line 4: This file is short enough to read easily.
Line 5: End of the sample file.
For larger files, we need better tools.
View the Beginning (head)
Sometimes you just need to check the title or the first few lines. This is where head comes in. It shows you the "head" (the beginning) of a file.
By default, head shows the first 10 lines.
That default is useful because the beginning of a file often contains context: a header, comments, column names, or the first records. In other words, head is a quick sampling tool.
Let's look at /etc/passwd. This is a long system file containing user information.
head /etc/passwd
You should see just the first 10 lines.
Customizing the View
What if you only want the top 5 lines? You can tell head exactly how many lines you want using the -n (number) flag.
head -n 5 /etc/passwd
This is a great way to "sample" a file without reading the whole thing.
View the End (tail)
If head is the start, tail is the finish. The tail command shows you the bottom lines of a file.
In Linux, logs are written sequentially. The "newest" entry is always at the bottom. So, if something goes wrong right now, the error message will be at the end.
This is why tail is so closely associated with operations work. Logs often grow by appending new lines, so the bottom of the file is the part most likely to explain a current problem.
Let's inspect the system log /var/log/syslog.
tail /var/log/syslog
You will see the last 10 entries.
Just like with head, you can restrict the number of lines. To see the last 5 events:
tail -n 5 /var/log/syslog
This is the bread-and-butter command for System Admins. When a server crashes, the first thing they type is usually tail.
Watch Live Logs (tail -f)
Static files are boring; live data is exciting. The tail command has a flag -f (which stands for follow).
When you use -f, tail waits. If a program writes a new line to that file, tail catches it and prints it instantly. It is like watching a live news ticker.
This is one of the first places where the terminal starts to feel interactive rather than static. Instead of reading a finished file, you are watching events appear as a program produces them.
Let's try this with two terminals.
Terminal 1 (The Watcher)
Start watching the log file:
tail -f ~/project/app.log
Your cursor will hang. The terminal looks "stuck," but it is actually listening.
Terminal 2 (The Actor)
Open a second terminal (click the + button next to tabs). We will write to the log:
echo "New log entry at $(date)" >> ~/project/app.log
Switch quickly back to Terminal 1. You will see the message appear!
Go back to Terminal 2 and fire off more events:
echo "Another event happened" >> ~/project/app.log
echo "System is running smoothly" >> ~/project/app.log
As soon as you hit Enter in Terminal 2, the text pops up in Terminal 1. This is how engineers monitor servers live.
To finish, go back to Terminal 1. Press Ctrl+C to stop following and return to your prompt.
Summary
Congratulations! You now have the basic toolkit for examining files in Linux.
cat: Reads the whole file. Good for small files.head: Reads the beginning. Good for quick checks.tail: Reads the end. Good for logs.tail -f: Watches a file in real-time.
These commands allow you to answer the question, "What is inside this file?"



