Home > Text Processing & Search > cat

cat Command Guide: View and Concatenate File Contents

`cat` command is used to read one or more files and display their content to standard output (usually the terminal screen). It is an abbreviation of 'concatenate', and it also provides the function to concatenate files and output them. Learn various ways to use the `cat` command through this guide.

cat Overview

`cat` is one of the most basic commands in Linux/Unix systems, useful for quickly checking the content of text files or combining the content of multiple files to create a new one. Be careful when outputting binary files with `cat`, as unknown characters may be displayed on the terminal.

Main Roles of cat

The `cat` command is primarily used for the following purposes:

Key Application Areas

  • Viewing File Content: Outputs the entire content of a text file to the terminal.
  • Concatenating Files: Combines the content of multiple files in order to create a single output or save it to a new file.
  • Creating New Files: Creates a new file by directly inputting content via standard input.
  • Simple Pipeline Construction: Used to pass file content as input to other commands.

Main cat Command Options

The `cat` command is simple, but it can control the output format through several useful options.

1. Basic Output Option

2. Formatting Options

Generated command:

Try combining the commands.

Description:

`cat` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to view file content, concatenate files, and create new files through various usage examples of the `cat` command.

Output Single File Content

cat my_file.txt

Displays the entire content of `my_file.txt` to the terminal.

Concatenate and Output Multiple Files

cat file1.txt file2.txt file3.txt

Combines the contents of `file1.txt`, `file2.txt`, and `file3.txt` in order and outputs them to the terminal.

Concatenate Multiple Files and Save to New File

cat file1.txt file2.txt > combined.txt

Combines the contents of `file1.txt` and `file2.txt` and saves them to a new file named `combined.txt`. If `combined.txt` already exists, it will be overwritten.

Output File Content with Line Numbers

cat -n document.txt

Outputs each line of `document.txt` prefixed with line numbers.

Create New File (using Standard Input)

cat > new_file.txt
# Type your content here
# Press Ctrl+D to save and exit

Creates `new_file.txt` and saves content directly typed into the terminal to the file. Press `Ctrl+D` to save and exit when done typing.

Real-time Log File Monitoring (used with tail)

cat /var/log/syslog | grep -i error

While `tail -f` is generally more suitable for real-time log file monitoring, `cat` can also be used in pipelines. This example shows a simple case of `cat` piping log file content to `grep`.


Same category commands