Deep Dive into Linux cat Command: From File Viewing to Low-Level Implementation#

Author: JsonKit Published: May 12, 2026 Original Link: https://jsokit.com/tools/linux-commands/cat/


Introduction#

In Linux systems, cat is one of the oldest and most commonly used commands. Its name comes from “concatenate,” originally designed to join multiple files and output to standard output. However, in practice, it’s more frequently used for viewing file contents, merging files, creating new files, and more.

As a developer, you might use cat every day, but do you understand its working principles, performance boundaries, and how to use it efficiently? This article will deeply analyze this seemingly simple command from a technical perspective.

How the cat Command Works#

System Call Level#

The core implementation of cat is very concise, essentially a loop of reading and writing:

// Simplified cat implementation logic
while ((n = read(fd, buffer, BUFSIZE)) > 0) {
    write(STDOUT_FILENO, buffer, n);
}

When executing cat file.txt, the kernel will:

  1. Open the file (open() system call)
  2. Loop to read file contents (read() system call)
  3. Write to standard output (write() system call)
  4. Close the file (close() system call)

The default buffer size is typically 8KB or 16KB, striking a balance between performance and memory usage.

Why cat is Suitable for Small Files#

cat loads the entire file to standard output at once, which is very efficient for small files. But for large files (like several GB logs), this will:

  • Occupy large amounts of memory (if redirected through pipes)
  • Unable to quickly locate specific content
  • Terminal buffer might overflow

This is why viewing large files should use less or head/tail.

Technical Details of Core Options#

-n: Line Number Display#

$ cat -n app.js
     1  const express = require('express');
     2  const app = express();
     3  // More code...

Implementation principle: cat inserts line numbers at the beginning of each line, right-aligned to 6 characters width. Note that blank lines are also numbered.

-b: Number Non-blank Lines#

$ cat -b app.js
     1  const express = require('express');
     2  const app = express();

     3  app.listen(3000);

Only numbers non-blank lines, suitable for viewing code files with many blank lines.

-s: Squeeze Blank Lines#

$ cat -s article.md
# Title

Content paragraph 1.

Content paragraph 2.

Compresses multiple consecutive blank lines into one, making document structure more compact.

-A: Show All Special Characters#

$ cat -A script.sh
#!/bin/bash^M$
echo "Hello"$
  • ^I represents Tab
  • $ represents end of line
  • ^M represents Windows line ending (CRLF)

This option is very useful when debugging script formatting issues, especially in cross-platform development scenarios.

-E and -T: Display Line Endings and Tabs Separately#

$ cat -E file.txt
Line 1$
Line 2$
$ cat -T code.py
def main():
^Iprint("Hello")

More precise than -A, only showing specific types of special characters.

Practical Application Scenarios#

1. Quick Configuration File Viewing#

# View multiple configuration files
cat /etc/nginx/nginx.conf
cat ~/.bashrc

# Combined view of multiple files
cat config/base.json config/override.json

2. Create New Files#

# Create file from standard input
cat > newfile.txt << EOF
This is line 1
This is line 2
EOF

# Or append content
cat >> existing.txt << EOF
Appended content
EOF

Here << is heredoc syntax, cat redirects input to a file.

3. Merge Files#

# Merge multiple log files
cat access.log.* > combined.log

# Merge CSS files
cat styles/base.css styles/components/*.css > bundle.css

# With order guarantee
cat {header,main,footer}.js > app.js

4. Filter with grep#

# Search for keywords
cat large.log | grep "ERROR"

# Count matching lines
cat access.log | grep "404" | wc -l

But note, this can be simplified to grep "ERROR" large.log, avoiding unnecessary pipes.

5. File Content Comparison#

# Compare differences between two files
cat file1.txt file2.txt | sort | uniq -u

Outputs lines that only appear in one of the files.

Performance Optimization and Pitfalls#

Avoid Useless Pipes#

# Inefficient approach
cat file.txt | grep "pattern"
cat file.txt | wc -l

# Efficient approach
grep "pattern" file.txt
wc -l file.txt

This is known as the “Useless Use of Cat Award” (UUOC). grep and wc can both read files directly, without needing to go through cat.

Large File Handling#

# Wrong: Viewing GB-level logs
cat production.log

# Correct: Paginated viewing
less production.log

# Or only view beginning/end
head -n 100 production.log
tail -f production.log

Binary File Warning#

# This will output garbled text, possibly corrupting the terminal
cat binary.jpg

# Check file type
file image.jpg
# image.jpg: JPEG image data, JFIF standard 1.01

For binary files, use the file command to check type first, or use hexdump/xxd to view hexadecimal content.

Combination with Other Commands#

cat + sort: Sort File Contents#

cat names.txt | sort > sorted.txt

cat + tr: Character Translation#

# Convert to lowercase
cat file.txt | tr 'A-Z' 'a-z'

# Remove all spaces
cat code.txt | tr -d ' '

cat + sed: Text Replacement#

# Batch replacement
cat config.txt | sed 's/old/new/g' > new_config.txt

Alternative Tools for cat#

Scenario Recommended Tool Reason
Large file paginated browsing less Memory-friendly, searchable
View first few lines head Quick preview
Real-time log viewing tail -f Continuous updates
With syntax highlighting bat Modern alternative
Binary files hexdump Hexadecimal viewing

bat is a modern alternative to cat, supporting syntax highlighting, Git integration, line number display, and other features. Highly recommended for development environments.

Summary#

cat may seem simple, but it’s a perfect embodiment of Unix philosophy: do one thing well. Understanding its working principles and applicable scenarios will make you more adept at file operations.

Key takeaways:

  • Suitable for quick viewing of small files
  • Supports practical options like line numbers and special character display
  • Avoid using on large files and binary files
  • Combined with other commands for powerful text processing capabilities
  • Be mindful of avoiding useless pipes (UUOC)

Next time you use cat, consider: Is this command the optimal choice? Can it be replaced with a more specialized tool?



This article is technically supported by JsonKit (https://jsokit.com). JsonKit provides 100+ developer tools, including JSON formatting, code conversion, text processing, and more, helping to improve development efficiency.