Introduction
The cat command is one of the simplest and most powerful tools we can find in any Linux distribution. Although its name comes from concatenating, its most frequent use is to show the content of text files directly in the terminal. In this article we will explore from the basic syntax to advanced examples that will allow you to make the most of your day-to-day cat as a system manager, developer or curious user.
Basic syntax
The cat syntax is extremely simple: it is written cat followed by one or more file routes separated by spaces. If no file is indicated, cat will read from the standard input, allowing it to be used in combination with pipes to process data flows. Some of the most common options include -n to number lines, -b to number only non-empty lines and -s to delete multiple consecutive blank lines. Although cat does not have a lot of lags, its real strength lies in the way it is combined with other shell commands.
Show the content of a file
To show the content of a single file simply run cat followed by the path of the file. For example, cat / etc / hosts will print the host configuration file on the screen. If the file is very large, it may be uncomfortable to see everything at once; in such cases it is usually combined cat with less or more, like cat file | less, which allows to move up and down through the keyboard. Another useful trick is to use the -n option to have line numbering, which facilitates reference to specific parts of the file during scripts debugging or log review.
Conducting several files
One of the original cat functions is to focus, that is, to link the content of several files into a single output flow. If you run cat file 1.txt file 2.txt file 3.txt, the result will be the succession of the three files, first file content 1.txt, followed by file 2.txt and finally file 3.txtt. This feature is very useful when you need to create a combined log file from several fragments, or when you want to generate a script that includes multiple separate stored code blocks. In addition, by combining the concatenation with output redirection, it is possible to create the resulting file directly: cat file 1.txt file 2.txt > combined.
Use with redirections and pipes
Cat really shines when used next to the shell's redirection and pipe mechanisms. For example, the content can be filtered by grep: cat registro.log | grep ERROR will show only the lines that contain the word ERROR. In addition, the content can be ordered with deaf: deordered cat. | sort > ordered. Another frequent application is to pass the cat output as input to other programs that expect data in stdin, such as awk or thirst, allowing for complex transformations without creating intermediate files. Finally, cat can also read from the standard input when you are not provided with files, which allows you to use it in interactive command sequences: type cat, write some lines and end with Ctrl + D so that the shell returns the input.
Practical examples
Let's see some concrete examples that illustrate the versatility of cat:
- Create a text file quickly: cat > nota.txt
This opens the standard input; after writing the content and pressing Ctrl + D, everything is saved in nota.txt. - Add content at the end of an existing file: cat > > registro.txt
Similar to the previous, but using the add-on operator > > to avoid overwriting the present. - Number lines of a configuration file: cat -n / etc / nginx / nginx. conf | less
Combine numbering and paging for a comfortable review. - Unite several fragments of a script: cat parte1.sh parte2.sh parte3.sh > script _ complete. sh
Then you can grant execution permits with chmod + x script _ complete. - Filter and count occurrences: cat acceso.log | grep '200 OK' | wc -l
It counts how many HTTP requests with code 200 appear in the log. - Convert capital letters to small letters: cat entrada.txt | tr '[: upper:]' '[: lower:]' > salida.txt
It shows how cat can serve as a bridge between other transformation commands.
Tips and tricks
Some tips to make the most of it:
- Always use clear absolute or relative routes to avoid ambiguities, especially when working on scripts that run from different directories.
- Remember that cat does not interpret anything; if you need to process special characters such as tabulations or line leaps differently, combine with tools such as od or hexdump for binary inspection.
- In production environments, avoid using cat with very large files without paging; prefer less or most to not saturate the terminal.
- When you need to see the end of a file, combine cat with tail: cat file | tail -20 shows the last 20 lines.
- To quickly check if a file is empty, you can use cat file | wc -l; a result of zero indicates absence of content.
- Finally, if you work frequently with configuration files, consider creating an alias as alias micat = 'cat -n' to have automatic numbering without writing the option at a time.
Conclusion
In short, the cat command, despite its apparent simplicity, is a fundamental piece of the command line environment in Linux. Its ability to show, focus and work with redirections makes it an indispensable ally for daily tasks such as reviewing logs, creating files quickly or combining code fragments. Dominating your basic options and learning to fit it with other shell tools will allow you to work more efficiently and safely. We invite you to practice the examples presented and explore your own combinations; you will soon discover that cat is much more than a simple file viewer.


