Curious how to peek into a text file from the shell? The cat command is the quick, reliable way to print a file’s contents straight to the terminal. It’s a staple in Linux, simple to remember, and often paired with other tools for filtering or paging—once you’ve seen it in action, you’ll reach for it naturally.

Multiple Choice

How can you display the contents of a text file in the terminal?

To display the contents of a text file in the terminal, the correct command is `cat filename.txt`. The `cat` command, short for "concatenate," is widely used in the Linux environment to read and display the content of files directly to the terminal. When you execute this command, the terminal reads the specified file and outputs its content to the screen, allowing users to view it quickly. The other commands listed do not serve this purpose in a standard Linux environment. For example, `show`, `display`, and `open` are not standard commands used to read text files in the terminal. They may belong to other utilities or environments, but they do not function as expected in basic Linux command line operations for displaying file contents. Thus, the adoption of `cat` makes it the most effective and widely recognized method for this task in Linux.

There’s something oddly satisfying about peeking into a text file right from your terminal. No fancy editors, no GUI fuss—just you, the command line, and whatever words someone decided to put on the screen. If you’re exploring Linux, you’ll quickly learn that there are plenty of ways to view a file’s contents. But the classic starting point is the humble cat command. It’s simple, fast, and sets a kind of baseline for how we interact with text in the shell.

Cat: the old reliable that still holds up

Cat is short for concatenate, but let’s not get tangled in the etymology. In practice, cat is the go-to tool for a quick glance at a file. You type something like:

  • cat filename.txt

And boom—the contents spill onto your terminal. It’s straightforward, no bells, no whistles. If the file is small, you’ll see everything in one go. If it’s big, you’ll scroll. That’s when you start to appreciate how the terminal can handle a lot of information without breaking a sweat.

But why does cat matter so much? Because it’s a foundation. It’s the first thing most new users try, and it’s a clean, reliable way to confirm you’re looking at the right file, the right directory, and the right permissions. It’s also a handy starting point for other workflows: you can pipe cat’s output into another command, redirect it to a file, or combine it with filters to wrangle text in real time.

Beyond the basics: when you might want something more than a straight dump

Not every file is a tidy, print-friendly page. Some files are long, some are formatted with columns, and others are streams you want to page through. That’s where other tools come into play, each offering a slightly different lens on the same content.

Less and more: paging through long files

If a file is longer than your terminal window, you’ll be grateful for a pager. Less is a common choice because it lets you move up and down through the text without losing your place. You can search within the file, scroll line by line, and even move by pages. Try:

  • less filename.txt

Things to know:

  • You can quit by pressing q.

  • / searches forward; ? searches backward.

  • Use arrows, Page Up/Page Down, or the j/k keys to navigate.

More, a cousin to less, is simpler and sometimes already installed by default. It’s okay for quick reads, but less usually wins for longer sessions because of its richer navigation features.

Head and tail for the quick snapshot

If you only need the first part or the tail end of a file, head and tail are perfect. They don’t dump the whole thing; they keep you focused on what’s immediate.

  • head filename.txt lets you see the first 10 lines, unless you tell it otherwise with -n 20, for example.

  • tail filename.txt shows the last 10 lines by default, with -n to adjust.

These are great when you’re looking at log files or real-time outputs where the most recent information matters.

Gaze with a bit of structure: numbering lines and filtering

Sometimes you want to know where you’re looking. It’s handy to add line numbers. The nl command prints line numbers alongside each line, which makes it easier to discuss a patch, a log snippet, or a piece of code:

  • nl filename.txt

If the file has a lot of whitespace or you’re chasing specific patterns, you can combine commands with pipes:

  • cat filename.txt | grep "pattern"

Here, you’re filtering the content as it flows through the pipeline. It’s a tiny example, but it demonstrates how flexible the shell can be when you mix simple tools.

A quick tour of the toolbox: what else might you reach for

  • sed and awk: If you need to transform the content on the fly, these two are the heavy hitters. Sed can perform substitutions and edits within the stream, while awk excels at field-based processing. For example, you could extract just the second column from a space-delimited file and print it with:

  • awk '{print $2}' filename.txt

  • cat with redirection: You can recreate, append, or combine content by redirecting output. For instance:

  • cat header.txt > combined.txt will replace combined.txt with header.txt’s contents.

  • cat header.txt >> combined.txt adds to the end.

  • od or hexdump: When text isn’t plain or you’re diagnosing encoding issues, these tools peek at the raw bytes, which can be surprisingly revealing.

Practical patterns you’ll actually use everyday

  • View a file and keep your place: pipe through less

  • cat filename.txt | less

This is a neat trick when you want the immediate dump but also want to explore it interactively without leaving the page.

  • Identify the line with a specific term, then jump there

  • grep -n "term" filename.txt

The -n flag prints the line number, which is incredibly helpful for citing a location in a conversation, a script, or a note.

  • Show the content with numbers for quick reference

  • nl -ba filename.txt

The -ba option numbers all lines, including blank ones, which can be handy when you’re debugging or writing documentation.

Common gotchas and little nudges

  • Binary files don’t behave like text files

If a file isn’t plain text (for example, it’s a binary or a very long line without newlines), the terminal may show garbled characters or wrap awkwardly. Tools like hexdump can help you inspect such files safely.

  • Encoding matters

If a file uses a non-UTF-8 encoding, you might see garbled characters. Some editors or tools let you force encoding to UTF-8 for readability, but in the terminal, you’ll often need to know what you’re dealing with and convert if necessary.

  • Line endings can trip you up

Windows-style CRLF endings can affect how tools display the file. If you notice odd characters at line ends, you’re probably looking at a Windows-origin file. Converting it with dos2unix or similar utilities can smooth things out.

A few short stories from the command line world

  • You’ve got a log that’s growing every minute. You want to keep an eye on the latest events without losing your previous notes. tail -f filename.log is your friend here. It tails the file, showing new lines as they’re written, which is perfect for monitoring in real time.

  • You’re preparing a quick report and need the most recent entries from a massive log. You could combine head and tail:

  • tail -n 1000 filename.log | head -n 200

It’s a tiny cliff note version, but it saves you from drowning in data.

  • You’re teaching a teammate how to read a script. You want the lines to be easy to discuss. nl script.sh | sed -n '1,50p' can show the first chunk with numbers, and you can annotate as you go.

The philosophy of viewing text: keep it simple, keep it swift

There’s a certain elegance in using the right tool for the moment. Cat is the start, a straightforward way to confirm what’s inside a file. But as you grow more comfortable with the shell, you’ll begin to reach for a richer set of helpers. The goal isn’t to memorize every trick but to know which tool fits the moment and how to combine it with others to do more with less effort.

If you’re ever unsure, a quick checklist helps:

  • Do I just want a quick glance or a thorough read-through? For quick, cat or head is fine. For longer reads, a pager like less is better.

  • Do I need to search for something specific? Use grep or awk to slice out the bits that matter.

  • Do I need to transform the data on the fly? Consider sed or awk to shape the output as you go.

  • Do I need to preserve or recreate the content elsewhere? Redirection and piping give you that flexibility.

A tiny, practical cheat sheet you can keep handy

  • cat filename.txt — display contents directly

  • less filename.txt — page through a long file

  • head -n 20 filename.txt — show the first 20 lines

  • tail -n 20 filename.txt — show the last 20 lines

  • nl -ba filename.txt — number all lines

  • grep -n "pattern" filename.txt — find lines with a pattern and show their numbers

  • awk '{print $2}' filename.txt — print the second field from each line

  • sed 's/old/new/' filename.txt — substitute text on the fly

  • cat header.txt > combined.txt — overwrite with new content

  • cat header.txt >> combined.txt — append to the end

A gentle nudge to keep exploring

The beauty of the Linux terminal isn’t just in the commands themselves. It’s in how you start to see text—how plain text becomes a canvas you can manipulate, filter, and analyze with a few keystrokes. It’s a language you learn bit by bit, like picking up a musical instrument: you don’t become a virtuoso overnight, but every practice session adds a layer of fluency. And while the cat command is often the first chord you strike, you’ll soon discover a whole orchestra ready to help you parse the world in words.

So next time you open a text file in the terminal, you’ll likely reach for cat first. You’ll hear that familiar, simple tone—the same one you hear when you pick up a familiar book from a shelf in a quiet room. And if the moment asks for something more nuanced, you’ll switch to a pager or a filter, weaving a little workflow that makes the content not just readable but understandable, too.

In the end, the act of viewing a file is more than a momentary glance. It’s a small, practical ritual that keeps you connected to the data, to the story the words tell, and to the way your own thinking will shape what you do next. If you keep that curiosity close, you’ll find the terminal isn’t just a tool—it’s a companion for everyday problem-solving, one that grows with you as you learn.