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.
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.txt2. 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 > FileC3. Append a file’s content to another file
Use the double‑greater‑than operator >> to add data without overwriting:
cat FileA >> FileB4. Show line numbers
Display a file with line numbers using the -n option:
cat -n File5. 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 '^$' > NewFileSummary 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
