Command: cat (Concatenate)
The cat command is one of the most frequently used tools in the Linux terminal. Its primary purpose is to read file data and display it on the screen, but its name actually comes from its ability to concatenate (join) multiple files together.
1. The "Why"
When you are working on your app or your LaTeX book, you often need to quickly check the contents of a file without waiting for a heavy editor like VS Code to open. cat is the "instant preview" tool of the terminal.
You use it to:
- Quickly read configuration files (like your Hyprland config).
- Combine several small text files into one large document.
- Pipe text into other commands for processing.
2. Basic Syntax
cat [OPTIONS] [FILE]
- Example:
cat README.mdprints the entire content of the README to your terminal.
3. Most Useful Flags
| Flag | Name | What it does |
|---|---|---|
-n |
Number | Displays line numbers next to each line of text. |
-b |
Number Non-blank | Numbers only the lines that are not empty. |
-s |
Squeeze | Compresses multiple consecutive empty lines into a single empty line. |
-E |
Show Ends | Displays a $ at the end of every line (helps find hidden spaces). |
-A |
Show All | Shows everything, including tabs and end-of-line characters. |
4. Practical Examples
A. Reading a Single File
cat main.java
B. Combining (Concatenating) Files
If you have your e-book chapters in separate files and want to see them all at once:
cat chapter1.tex chapter2.tex chapter3.tex
C. Creating a New File from Multiple Sources
You can use the redirection operator (>) to merge files into a new one:
cat part1.txt part2.txt > full_report.txt
D. Viewing with Line Numbers
This is very helpful when debugging code or referencing a specific line in a tutorial:
cat -n script.sh
5. Pro-Tips
- The "Big File" Warning: If a file is very long (thousands of lines),
catwill dump it all at once, and you'll have to scroll up forever. For large files, uselessinstead ofcat. - Quick File Creation: You can use
catto quickly write a few lines into a file without an editor:cat > note.txt (Type your text here) (Press Ctrl+D to save and exit) - Arch Linux Context: You'll use this often to check your system logs or verify that a configuration change you made with a script actually worked (e.g.,
cat /etc/fstab). - Piping:
catis often the first step in a chain of commands:cat data.txt | grep "Error"
6. Summary Reference
| Goal | Command |
|---|---|
| View file content | cat filename |
| View with line numbers | cat -n filename |
| Merge two files | cat file1 file2 > newfile |
| Append file1 to file2 | cat file1 >> file2 |
| Clear multiple empty lines | cat -s filename |