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¶
Course Progress
What You'll Learn¶
By the end of this lesson, you'll be able to:
- Understand the purpose of the
catcommand - Display file contents
- Create files using
cat - Concatenate multiple files
- Display line numbers
- Redirect output using
cat - Use
catin command pipelines - Apply
catin 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¶
Display File Contents¶
Create a file.
Display:
Output:
Display Multiple Files¶
Output:
Concatenate Files¶
Merge files into another file.
View:
Create a File Using cat¶
Type:
Press:
Display:
Append to a File¶
Add:
Press:
Display Line Numbers¶
Example:
Display Non-Blank Line Numbers¶
Blank lines are ignored.
Show Tabs¶
Tabs appear as:
Useful for debugging configuration files.
Show End of Line¶
Example:
Show Hidden Characters¶
Displays:
- Tabs
- Line endings
- Non-printable characters
Useful when troubleshooting malformed configuration files.
Number All Lines¶
Combine with Other Commands¶
Count lines.
Search.
Sort.
Remove duplicates.
Production Examples¶
View SSH configuration.
Display hosts file.
View Docker Compose file.
View Kubernetes manifest.
View application configuration.
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.
Add:
Press:
Task 2¶
Display the file.
Task 3¶
Display line numbers.
Task 4¶
Append another line.
Add:
Task 5¶
Create another file.
Task 6¶
Merge both files.
Task 7¶
Count lines.
Task 8¶
Search.
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
serverdirective. - 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
catfor small files. - Use
lessfor large files. - Combine
catwith pipes for text processing. - Use
cat -nwhen discussing configuration files. - Avoid using
caton very large log files.
Common Mistakes¶
❌ Using cat on huge log files.
✅ Instead:
or
❌ Creating unnecessary pipelines.
✅ Instead of:
You can simply write:
This is more efficient.
Interview Questions¶
Beginner¶
- What does
catstand for? - How do you display a file?
- How do you merge two files?
- Which option shows line numbers?
Intermediate¶
- Explain
cat -A. - When should you use
catinstead ofless? - Why is
grep file.txtgenerally preferred overcat file.txt | grep?
Architect Level¶
- Why is
catcommonly seen in shell scripts? - How would you inspect a production configuration file safely?
- When does using
catbecome inefficient?
Summary¶
In this lesson, you learned:
- Viewing files
- Creating files
- Appending text
- Merging files
- Displaying line numbers
- Using
catin 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¶
catstands for concatenate.- It can display, create, append, and combine files.
cat -ndisplays line numbers.catintegrates seamlessly with pipes and redirection.- Prefer
lessfor 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