logo
search
list

Table of Content

What Cat Is For
Basic: Print a File
Create or Overwrite a File (cat >)
Append to a File (cat >>)
Heredoc: Write Multi-Line Content in One Shot
Concatenate Files into One
Mix Files and Standard Input
Useful Cat Options (GNU)
Cat in Pipelines
Cat vs Less (and Related Tools)
Permissions and Common Errors
Quick Cheat Sheet
From Terminal Text to Shareable Documents
FAQ

Cat Command in Linux: View and Join Files

Posted by Algirdas Jasaitis

calendar

2026-08-12

views

871

likes

4

The cat command in Linux concatenates files and writes them to standard output. With one file it simply prints that file; with several files it prints them one after another. On Ubuntu and other GNU/Linux systems (GNU coreutils):

cat [OPTION]... [FILE]...

With no FILE, or when FILE is -, cat reads standard input. Success means the content appears on your terminal (or in a redirected file). For long files, prefer less or more so you can scroll—Ubuntu’s CLI tutorials recommend less for paging.

Official reference: Ubuntu man page cat(1) — run man cat or cat --help locally.

If you already know your goal, jump to: create a file, append, concatenate, line numbers, or cat vs less.

Building a broader CLI toolkit? See essential Linux commands and the cheat sheet.

Concept illustration of the Linux cat command concatenating and displaying text files in a terminal
Use cat to print or join text files—redirect with > or >> to create and append, and prefer less for long logs.

What Cat Is For

UseExample idea
Show a short filecat notes.txt
Join filescat a.txt b.txt > all.txt
Create/overwrite a filecat > new.txt then type, Ctrl+D
Append to a filecat >> log.txt
Pipe content into another commandcat file | grep error
Reveal hidden characterscat -A file / cat -T file

cat does not open an interactive editor (use nano/vim) and is a poor pager for huge logs.

Basic: Print a File

cat file.txt
cat /etc/hostname

Multiple files print in order:

cat part1.txt part2.txt part3.txt

Create or Overwrite a File (cat >)

cat > hello.txt
Hello from cat
This is line 2

Press Ctrl+D (EOF) to finish. This overwrites hello.txt if it already exists.

Safer for beginners: confirm the path before using > so you do not wipe an important file.

Append to a File (cat >>)

cat >> hello.txt
Extra line added later

Ctrl+D again. >> appends; > truncates.

Heredoc: Write Multi-Line Content in One Shot

Common shell pattern (also used in install scripts):

cat > config.env << 'EOF'
NAME=demo
DEBUG=true
EOF

Quoted 'EOF' keeps variables from expanding. Unquoted EOF allows $VAR expansion.

Concatenate Files into One

# Print joined content on screen
cat chapter1.md chapter2.md

# Write joined content to a new file
cat chapter1.md chapter2.md > book.md

# Append more files onto an existing file
cat chapter3.md >> book.md

Name comes from catenate—this is the original purpose.

Mix Files and Standard Input

From the man page examples:

# Print f, then whatever you type, then g
cat f - g

# Copy stdin to stdout (filter/pass-through)
cat

- means “read stdin at this position in the list.”

Useful Cat Options (GNU)

OptionMeaning
-n / --numberNumber all output lines
-b / --number-nonblankNumber nonempty lines only (overrides -n)
-s / --squeeze-blankSqueeze repeated blank lines
-E / --show-endsShow $ at end of each line
-T / --show-tabsShow tabs as ^I
-v / --show-nonprintingShow non-printing chars (^ / M- notation)
-A / --show-allSame as -vET
-eSame as -vE
-tSame as -vT
-uIgnored (POSIX compatibility)

Examples:

cat -n script.sh              # numbered lines
cat -b messy.txt              # skip numbering blank lines
cat -s doublespaced.txt       # squeeze blank lines
cat -A weird.txt              # see tabs, line ends, odd chars

Cat in Pipelines

cat access.log | grep "404"
cat *.conf | wc -l

Often you can skip cat and pass the file to the next tool (grep "404" access.log)—a classic Unix efficiency tip. Keep cat when you need to join multiple files first or when teaching stdin flow.

Cat vs Less (and Related Tools)

ToolBest for
catShort files; joining; simple create/append
lessLong files you need to scroll (q to quit)
tacReverse line order (GNU)
less big.log
cat -n script.sh
zcat file.txt.gz | less

Ask Ubuntu and Ubuntu docs often point people to less when cat floods the terminal. For the last lines of a log—or live follow—use the tail command in Linux guide instead of stretching cat.

Permissions and Common Errors

cat: file.txt: No such file or directory
cat: secret.key: Permission denied
  • Check the path with ls / pwd
  • Use sudo cat /etc/shadow only when you truly need root-readable files—avoid redirecting sudo output wrongly:
# Wrong: shell opens out.txt as you, before sudo
sudo cat /etc/hosts > /root/out.txt

# Better pattern
sudo tee /root/out.txt < /etc/hosts >/dev/null
# or
sudo bash -c 'cat /etc/hosts > /root/out.txt'

Redirection under root is easier with tee—covered in depth in the sudo and echo guides.

Quick Cheat Sheet

cat file.txt                      # view
cat a.txt b.txt > c.txt           # concatenate
cat > new.txt                     # create (Ctrl+D to end)
cat >> new.txt                    # append
cat -n file.txt                   # number lines
cat -A file.txt                   # show tabs/ends/nonprinting
man cat                           # full manual

From Terminal Text to Shareable Documents

cat is perfect for printing, joining, and redirecting plain text. Once that text needs to become a formatted handoff, you usually want a desktop document instead of another shell redirect.

WPS Office for Linux fills that next step with Writer, Spreadsheet, Presentation, and PDF tools on Ubuntu.

WPS Office Writer on a Linux desktop editing an incident report DOCX with a familiar ribbon toolbar
WPS Writer on Linux: turn setup notes, terminal findings, and troubleshooting steps into a DOCX or PDF that teammates can open easily.
What you need nextWhat WPS gives you
Move plain text into DOCX, sheets, or PDFsStrong .docx / .xlsx / .pptx layout fidelity when files move between Linux, Windows, and macOS
One place for notes, sheets, slides, and PDFsFree core suite: Writer, Spreadsheet, Presentation, plus PDF view/edit/convert tools
A familiar desktop UI for switchersRibbon-style apps with a lighter footprint than many heavy suites
A trusted install pathOfficial DEB (Ubuntu/Debian/Mint) and RPM packages from the vendor

Download the package from the official WPS Office for Linux download page, install it with sudo apt install ./wps-office_VERSION_amd64.deb, then paste your terminal text into Writer or export it as PDF when it needs a cleaner final form.

FAQ

What does the cat command do in Linux?
It concatenates files and prints them to standard output—commonly used to display or join text files.

How do I create a file with cat?
cat > filename, type content, then Ctrl+D. Use >> to append instead of overwrite.

How do I merge two files with cat?
cat file1 file2 > merged.txt

When should I use less instead of cat?
When the file is long and you need scrolling, search, or to avoid flooding the terminal.

What does cat -n do?
Numbers all output lines. Use cat -b to number only nonempty lines.

Where is the official documentation?
Run man cat or see the Ubuntu cat(1) man page.

Algirdas Jasaitis

15 years of office industry experience, tech lover and copywriter. Follow me for product reviews, comparisons, and recommendations for new apps and software.