Tutorials  /  Linux Basics

Linux cat Command: View, Merge, and Number Files

Ccentron Redaktion · December 2025 ·8 min read ·Linux Basics, Tutorial

The cat command—short for “concatenate”—is one of the core tools in Unix-style systems. It first appeared in Unix Version 1 (1971) and is available by default on almost every Linux distribution. Although many people use it mainly to print a file’s content in the terminal, it can also combine files, create new files, append text, and redirect output. That flexibility makes it a everyday helper for shell work and file handling.

This guide shows how to work with cat in real situations. You’ll learn typical tasks such as displaying files, merging multiple files, redirecting input and output, numbering lines, and using pipes. It also walks through helpful flags that make cat faster and more practical across different setups.

Prerequisites

Before you start, make sure you have:

  • A Linux server running Ubuntu or another supported distribution.
  • SSH access to that server.
  • A non-root account with sudo rights.
  • A text editor such as nano for creating and editing files.
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

cat Command Options

The cat utility is both simple and powerful. It lets you view, join, and process file contents. Its name comes from “concatenate,” pointing to its original job of merging files into a single output. While its most common role is showing a file in the terminal, different options unlock extra use cases like formatting output, troubleshooting content, and adjusting streams.

This part explains the basic syntax, lists frequently used flags, and provides hands-on examples.

Command Syntax

The term “concatenate” describes how cat joins file contents into one stream. Even though combining files is still its key feature, it’s often used for quickly checking files, reviewing text, or passing data between commands.

The general format is:

Code
cat [options] [file(s)]
[options]: Flags that change how cat behaves, such as formatting output.
[file(s)]: One or more files to process. If you leave this out, cat reads from standard input.

If you run cat without any files, it switches to interactive mode. You can type text directly, then press Ctrl + D to finish. This is handy for quick notes or manual input into a pipe.

Summary of Common Options

The following list covers the most popular cat options.

Option Description Example
-b Numbers only lines that are not empty. cat -b example.txt
-s Collapses repeated blank lines into a single blank line. cat -s example.txt
-E Shows a $ at the end of each line. cat -E example.txt
-T Displays tab characters as ^I. cat -T tabbed.txt
-A Same as using -vET together. cat -A example.txt
-v Reveals non-printing characters. cat -v ctrlc.txt

Demonstrating Selected Options

cat includes several flags that expand what it can do. The examples below show their behavior.

Prepare the Test File

Create and open a file called example.txt:

Console
$ nano example.txt

Add this text to the file:

Hello, everyone!

Greetings from centron!

Save and close with Ctrl + X, then press Y and Enter.

Try These cat Options

-b: Counts only non-empty lines. This is useful for logs or code where blank spacing isn’t important.

Console
$ cat -b example.txt

Output:

1 Hello, everyone!

2 Greetings from centron!!

-s: Reduces multiple blank lines in a row down to one, helping clean up messy files.

Console
$ cat -s example.txt

-E: Appends a $ at each line end so you can spot trailing spaces or line breaks.

Console
$ cat -E example.txt

Output:

Hello!$

$

Greetings from centron!$

-T: Shows tab characters as ^I, which helps find indentation problems. You can create a tabbed file like this:

Console
$ echo -e "Hello,\tWorld!" > tabbed.txt
$ cat -T tabbed.txt

Output:

Hello,^IWorld!

-A: Combines -E, -T, and -v to show line endings, tabs, and hidden characters at once.

Console
$ cat -A example.txt

Output:

Hello,^Ieveryone!$

$

Greetings from centron!$

-v: Makes control characters visible. For instance:

Console
$ echo $'\x03' > ctrlc.txt
$ cat -v ctrlc.txt

Output:

^C

Display Files Using the cat Command

The most typical use of cat is printing file contents straight to the terminal. It’s quick and lightweight for reading logs, scripts, or configs without opening an editor. The next examples show how to output one file, several files, and handle large files more comfortably.

Create a file named tutorial.txt with one line inside it:

Console
$ echo "Learn cat commands with centron!" > tutorial.txt

Show the content of tutorial.txt:

Console
$ cat tutorial.txt

Output:

Learn cat commands with centron!

To print multiple files, list them after cat. For example, to show example.txt and then tutorial.txt:

Console
$ cat example.txt tutorial.txt

Output:

Hello, everyone!

Greetings from centron!

Learn cat commands with centron!

cat prints files in the order you provide. If you want the reverse order, use tac, which is simply cat spelled backwards. It outputs lines from bottom to top.

Use a wildcard (*) to output every .txt file in the current folder:

Console
$ cat *.txt

If your text files are in another directory, include the directory path before the wildcard:

Console
$ cat /path/to/directory/*.txt

Replace /path/to/directory/ with the real location.

For very large files, you can avoid flooding your terminal by combining cat with other commands through pipes.

Pipe into head to read only the beginning of a file. For example, to show the first 20 lines of largefile.txt:

Console
$ cat largefile.txt | head -n 20

Pipe into tail to read from the end instead:

Console
$ cat largefile.txt | tail -n 20

Swap largefile.txt with any large file you want to inspect.

You can also pass the output into less to read it in a scrollable view:

Console
$ cat tutorial.txt | less

Number Only Non-Empty Lines with -b

You can add line numbers by running cat together with the -b flag. This option counts only lines that contain text, skipping blank ones. It’s helpful when you want to concentrate on real content in files that include lots of empty spacing, such as configuration files or lightweight logs.

Print /etc/ssh/ssh_config while leaving out empty lines:

Console
$ cat -b /etc/ssh/ssh_config

Output:

1 # This is the ssh client system-wide configuration file. See

2 # ssh_config(5) for more information. This file provides defaults for

3 # users, and the values can be changed in per-user configuration files

4 # or on the command line.

Combine Line Numbering with Pipes

You can pair cat -n or cat -b with other commands to do more advanced filtering and formatting.

Search through numbered syslog lines for error messages:

Console
$ sudo cat -n /var/log/syslog | grep "error"

Example Output:

1452 Mar 1 14:22:01 ubuntu-server cron[1234]: ERROR: Failed to start job

1789 Mar 1 14:35:01 ubuntu-server kernel: [UFW BLOCK] IN=eth0 SRC=203.0.113.5

Pull specific lines from example.txt using awk:

Console
$ cat -n example.txt | awk 'NR==1 || NR==3 {print "Line " $1 ": " $0}'

Output:

Line 1: 1 Hello, everyone!

Line 2: 2

Line 3: 3 Greetings from centron!

Conclusion

In this article, you looked at how to work with the cat command to read, combine, and handle file content on Linux systems. You practiced showing files, redirecting input and output, appending new lines, inserting variables, and numbering lines—both interactively and in scripts that fit automation workflows.

Whether you’re reviewing logs, building scripts, or troubleshooting configuration files, cat stays a dependable and must-have tool in any Linux user’s toolbox. For deeper or more specialized usage, check the Linux cat man page.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren