cat
100%

Command Line · Lesson 7

cat

Learn how to display, concatenate, and redirect file content safely with the cat command.

After learning to identify files, the next step is to read their contents. The cat command displays files and joins their contents; its name is short for "concatenate."

Viewing File Contents

The simplest use of cat displays a file directly in the terminal:

$ cat myfile.txt

The command writes the entire file to standard output. This works well for short text, but a long file may scroll past too quickly.

Which command displays all of myfile.txt in the terminal?

Concatenating Files

When you give cat several files, it reads them in operand order and writes their contents one after another:

$ cat dogfile birdfile

This displays dogfile first and birdfile second. To save the combined output in a new file, redirect standard output with >:

$ cat dogfile birdfile > animals

The shell creates animals or truncates it before running cat, then sends the combined output there. Do not use one of the input files as this destination, because it may be emptied before cat reads it.

Which command writes part1 followed by part2 into a new or replaced file named whole?

Reading Terminal Input into a File

When no input file is given, cat reads standard input. You can combine that behavior with > to enter text from the terminal and write it to a file:

$ cat > newfile.txt

After running the command, type the desired text. Press Ctrl+D to send an end-of-file signal and return to the shell. Be careful: if newfile.txt already exists, > truncates its previous contents.

Use >> to append new input instead of replacing existing contents:

$ cat >> notes.txt

You want to type more text at the end of an existing notes.txt. Which command starts that operation without truncating the file?

Formatting the Output

Several options make output easier to inspect:

  • -n: Number all output lines, starting from 1.
  • -b: Number only non-empty output lines.
  • -s: Squeeze multiple blank lines into one blank line.
  • -A: Show nonprinting characters, tabs, and line endings.

Examples:

$ cat -n script.sh
$ cat -b notes.txt
$ cat -s messy.txt

Which command numbers only the nonempty output lines from notes.txt?

Choosing a Viewer for Long Files

Use cat when you want the entire output at once. For a long file, less is usually more convenient because it lets you scroll, search, and quit without flooding the terminal:

$ less /var/log/syslog

Which command is better suited to interactively reading a long log file?

To practice displaying and combining file content, try these hands-on labs:

  1. Linux cat Command: File Concatenating - Learn the cat command for viewing, concatenating, and manipulating text files, enhancing your command-line skills for efficient text file handling.
  2. Viewing Log and Configuration Files in Linux - Practice using commands like cat to efficiently view and navigate text files, including system logs and configuration files, to extract critical information.

Lesson complete

You finished cat

You can now use cat to display and combine file content while choosing safe redirection.

  • Display the complete contents of a short file.

  • Concatenate files in a chosen order.

  • Replace or append to a destination deliberately.

  • Number or simplify output lines.

  • Choose less when interactive reading is more suitable.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Command Line