The cat command in Linux: display and concatenate files

Introduction

The cat command is one of the most basic and powerful tools found in any Linux distribution. Its name comes from the English word concatenate, which means to join or chain. Although at first glance it seems merely a file viewer, its true strength lies in the ability to combine multiple data streams and send them to standard output, to a file, or to another process via pipes. In this article we will explore from its simplest usage to some advanced tricks that can save you time and improve your workflow in the terminal.

What is the cat command?

Cat (concatenate) reads the content of one or more files and writes it to standard output without modifying the data. It does not include line numbering or advanced formatting options, making it ideal when you need a pure and direct view of the content. It is available in practically all shells (bash, zsh, fish) and is part of the GNU Coreutils suite, which guarantees consistent behavior across different systems.

Basic Syntax

The simplest way to use cat is: cat filename. If the filename is omitted, cat reads from standard input, allowing it to be used in combination with redirections and pipes. You can specify multiple files separated by spaces: cat file1 file2 file3. In this case, the contents are displayed one after another in the same order they appear on the command line.

Displaying the Content of a File

To view a text file, simply run cat followed by the file’s path. For example: cat /etc/hostname displays the hostname. If the file is large, the output will scroll quickly in the terminal; in those cases it is common to combine cat with less or more to paginate the output: cat large_file | less. This technique preserves data integrity while allowing comfortable reading.

Concatenating Multiple Files

One of the most frequent uses of cat is to join several files into a single stream. For example, if you have three parts of a document named part1.txt, part2.txt, and part3.txt, you can generate the complete document with: cat part1.txt part2.txt part3.txt > combined_document.txt. The > operator redirects standard output to a new file, overwriting it if it already exists. If you prefer to append to the end of an existing file, use >> instead: cat part4.txt >> combined_document.txt.

Advanced Uses with Redirections and Pipes

  • Combining cat with grep to search for patterns within multiple files without opening each one: cat *.log | grep 'error'.
  • Creating files quickly using standard input: cat > note.txt lets you type lines directly and finish with Ctrl+D.
  • Creating files with multiple lines using a command block: { echo 'First line'; echo 'Second line'; } > file.txt.

Practical Examples

  1. Suppose you want to back up several configuration files into a single tarball: first concatenate them, then compress: cat /etc/hosts /etc/resolv.conf /etc/hostname > backup_conf.tar && tar -czf backup_conf.tar.gz backup_conf.tar && rm backup_conf.tar.
  2. If you need to verify that two files are identical, you can compare their output using diff after concatenating them: cat file1.txt file2.txt | diff -u (if there is no difference, diff will produce no output).
  3. To add a header to a log file without opening an editor, you can do: echo '=== Session Start ===' > temporal.log && cat /var/log/app.log >> temporal.log && mv temporal.log /var/log/app.log.

Tips and Best Practices

  • Avoid using cat solely to display very large files; combine it

    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 .