Skip to content

cat Command

The cat (concatenate) command is one of the most frequently used Linux utilities for viewing, creating, combining, and redirecting text files. Although simple, it plays an important role in shell scripting, automation, and system administration.


Learning Path

Linux Mastery → Module 3: Text Processing → Lesson 1

Difficulty: Beginner

Reading Time: 20 Minutes

Course Progress

Course: Linux Mastery

Module: Text Processing

Lesson: 1 of 18


What You'll Learn

By the end of this lesson, you'll be able to:

  • Understand the purpose of the cat command
  • Display file contents
  • Create files using cat
  • Concatenate multiple files
  • Display line numbers
  • Redirect output using cat
  • Use cat in command pipelines
  • Apply cat in real-world administration tasks

Prerequisites

Before starting this lesson, complete:

  • Module 1 – Linux Fundamentals
  • Module 2 – Linux Command Line Essentials

What is cat?

cat stands for:

Concatenate

Originally, it was designed to join multiple files together.

Today, it is commonly used to:

  • View file contents
  • Create text files
  • Merge files
  • Redirect output
  • Feed data into pipelines

Command Syntax

cat [OPTIONS] FILE...

Display File Contents

Create a file.

echo "Welcome to REBASH Academy" > notes.txt

Display:

cat notes.txt

Output:

Welcome to REBASH Academy

Display Multiple Files

cat file1.txt file2.txt

Output:

Contents of file1

Contents of file2

Concatenate Files

Merge files into another file.

cat part1.txt part2.txt > complete.txt

View:

cat complete.txt

Create a File Using cat

cat > users.txt

Type:

Alice

Bob

Charlie

Press:

Ctrl + D

Display:

cat users.txt

Append to a File

cat >> users.txt

Add:

David

Press:

Ctrl + D

Display Line Numbers

cat -n users.txt

Example:

1 Alice

2 Bob

3 Charlie

4 David

Display Non-Blank Line Numbers

cat -b users.txt

Blank lines are ignored.


Show Tabs

cat -T sample.txt

Tabs appear as:

^I

Useful for debugging configuration files.


Show End of Line

cat -E sample.txt

Example:

Linux$

Docker$

Kubernetes$

Show Hidden Characters

cat -A sample.txt

Displays:

  • Tabs
  • Line endings
  • Non-printable characters

Useful when troubleshooting malformed configuration files.


Number All Lines

cat -n /etc/passwd

Combine with Other Commands

Count lines.

cat users.txt | wc -l

Search.

cat users.txt | grep Bob

Sort.

cat users.txt | sort

Remove duplicates.

cat users.txt | sort | uniq

Production Examples

View SSH configuration.

cat /etc/ssh/sshd_config

Display hosts file.

cat /etc/hosts

View Docker Compose file.

cat docker-compose.yml

View Kubernetes manifest.

cat deployment.yaml

View application configuration.

cat application.properties

Production Perspective

Linux administrators frequently use cat to:

  • Read configuration files
  • Verify deployment manifests
  • Display scripts
  • Combine configuration fragments
  • Feed text into pipelines

Although cat is simple, it appears in countless shell scripts and automation workflows.


Hands-on Lab

Task 1

Create a file.

cat > linux.txt

Add:

Linux

Docker

Kubernetes

Press:

Ctrl + D

Task 2

Display the file.

cat linux.txt

Task 3

Display line numbers.

cat -n linux.txt

Task 4

Append another line.

cat >> linux.txt

Add:

Terraform

Task 5

Create another file.

echo "Ansible" > tools.txt

Task 6

Merge both files.

cat linux.txt tools.txt > complete.txt

Task 7

Count lines.

cat complete.txt | wc -l

Task 8

Search.

cat complete.txt | grep Docker

Command Deep Dive

Option Description
cat file Display file
-n Number all lines
-b Number non-empty lines
-E Show line endings
-T Show tabs
-A Show all hidden characters

Production Troubleshooting Scenario

Scenario

A web server is failing after a configuration change.

Tasks:

  • View the NGINX configuration.
  • Display line numbers.
  • Search for the server directive.
  • Count the number of configuration lines.

Example:

cat -n /etc/nginx/nginx.conf

cat /etc/nginx/nginx.conf | grep server

cat /etc/nginx/nginx.conf | wc -l

Best Practices

  • Use cat for small files.
  • Use less for large files.
  • Combine cat with pipes for text processing.
  • Use cat -n when discussing configuration files.
  • Avoid using cat on very large log files.

Common Mistakes

❌ Using cat on huge log files.

✅ Instead:

less logfile.log

or

tail -f logfile.log

❌ Creating unnecessary pipelines.

✅ Instead of:

cat file.txt | grep Linux

You can simply write:

grep Linux file.txt

This is more efficient.


Interview Questions

Beginner

  1. What does cat stand for?
  2. How do you display a file?
  3. How do you merge two files?
  4. Which option shows line numbers?

Intermediate

  1. Explain cat -A.
  2. When should you use cat instead of less?
  3. Why is grep file.txt generally preferred over cat file.txt | grep?

Architect Level

  1. Why is cat commonly seen in shell scripts?
  2. How would you inspect a production configuration file safely?
  3. When does using cat become inefficient?

Summary

In this lesson, you learned:

  • Viewing files
  • Creating files
  • Appending text
  • Merging files
  • Displaying line numbers
  • Using cat in pipelines
  • Production use cases

Although simple, cat is one of the most frequently used Linux commands and serves as the foundation for more advanced text-processing techniques.


Key Takeaways

  • cat stands for concatenate.
  • It can display, create, append, and combine files.
  • cat -n displays line numbers.
  • cat integrates seamlessly with pipes and redirection.
  • Prefer less for large files.

What's Next?

grep Command — Searching Text in Linux

In the next lesson, you'll learn:

  • Basic and advanced text searching
  • Regular expressions
  • Recursive search
  • Case-insensitive search
  • Counting matches
  • Production log analysis