cat — Linux basic command
Linux / Basic Linux commands / cat.html
A command that displays the contents of text files on the screen or combines multiple files into one.
Basic Information
| Command Name | cat (concatenate) |
|---|---|
| One-line Introduction | A command that displays the contents of text files on the screen or combines multiple files into one. |
| Provided By | GNU coreutils (pre-installed in all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/cat.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL |
| Installation Status | Pre-installed (no separate installation required) |
| Category | Basic Linux command (tags: file output, text, Linux, basics) |
What is this command?
cat is short for concatenate, a command that displays the contents of text files on the screen or concatenates multiple files into one. It is most frequently used to quickly view the contents of short files.
When used with redirection (>, >>), it can save or merge file contents into another file.
What problem does it solve?
- Check the contents of short files.
- Combine multiple files into one.
- Copy file contents to another file.
- Display the contents of multiple files in sequence.
Key Features
Display file contents
Displays the file contents exactly as they are on the screen.
- Usage scenario: When checking short configuration or log files.
- Result obtained: The entire file content is displayed at once.
- Limitations: Long files are displayed all at once, making scrolling difficult.
Concatenate multiple files
Concatenates the contents of multiple files in order and displays them.
- Usage scenario: When checking multiple log files at once.
- Result obtained: All files are merged and displayed in sequence.
- Limitations: Large numbers of files may result in large volumes.
Create and copy files
Saves file contents to another file using redirection.
- Usage scenario: When copying a file or creating an empty file.
- Result obtained: A new file is created.
- Limitations: Be careful as it overwrites the contents of existing files.
Multiple-line input
Inputs multiple lines of text at once and saves them to a file.
- Usage scenario: When quickly creating a simple text file.
- Result obtained: A multi-line file is created.
- Limitations: For long documents, it's better to use an editor.
Installation and Execution
Check if pre-installed
It is included by default in all Linux distributions.
which cat
cat --version
Alternatives for long files
For long files, it's better to use less or head/tail.
# Use less for long files
less long-file.log
# First 10 lines only
head long-file.log
# Last 10 lines only
tail long-file.log
Practical Usage Examples
Basic file output
Check file contents.
# Display file contents
cat /etc/hostname
# Display multiple files in sequence
cat part1.txt part2.txt part3.txt
File copying and creation
Copy or create files using cat.
# Copy a file
cp original.txt backup.txt
cat original.txt > backup.txt
# Create an empty file
touch empty.txt
# or
cat /dev/null > empty.txt
# Combine multiple files
cat file1.txt file2.txt > combined.txt
Multiple-line text input
Save multiple lines to a file at once.
# Multiple-line input using here-document
cat > message.txt << EOF
Hello
This is a test file
Thank you
EOF
# Check contents
cat message.txt