Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 16

Viewing File Contents

Recommended resources

Introduction

Welcome back! In our last lesson, you took your first steps with vim, learning how to open, edit, save, and search for content within files. This is an indispensable skill for modifying configurations or writing code. However, as a system administrator or developer, you often just need to view files without the risk of accidentally changing them—checking a log file for errors, verifying a configuration setting, or quickly reading a project's README.

Today, we'll build your "read-only" toolkit. You will learn to use four essential commands—cat, less, head, and tail—to view and navigate the contents of files directly from your terminal. Mastering these commands will make you much more efficient at inspecting your system and application behavior.


Setting Up Your Practice Area

Before we dive in, let's create a dedicated space with some sample files to work with. This hands-on approach will help you see the effect of each command immediately.

Open your terminal and run these commands one by one:

  1. Create a new directory in your home folder and navigate into it:
    mkdir ~/text_viewing_practice && cd ~/text_viewing_practice
    
  2. Copy a large system file into your practice directory. The /etc/services file is a good example, as it's a long text file that maps network service names to port numbers.
    cp /etc/services .
    
  3. Create a small, simple text file.
    echo -e "First line.\nSecond line.\nThird line." > small.txt
    
  4. Generate a sample log file. System logs can be very long, so we'll grab the last 100 lines from your system's journal to create a realistic practice file.
    journalctl -n 100 > practice_log.txt
    

You should now have three files in your ~/text_viewing_practice directory: services, small.txt, and practice_log.txt. You are all set!


cat: Quick and Complete Display

The cat command (short for "concatenate") is the simplest way to view a file. It reads the entire file and prints its content to the terminal.

To see how it works, let's start with the small file you created.

View the File Contents in Linux Command Line

The article 'View the File Contents in Linux Command Line' from It's FOSS provides a clear introduction to cat. We'll use it to understand the command's basic function and its limitations.

Read the section 'Use cat command to display file content'. It's short and to the point.

Now, try it yourself:

cat small.txt

You'll see all three lines printed neatly. cat is perfect for short files.

But what happens with a large file? Try it on the services file:

cat services

As you can see, the text floods your terminal, and you can only see the very end of the file. You'd have to scroll way up to find the beginning, which is highly impractical. This problem leads us directly to our next, more powerful tool.


less: The Interactive Pager

The less command is designed specifically for viewing large files. It's a "pager," meaning it displays the file's content one page (or screen) at a time, giving you full control over navigation. It's called less as a clever nod to an older Unix command called more.

The It's FOSS article you just read introduces less as the solution to cat's shortcomings. Now, let's dive deep into how to use it effectively.

Linux Command Line Tutorial For Beginners 10 - less command

This video from ProgrammingKnowledge gives an excellent, hands-on demonstration of less, covering navigation, searching, and quitting.

Watch from the beginning to 07:15. The video will walk you through: Opening a file with less. Navigating line-by-line (arrow keys) and page-by-page (spacebar, 'b'). Jumping to the beginning ('g') and end ('G') of the file. Searching for text ('/'). Quitting less ('q'). Try the commands in your own terminal on the services file as you watch.

To summarize the key less commands:

Keys Action
Up/Down Arrow Move one line up or down.
Spacebar/PgDn Move one page down.
b/PgUp Move one page up.
g Move to the beginning of the file.
G Move to the end of the file.
/pattern Search forward for pattern. Press n for next match.
?pattern Search backward for pattern. Press n for next match.
q Quit less and return to the terminal.

Practice these now by opening your services file: less services. Try finding the entry for "http" or "ssh".


head and tail: Peeking at the Ends

Sometimes you don't need to see the whole file or even a full page. You just want a quick peek at the beginning or the end. This is where head and tail are invaluable.

  • head: Shows the first part of a file.
  • tail: Shows the last part of a file.

By default, both commands show 10 lines.

Linux Crash Course - The head and tail Commands

The 'Linux Crash Course' video on head and tail by Learn Linux TV is a perfect guide. It covers the basics, how to specify the number of lines, and a critically important feature of tail for developers.

Watch the following segments: 01:10 - 01:46: Introduction to the basic function of head and tail. 03:31 - 06:53: See head and tail in action on a log file. 08:16 - 09:24: Learn how to customize the number of lines shown using the -n option. 11:07 - 17:02: This is the most important part for you as a developer. Pay close attention to the tail -f (follow) option, which allows you to watch a file for changes in real-time. The demonstration of troubleshooting an SSH login is a classic system administration workflow.

Now, try these commands yourself on your practice_log.txt file:




# See the first 10 lines
head practice_log.txt




# See the first 5 lines
head -n 5 practice_log.txt




# See the last 10 lines
tail practice_log.txt




# See the last 15 lines
tail -n 15 practice_log.txt

Your Turn: Real-Time Log Monitoring

The tail -f command is one of the most frequently used tools for debugging applications. When your Django server is running, its logs are continuously being written to a file. Watching this file in real-time lets you see errors and requests as they happen.

Let's simulate this powerful feature. You will need two terminal windows open for this exercise.

In your first terminal:

  1. Make sure you are in the ~/text_viewing_practice directory.
  2. Run tail in "follow" mode on your sample log file. The cursor will blink, and the command will not exit. It's now watching for changes.
    tail -f practice_log.txt
    

In your second terminal:

  1. Navigate to the same directory.
    cd ~/text_viewing_practice
    
  2. Now, append a new line to the practice_log.txt file. We use echo to create the text and >> to append it to the file.
    echo "This is a new line simulating a Django app request." >> practice_log.txt
    

Look at your first terminal! The new line you just added will instantly appear. This is tail -f at work.

You can append another line to see it happen again:

echo "ERROR: Database connection failed." >> practice_log.txt

This immediate feedback loop is exactly what you'll use to debug services. To stop the tail -f command, go back to the first terminal and press Ctrl+C.


Conclusion

Great job! You've now added four fundamental commands to your Linux toolkit that complement your editing skills with vim. You've learned how to choose the right tool for the job, whether you're viewing a small config, navigating a giant log file, or monitoring activity in real-time.

Key Takeaways:

  • cat: Best for quickly displaying the entirety of small files.
  • less: The essential, interactive "pager" for viewing large files with search and navigation capabilities.
  • head: Used to view the first few lines of a file (default 10).
  • tail: Used to view the last few lines of a file (default 10).
  • tail -f: A developer's best friend. It allows you to monitor files (especially logs) for new content in real-time.

In the next lesson, we will explore one of the most powerful concepts in the command line: chaining commands together with pipes (|) and redirecting output (>, >>). You've already had a preview of this with the >> operator in today's exercise. Next, you'll learn how to build powerful command sequences, such as using head and tail together to view a specific range of lines from the middle of a file.