[Linux] Streamlining File Display, Concatenation, and Processing with the cat Command

目次

Overview

The cat (concatenate) command is one of the most fundamental tools in Linux, primarily used to display file contents to the standard output and join multiple files into one. Beyond simply viewing data, it becomes a powerful asset for log analysis and document preparation when combined with pipelines and redirection. Mastering this command allows for efficient data stream management within a terminal environment.

Specifications (Arguments and Options)

Syntax

BASH

cat [options] [file_name...]

Major Options

OptionDescription
-nAdds line numbers to all output lines.
-bAdds line numbers only to non-empty lines.
-EDisplays a “$” at the end of each line to visualize line endings.
-sSuppresses repeated empty lines into a single empty line.
-TDisplays tab characters as “^I” to visualize invisible characters.

Basic Usage

This procedure is used to quickly inspect the beginning of a server access log. It is a common first step when troubleshooting connectivity or user activity.

BASH

# Display the access log and extract only the first 5 lines
cat /var/log/audit_service/access.log | head -n 5

TEXT

192.168.1.10 - mori [26/Jan/2026:14:00:01 +0900] "GET /index.html HTTP/1.1" 200
192.168.1.11 - guest [26/Jan/2026:14:00:05 +0900] "GET /css/style.css HTTP/1.1" 200
192.168.1.10 - mori [26/Jan/2026:14:00:10 +0900] "POST /api/login HTTP/1.1" 200
192.168.1.12 - admin [26/Jan/2026:14:01:00 +0900] "GET /admin/config HTTP/1.1" 403
192.168.1.10 - mori [26/Jan/2026:14:01:05 +0900] "GET /favicon.ico HTTP/1.1" 404

Practical Commands

In this practical pipeline, we merge a compressed legacy log archive with a current log file to create a single consolidated file for an investigation.

BASH

# Display the error log with line numbers
cat -n /var/log/audit_service/error.log | head -n 5

# Combine an unzipped old log and the current log into a temporary file
# The "-" symbol is a special argument that specifies the position for standard input
zcat /var/log/audit_service/error.log.1.gz | \
cat - /var/log/audit_service/error.log \
> /tmp/mori_investigation_log.txt

TEXT

     1  [2026-01-26 10:00:00] ERROR: Connection timeout from 10.0.0.5
     2  [2026-01-26 10:05:22] WARN: Resource spike detected
     3  [2026-01-26 10:10:45] ERROR: Invalid authentication for user: mori
     4  [2026-01-26 10:12:01] INFO: Background worker started
     5  [2026-01-26 10:15:30] ERROR: Database connection failed

Customization Points

The file path /var/log/audit_service/ should be updated to reflect the actual directory where your target logs are stored. The output path /tmp/mori_investigation_log.txt is intended as a temporary file; if you need to keep the data permanently, choose a different directory. You can also adjust the number in head -n 5 to view a different amount of lines depending on your specific needs.

Notes

When dealing with massive files that exceed several gigabytes, using cat to display the entire content can make terminal control difficult or overwhelm system memory. In such cases, consider using a pager like less. Be aware that viewing system logs may require sudo privileges. Furthermore, avoid using cat on binary files like images or executables, as this can corrupt the terminal display or trigger continuous beeping sounds. Always ensure the target is a text file before proceeding.

Applications

The following examples demonstrate how to use heredocs to instantly create configuration files and how to insert a custom message between existing files during concatenation.

BASH

# Create a task memo for user mori using a heredoc
cat - << EOF > /tmp/mori_task.txt
Task: Security audit for project Gamma
Date: 2026-01-26
Assignee: mori
EOF

# Insert a separator (from standard input) between two files during output
# Assumes app_info.txt exists in the /tmp directory
echo "------- DATA SEPARATOR -------" | cat /tmp/mori_task.txt - /tmp/app_info.txt

TEXT

Task: Security audit for project Gamma
Date: 2026-01-26
Assignee: mori
------- DATA SEPARATOR -------
Application Version: 3.1.2
Status: Active

Summary

The cat command is far more than a simple file viewer. It plays a vital role in supporting the flexibility of Linux operations by enabling file concatenation through the use of the “-” symbol for standard input and text generation via heredocs. In large-scale log investigations, combining cat with commands that handle compressed files allows for efficient data analysis while saving disk space through stream processing. Correctly utilizing options and redirection is the key to achieving accurate and rapid system administration as demonstrated in this guide.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次