Fundamentals 7 min read

Master the Linux cat Command: From Simple Viewing to Advanced Redirection

This guide walks you through the Linux cat command, explaining its basic file‑display function, how to create and append files, redirect content, show line numbers, and remove empty lines, all with clear examples and practical tips for everyday shell use.

21CTO
21CTO
21CTO
Master the Linux cat Command: From Simple Viewing to Advanced Redirection

In Linux, cat is not a cat but a command that prints the contents of text files to the terminal.

Using the cat command

The basic syntax is: cat [options] Filename(s) [options] modify cat’s default behavior, e.g., -n to number lines. Filename is the name of the file to read.

1. Create a new file

You can create a file with cat > Filename. After running the command, a blinking cursor appears; type the desired content and press Ctrl+D to save. Pressing Ctrl+D immediately creates an empty file.

cat > NewFile.txt

2. Copy file contents to another file

Redirect the output of one file to another: cat FileA > FileB This overwrites FileB with the contents of FileA. You can also copy multiple files at once:

cat FileA FileB > FileC

3. Append a file’s content to another file

Use the double‑greater‑than operator >> to add data without overwriting:

cat FileA >> FileB

4. Show line numbers

Display a file with line numbers using the -n option:

cat -n File

5. Delete empty lines

Pipe cat’s output to grep -v '^$' to filter out blank lines: cat File | grep -v '^$' You can redirect the filtered result to a new file:

cat File | grep -v '^$' > NewFile

Summary of common cat usages

cat <Filename>

– Print file contents to the terminal. cat > File – Create a new file (or overwrite) and write content. cat FileA > FileB – Replace FileB with FileA 's contents. cat FileA >> FileB – Append FileA to FileB. cat -n File – Show line numbers. cat File | more – Page through a large file. cat File | less – Page with bidirectional scrolling. cat File | grep -v '^$' – Remove all empty lines.

Practice using different options and files to reinforce what you’ve learned.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-linefile-manipulationcat command
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.