cat Command Guide
The cat (concatenate) command is used to display file contents, combine files, and create new files. Learn how to use it effectively.
5 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Display
cat file.txtDisplay file contentscat -n file.txtShow line numberscat -A file.txtShow hidden charsCombine
cat f1 f2 > outMerge filescat f1 >> f2Append to filecat f1 f2 f3Display multipleCreate
cat > file.txtCreate from inputcat << EOF > fileHeredoc createtac file.txtDisplay reversedOptions
-bNumber non-empty lines-sSqueeze blank lines-EShow $ at line endDownloadable Image Preview
Failed to generate preview
Basic Usage
The simplest use of cat is to display the contents of a file.
bash
cat filename.txtYou can display multiple files at once:
bash
cat file1.txt file2.txt file3.txtCommon Options
Show line numbers (-n)
bash
cat -n filename.txtNumber non-empty lines (-b)
bash
cat -b filename.txtShow non-printing characters (-v)
bash
cat -v filename.txtShow tabs and end of lines (-A)
bash
cat -A filename.txtCommon Options
| -n | Number all output lines |
| -b | Number non-empty lines only |
| -s | Squeeze multiple blank lines into one |
| -v | Show non-printing characters |
| -E | Show $ at end of each line |
| -T | Show tabs as ^I |
| -A | Equivalent to -vET |
Concatenating Files
The original purpose of cat is to concatenate (combine) files.
Combine files into one
bash
cat file1.txt file2.txt > combined.txtAppend to a file
bash
cat newcontent.txt >> existing.txtTip
Use
> to overwrite and >> to append.Creating Files
Create a file from input
bash
cat > newfile.txt
Type your content here
Press Ctrl+D when doneCreate a file with heredoc
bash
cat << EOF > config.txt
line 1
line 2
line 3
EOFPractical Examples
View a configuration file
bash
cat /etc/hostsDisplay with line numbers for debugging
bash
cat -n script.shCombine multiple log files
bash
cat access.log.1 access.log.2 access.log.3 > combined.logPipe to other commands
bash
# Count lines
cat file.txt | wc -l
# Search content
cat file.txt | grep "pattern"
# View with pagination
cat file.txt | lessDisplay file in reverse (tac)
bash
tac file.txtInfo
tac is cat spelled backwards and displays files in reverse line order.Alternatives to cat
For large files or specific use cases, consider these alternatives:
bash
# View beginning of a file
head -n 20 file.txt
# View end of a file
tail -n 20 file.txt
# View with pagination
less file.txt
# View with syntax highlighting
bat file.txt # if installedWarning
Avoid using cat for very large files as it will load everything into memory. Use
less or head/tail instead.Summary
The cat command is versatile for viewing and combining files. Key takeaways:
- Use
cat fileto display file contents - Use
cat -nto show line numbers - Use
cat file1 file2 > combinedto merge files - Use
>>to append instead of overwrite - Consider
lessorhead/tailfor large files
Related Articles
grep Command: Practical Examples & Usage Guide
Master grep with practical examples for developers. Learn pattern matching, regex, and real-world use cases.
linux12 min read
sed Command Guide
Stream editor for text transformation
linux8 min read
awk Command Guide
Text processing and data extraction
linux10 min read