Introduction
Hello! In this article, we’ll learn about the cat command.
cat is a command for displaying file contents, and it’s one of the absolute must-know basic commands in Linux. The name comes from “concatenate,” but most of the time we just use it to view files.
What is the cat Command?
cat is a command that displays file contents to standard output (the screen) or concatenates multiple files together.
When you want to quickly check what’s inside a text file or combine multiple files into one, this is what you use. Super simple, but used incredibly frequently.
Basic Syntax
|
|
Main Options
| Option | Description |
|---|---|
-n |
Number all output lines |
-b |
Number non-empty output lines only |
-s |
Squeeze multiple adjacent blank lines into one |
-E |
Display $ at end of each line |
-T |
Display TAB characters as ^I |
-A |
Equivalent to -vET (show all special characters) |
-v |
Make non-printing characters visible |
Usage Examples
Example 1: Display File Contents
|
|
Output:
|
|
This is the most basic usage. The file contents are displayed on the screen.
Example 2: Display Multiple Files
|
|
Output:
|
|
When you specify multiple files, they’re concatenated and displayed in order.
Example 3: Display with Line Numbers
|
|
Output:
|
|
The -n option adds numbers to all lines. Note that blank lines also get numbers.
Example 4: Number Non-Empty Lines Only
|
|
Output:
|
|
The -b option doesn’t number blank lines. This is often more readable.
Example 5: Create a New File
|
|
After running this, it waits for input. Type whatever you want.
Press Ctrl + D to save and exit.
|
|
Handy for creating simple files.
Example 6: Append to a File
|
|
Using >> appends to an existing file.
Also press Ctrl + D to save.
Example 7: Concatenate and Save Files
|
|
You can combine multiple files into one and save to a new file. Useful when you want to merge log files.
Example 8: Visualize Line Endings
|
|
Output:
|
|
The -E option displays $ at the end of each line.
Useful for checking line endings or trailing spaces.
Example 9: Visualize Tabs
|
|
Output:
|
|
The -T option displays tabs as ^I.
Use this when you can’t tell spaces from tabs.
Example 10: Combine with Pipes
|
|
Often used in combination with other commands. This example displays only lines containing “error” from the log file.
Tips & Notes
-
Be careful with large files:
catdisplays all content at once, so huge files will scroll like crazy. In those cases, uselessormoreinstead1less bigfile.txt # Better choice -
Read from standard input: If you don’t specify a filename, it reads from standard input
1 2echo "test" | cat # Output: test -
Mix files and standard input with dash:
-represents standard input1 2cat file1.txt - file2.txt # Displays file1, then stdin, then file2 -
Don’t display binary files: Using
caton binary files can mess up your terminal display. If you accidentally do this, use theresetcommand to fix it -
Redirection caution: Reading and writing the same file will erase its contents
1cat file.txt > file.txt # Don't do this! File will be emptied
Practical Usage
Check Log Files
|
|
When you want to see just the last 20 lines of a log.
Check Configuration Files
|
|
Quickly check your bash configuration file.
Create File with Here Document
|
|
You can create multi-line files in one go. This is a common technique in scripts.
Combine Multiple Logs
|
|
Useful for merging split log files into one.
Code Review with Line Numbers
|
|
When discussing specific lines of code, line numbers are helpful.
Summary
In this article, we learned about the cat command.
Key Points:
catis a basic command for displaying file contents- Can concatenate multiple files (that’s where the name comes from!)
-nfor line numbers,-Eto visualize line endings- Combine with redirection to create/concatenate files
- For large files,
lessormoreare better choices
Simple but super frequently used command. When you want to quickly peek at a file, just cat it.
Once you learn to use cat together with less, head, and tail, you’ll work much more efficiently.
Keep learning Linux commands!