Bash Cat Command
Concatenate and display file contents
📄 What is the Cat Command?
The cat command (concatenate) displays file contents, combines multiple files, and creates new files. It's one of the most frequently used commands for viewing and manipulating text files in Linux and Unix systems.
# Display file contents
cat filename.txt
Common Cat Operations
View Files
Display file contents
cat file.txt
Combine Files
Merge multiple files together
cat file1.txt file2.txt
Create Files
Make new files with content
cat > newfile.txt
Number Lines
Show line numbers in output
cat -n file.txt
🔹 Display File Contents
The cat command displays file contents directly in the terminal for quick viewing. Use cat filename to output entire file contents sequentially. This is ideal for examining configuration files, logs, scripts, or documentation. For large files, combine with less or more for paginated viewing. The simple syntax and immediate output make cat one of the most frequently used file inspection tools, providing quick access to file contents without opening full text editors.
# View a single file
cat document.txt
# View multiple files
cat file1.txt file2.txt file3.txt
Output:
Hello, this is the content of document.txt Line 2 of the file Line 3 of the file
🔹 Concatenate Multiple Files
cat combines multiple files into a single output stream for unified processing or viewing. Use cat file1 file2 > combined to merge files permanently or cat file1 file2 | less for temporary viewing. This concatenation capability is valuable for combining log files, configuration fragments, or data chunks. The operation preserves original file order and content, creating continuous output that can be redirected to new files or piped to other commands for further processing and analysis.
# Combine and display two files
cat header.txt content.txt
# Save combined output to new file
cat file1.txt file2.txt > combined.txt
Output:
Contents of header.txt Contents of content.txt (Both files displayed together)
🔹 Create New Files
cat with output redirection quickly creates files without opening text editors. Use cat > newfile to start input mode, type content, then press Ctrl+D to save. Alternatively, use cat << EOF > file for multi-line input with a termination marker. This method is perfect for creating short configuration files, scripts, or notes directly from the command line. The immediate feedback and keyboard-only operation streamline file creation during administrative tasks or quick documentation.
# Create a new file (type content, then press Ctrl+D)
cat > newfile.txt
# Append to existing file
cat >> existingfile.txt
How it works:
- Type: cat > filename.txt
- Enter your text content
- Press Ctrl+D to save and exit
- Use >> to append instead of overwrite
🔹 Display with Line Numbers
The cat -n option prefixes each line with numbers for easy reference and debugging. This numbering helps identify specific lines in configuration files, source code, or data files during discussions, error reporting, or editing. Combined with grep (cat -n file | grep pattern), it quickly locates and references pattern occurrences. Line numbers transform cat from simple content display to a lightweight code review and debugging tool that facilitates precise communication about file contents.
# Show line numbers for all lines
cat -n file.txt
# Show line numbers only for non-empty lines
cat -b file.txt
Output:
1 First line of text
2 Second line of text
3 Third line of text
🔹 Useful Cat Options
cat offers several options that modify output formatting and behavior for different use cases. Common options include -n for line numbers, -b for numbering non-blank lines, -s to squeeze consecutive blank lines, and -v to display non-printing characters. These options adapt cat for code review, log analysis, binary file inspection, and clean output presentation. Understanding available options maximizes cat's utility across various file types and viewing requirements.
Common Options:
- -n : Number all output lines
- -b : Number non-empty lines only
- -s : Squeeze multiple blank lines into one
- -E : Display $ at end of each line
- -T : Display TAB characters as ^I
- -A : Show all non-printing characters
# Squeeze blank lines
cat -s file.txt
# Show line endings
cat -E file.txt
# Combine multiple options
cat -nE file.txt
🔹 Practical Examples
Everyday uses of head include validating data files, monitoring log heads, and creating data samples. A data scientist might run head -1000 large_dataset.csv > sample.csv to create a manageable sample for testing scripts. A developer might use head -20 application.log to check recent activity after a deployment. These scenarios highlight head's role as a gatekeeper, providing a safe, controlled way to interact with the beginning of files and streams, which is often where the most current or defining information resides.
# View log file
cat /var/log/syslog
# Combine config files
cat config1.conf config2.conf > final.conf
# Create a quick note
cat > note.txt
This is my note
Press Ctrl+D to save
# Display file with page breaks
cat file.txt | less
# Count lines in a file
cat file.txt | wc -l