The cat command in Linux: display and concatenate files

Introduction

The cat command is one of the simplest and most powerful tools found in any Linux distribution. Although its name comes from concatenate, its most frequent use is to display the contents of text files directly in the terminal. In this article we will explore everything from basic syntax to advanced examples that will let you get the most out of cat in your daily work as a system administrator, developer, or curious user.

Basic Syntax

The syntax of cat is extremely simple: you write cat followed by one or more file paths separated by spaces. If no file is specified, cat reads from standard input, allowing it to be combined with pipes to process data streams. Some of the most common options include -n to number lines, -b to number only non‑empty lines, and -s to suppress consecutive blank lines. Although cat does not have many flags, its true strength lies in how it combines with other shell commands.

Displaying the Content of a File

To display the content of a single file simply run cat followed by the file’s path. For example, cat /etc/hosts prints the hosts configuration file to the screen. If the file is very large, viewing it all at once can be inconvenient; in those cases cat is often combined with less or more, such as cat file | less, which lets you scroll up and down with the keyboard. Another useful trick is to use the -n option to get line numbering, making it easier to refer to specific parts of the file while debugging scripts or reviewing logs.

Concatenating Multiple Files

One of cat’s original functions is concatenation, i.e., joining the contents of several files into a single output stream. If you run cat file1.txt file2.txt file3.txt, the result will be the three files in succession: first the content of file1.txt, then file2.txt, and finally file3.txt. 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 code blocks stored separately. Moreover, by combining concatenation with output redirection you can create the resulting file directly: cat file1.txt file2.txt > combined.txt.

Use with Redirections and Pipes

Cat really shines when used together with the shell’s redirection and pipe mechanisms. For example, you can filter the content with grep: cat logfile.log | grep ERROR shows only the lines containing the word ERROR. Likewise, you can sort the content with sort: cat unsorted.txt | sort > sorted.txt. Another frequent use is to pipe cat’s output into other programs that expect data on stdin, such as awk or sed, allowing complex transformations without creating intermediate files. Finally, cat can also read from standard input when no files are given, enabling its use in interactive command sequences: type cat, write a few lines, and finish with Ctrl+D so the shell returns what you entered.

Practical Examples

Let’s look at some concrete examples that illustrate cat’s versatility:

  • Create a text file quickly: cat > note.txt
    This opens standard input; after typing the content and pressing Ctrl+D, everything is saved in note.txt.
  • Append content to an existing file: cat >> log.txt
    Similar to the above, but using the append operator >> to avoid overwriting existing data.
  • Number lines of a configuration file: cat -n /etc/nginx/nginx.conf | less
    Combines numbering and paging for a comfortable review.
  • Join several script fragments: cat part1.sh part2.sh part3.sh > complete_script.sh
    Then you can make it executable with chmod +x complete_script.sh.
  • Filter and count occurrences: cat access.log | grep ‘200 OK’ | wc -l
    Counts how many HTTP requests with status 200 appear in the log.
  • Convert uppercase to lowercase: cat input.txt | tr ‘[:upper:]’ ‘[:lower:]’ > output.txt
    Shows how cat can act as a bridge between other transformation commands.

Tips and Tricks

Some tips to get the most out of cat:

  • Always use clear absolute or relative paths to avoid ambiguity, especially when running scripts from different directories.
  • Remember that cat does not interpret anything; if you need to process special characters like tabs or newlines differently, combine it with tools such as od or hexdump for binary inspection.
  • In production environments, avoid using cat on very large files without paging; prefer less or most to avoid overwhelming the terminal.
  • When you need to see the end of a file, combine cat with tail: cat file | tail -20 shows the last twenty lines.
  • To quickly check if a file is empty, use cat file | wc -l; a result of zero indicates no content.
  • Finally, if you frequently work with configuration files, consider creating an alias like alias micat=’cat -n’ to have automatic numbering without typing the option each time.

Conclusion

In summary, the cat command, despite its apparent simplicity, is a fundamental piece of the Linux command‑line environment. Its ability to display, concatenate, and work with redirections makes it an indispensable ally for everyday tasks such as reviewing logs, creating files quickly, or combining code snippets. Mastering its basic options and learning to fit it together with other shell tools will let you work more efficiently and safely. We invite you to practice the examples presented and to explore your own combinations; you will soon discover that cat is much more than a simple file viewer.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .