cat – Concatenate and Display File Contents
The cat command (short for “concatenate”) reads files sequentially, writing their contents to standard output. It is commonly used to display file contents, combine files, or create new files.
Syntax
cat [OPTION]... [FILE]...
- If no files are specified,
catreads from standard input (keyboard) until EOF (Ctrl+D).
Options
| Option | Description |
|---|---|
-n |
Number all output lines |
-b |
Number non-empty output lines only |
-s |
Squeeze multiple adjacent empty lines into one |
-E |
Display $ at the end of each line |
-T |
Display TAB characters as ^I |
-v |
Show non-printing characters (except tabs and line ends) |
Practical Examples
1. Display the contents of a file
cat filename.txt
This prints the entire contents of filename.txt to the terminal.
2. Display contents of multiple files concatenated
cat file1.txt file2.txt
This outputs the contents of file1.txt followed immediately by file2.txt.
3. Number all lines in a file
cat -n filename.txt
Sample Output:
1 First line of the file
2 Second line of the file
3 Third line of the file
4. Number only non-empty lines
cat -b filename.txt
5. Squeeze multiple empty lines into a single empty line
cat -s filename.txt
6. Show line ends explicitly with $
cat -E filename.txt
7. Display TAB characters as ^I
cat -T filename.txt
8. Combine options (e.g., number non-empty lines and show line ends)
cat -bE filename.txt
9. Create a new file using cat and keyboard input
cat > newfile.txt
Type your content, then press Ctrl+D to save and exit.
10. Append text to an existing file
cat >> existingfile.txt
Type the additional content and press Ctrl+D when finished.
Tips
- For very large files, use
lessormoreinstead ofcatto view content page by page. - Combine
catwith other commands using pipes. - To view hidden/control characters in files, use
cat -v.
cat file.txt | grep "search_term"
See Also
man cat– the manual page forcattac– reverse version ofcathead/tail– view the beginning or end of files