Terminal GuideTerminal Guide

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 readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Display

cat file.txtDisplay file contents
cat -n file.txtShow line numbers
cat -A file.txtShow hidden chars

Combine

cat f1 f2 > outMerge files
cat f1 >> f2Append to file
cat f1 f2 f3Display multiple

Create

cat > file.txtCreate from input
cat << EOF > fileHeredoc create
tac file.txtDisplay reversed

Options

-bNumber non-empty lines
-sSqueeze blank lines
-EShow $ at line end

Downloadable Image Preview

Failed to generate preview

Basic Usage

The simplest use of cat is to display the contents of a file.

bash
cat filename.txt

You can display multiple files at once:

bash
cat file1.txt file2.txt file3.txt

Common Options

Show line numbers (-n)

bash
cat -n filename.txt

Number non-empty lines (-b)

bash
cat -b filename.txt

Show non-printing characters (-v)

bash
cat -v filename.txt

Show tabs and end of lines (-A)

bash
cat -A filename.txt

Common Options

-nNumber all output lines
-bNumber non-empty lines only
-sSqueeze multiple blank lines into one
-vShow non-printing characters
-EShow $ at end of each line
-TShow tabs as ^I
-AEquivalent 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.txt

Append to a file

bash
cat newcontent.txt >> existing.txt
Tip
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 done

Create a file with heredoc

bash
cat << EOF > config.txt
line 1
line 2
line 3
EOF

Practical Examples

View a configuration file

bash
cat /etc/hosts

Display with line numbers for debugging

bash
cat -n script.sh

Combine multiple log files

bash
cat access.log.1 access.log.2 access.log.3 > combined.log

Pipe to other commands

bash
# Count lines
cat file.txt | wc -l

# Search content
cat file.txt | grep "pattern"

# View with pagination
cat file.txt | less

Display file in reverse (tac)

bash
tac file.txt
Info
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 installed
Warning
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 file to display file contents
  • Use cat -n to show line numbers
  • Use cat file1 file2 > combined to merge files
  • Use >> to append instead of overwrite
  • Consider less or head/tail for large files

Related Articles