Baike.dev
Connexion
> 返回资讯列表
news_article.exe
📰

You’re Probably Using cat Wrong: Here’s What I Use Instead

2026年9月4日2 次浏览来源:Dev.to 阅读原文

Why the most popular Linux command gets abused, how it hurts performance, and the modern CLI tools that replace it. Almost every Linux user learns on their very first day in the terminal. You open a shell, navigate into a directory, and need to see what is inside a file. Your fingers automatically type . The contents flash across your screen, and you move on to your next task. Over time, that initial habit hardens into an unconscious reflex. You reach for when you want to view a configuration file. You reach for when you need to inspect a Docker log. You type without giving it a second thought. You use it to browse source code, peek at server metrics, and pipe data between commands. I did the exact same thing for years. It felt natural, quick, and harmless. Then I started managing...

Why the most popular Linux command gets abused, how it hurts performance, and the modern CLI tools that replace it. Almost every Linux user learns on their very first day in the terminal. You open a shell, navigate into a directory, and need to see what is inside a file. Your fingers automatically type . The contents flash across your screen, and you move on to your next task. Over time, that initial habit hardens into an unconscious reflex. You reach for when you want to view a configuration file. You reach for when you need to inspect a Docker log. You type without giving it a second thought. You use it to browse source code, peek at server metrics, and pipe data between commands. I did the exact same thing for years. It felt natural, quick, and harmless. Then I started managing production environments with multi-gigabyte log streams, large distributed microservices, and high-throughput pipelines. That was when I realized something important: is one of the most misunderstood and misused commands in the entire Unix toolkit. Using as your default file viewer is not just inefficient. In many situations, it freezes your terminal, wastes CPU cycles, creates unnecessary kernel overhead, and strips away helpful context like syntax highlighting and git status. Understanding why causes these problems, and knowing what to use instead, will change the way you interact with the Linux command line. 1. The Original Purpose of cat: Concatenation, Not Viewing To understand why using as a daily file reader is a mistake, you have to look at what the command was built to do. The name is short for catenate, which means to connect things together in a series or chain. Ken Thompson and Dennis Ritchie wrote the original version in 1971 for Version 1 Unix on the PDP-11. The primary goal of was simple: take two or more files, read their byte streams in sequential order, and write them out together as a single combined stream. Here is what proper, intended use of looks like: In every single one of those examples, does exactly what its name promises. It takes multiple inputs and concatenates them into one destination stream. So how did it become the default command for reading a single file? In Unix, standard output () points to the terminal screen by default. If you run with only one file argument and do not redirect the output to another file, the command simply reads those bytes and pushes them directly to your screen. It worked, so early Unix users adopted it as a quick shortcut. Over decades, tutorials and books passed down that shortcut to generations of new administrators. But printing a single file to a terminal screen was never the reason was written. It was merely a convenient side effect of Unix stream design. When you use a tool designed for stream concatenation as an interactive document viewer, you quickly run into severe limitations. 2. The "Useless Use of Cat" (UUOC) and Kernel Overhead The most common mistake people make with is using it to feed data into another command through a pipe. You see this pattern everywhere in production shell scripts, tutorials, and daily terminal habits: This pattern is so pervasive that in the mid-1990s, Usenet shell programmer Randal L. Schwartz coined the term Useless Use of Cat (UUOC). For years, senior Unix administrators awarded tongue-in-cheek "UUOC Awards" to scripts that piped into commands that could already read files directly. Why does this matter? Is it just pedantic style critique, or does it truly impact your system? To see why UUOC is bad engineering, look at what the Linux kernel must do behind the scenes when you run a pipeline like . When you type , your shell performs the following sequence of operations: Kernel Pipe Creation: The shell executes the system call. The kernel allocates a dedicated inter-process communication (IPC) circular buffer in kernel memory, which defaults to 64 kilobytes on modern Linux systems. Process Forking: The shell invokes (or ) twice. It spawns two completely separate child processes: one for the binary and one for the binary. File Descriptor Duplication: The shell calls multiple times. It wires the standard output of the process to the write end of the pipe, and wires the standard input of the process to the read end of the pipe. Binary Execution: The shell runs to load and execute , and runs another to load and execute . Memory Copying and Context Switching: The process issues system calls to fetch chunks from the storage drive into its own user-space memory buffer. Then it issues calls to copy that data into the kernel pipe buffer. Meanwhile, the operating system scheduler repeatedly pauses and resumes execution, performing CPU context switches between the process and the process. Finally, issues its own system calls to pull data from the pipe buffer into its user space. Now compare that entire cascade of operations with the direct command: In the direct version, the shell spawns exactly one process (). The kernel creates zero pipes. There is zero inter-process context switching. The binary opens directly via the system call and reads the file into memory using fast, sequential page-aligned reads. If a command accepts a file path as an argument, passing the file directly is always faster, cleaner, and less taxing on system resources. What if a command does not accept a file path, such as or certain custom binaries? You still do not need . Use standard shell input redirection instead: Using instructs the shell itself to open the file descriptor and bind it to standard input before executing . You get zero extra child processes and zero pipe overhead. 3. Production Danger: The Terminal Screen Freeze Beyond process overhead, running on single files in production can cause real operational headaches. Imagine you are SSHed into a busy production server that is experiencing an alert. You change into and want to inspect what just happened. Without thinking, you type: If that log file is ten gigabytes, you just initiated a small disaster for your local terminal session. does not care how large the file is. It has no internal rate limiting and no awareness of your screen size. It immediately reads every single byte from disk as fast as the NVMe drive can push it and dumps it straight into your standard output stream. Here is what happens next: Pseudo-Terminal Buffer Congestion: Thousands of lines per second flood into your pseudo-terminal device (). Your terminal emulator (such as Windows Terminal, iTerm2, Alacritty, or Kitty) must parse ANSI sequences, calculate word wraps, render glyphs, and update graphics memory. The terminal application will frequently freeze or stutter. Network Saturation: If you are connected over an SSH session, the server tries to push gigabytes of raw text across your TCP connection. The SSH TCP window fills up, your latency skyrockets, and your keystrokes stop responding. The Ctrl+C Delay: You desperately press to send a signal. While might terminate on the remote host within a few hundred milliseconds, your local terminal emulator still has megabytes of text buffered in its socket receive queue. Your screen continues to flicker and scroll uncontrollably for another thirty seconds while you wait for the backlog to clear. Lost History: The runaway stream instantly overflows your terminal scrollback buffer. Any previous diagnostic commands, error codes, or notes you had visible in your terminal window get wiped away into oblivion. There is another common trap: accidentally running on a binary or compressed file. When raw binary data dumps into a terminal emulator, unprintable byte sequences get interpreted as hardware control characters. These escape codes can alter your terminal character set, turn all future typed text into unreadable Greek or graphic runes, disable cursor visibility, and break your prompt formatting. When that happens, you have to run or blindly to recover your shell session: A tool that can freeze your session or corrupt your display with

> 分享: