cat Command Explained
cat is one of the oldest and most reflexively used commands in Unix, and its name gives away its real purpose more directly than most people realize: it concatenates.
Basic usage: viewing a file
cat notes.txt
# prints the entire contents directly to the terminal
For a short file, this is the fastest way to see what is inside it: no pager, no editor, just the raw content dumped straight to your screen.
The name is literal: concatenating files
cat file1.txt file2.txt
# prints file1.txt's contents immediately followed by
# file2.txt's contents, as one continuous stream
cat file1.txt file2.txt file3.txt > combined.txt
# creates combined.txt containing all three files joined together, in order
This is not just a display trick. cat genuinely treats multiple input files as one combined stream, which is exactly why redirecting its output with > produces a real, single file containing everything from all the sources listed. This is the origin of the name: con-cat-enate.
# Append rather than overwrite the destination
cat extra-notes.txt >> existing-notes.txt
>> appends the new content to the end of an existing file instead of replacing it entirely, which > would do.
Numbering lines
cat -n script.sh
# 1 #!/bin/bash
# 2 echo "starting up"
# 3
# 4 systemctl restart nginx
cat -b script.sh
# 1 #!/bin/bash
# 2 echo "starting up"
# 3 systemctl restart nginx
# (blank line 3 above is skipped in the numbering with -b)
-n numbers every line, including blank ones. -b numbers only non-blank lines, which can make a file with a lot of intentional spacing easier to reference without the line numbers drifting away from where the actual content is.
Why cat is the wrong tool for large files
cat /var/log/syslog
# floods your terminal with potentially hundreds of thousands
# of lines, scrolling past faster than you can read, with no
# way to search or go back
cat has no concept of pausing, scrolling, or searching. It reads the entire file and writes it out as fast as the terminal can display it. For anything beyond a short file, this makes the output effectively unusable for actually reading. A pager like less exists specifically to solve this: it loads content progressively, lets you scroll in both directions, and supports searching with /.
# Better for large files:
less /var/log/syslog
Quickly creating a file without an editor
cat > quick-notes.txt
This is the first line.
This is the second line.
# press Ctrl+D here to save and exit
Running cat with output redirected to a new filename and no input file specified reads from your keyboard (standard input) until you signal the end with Ctrl+D. Whatever you typed becomes the contents of the new file. This is a handy shortcut during an SSH session when you need to create a short config file or script and do not want to open a full text editor for just a few lines.
# A related, more structured pattern using a heredoc
cat > config.ini << 'EOF'
[settings]
debug = false
port = 8080
EOF
The heredoc form (<< 'EOF' … EOF) is more common in scripts because it clearly marks the beginning and end of the inserted content and can include variables or be quoted to prevent variable expansion, depending on whether the delimiter is quoted.
Useful flag combinations
cat -A file.txt # show non-printing characters, including line endings
cat -s file.txt # squeeze multiple blank lines into a single blank line
cat -A script.sh | grep '\$$' # find lines ending with a literal dollar sign
-A is particularly useful for diagnosing hidden formatting issues, such as Windows-style line endings (^M$ at the end of each line) that can cause a script written on Windows to behave strangely when run on Linux.
Frequently Asked Questions
What does the cat command do?
cat, short for concatenate, prints the contents of one or more files directly to the terminal. Given a single file, it simply displays that file’s contents. Given multiple files, it prints them one after another as if they were joined together into a single continuous stream of text.
Why is it called cat instead of something like show or print?
The name reflects its original and still-valid core purpose: concatenating files together. cat file1.txt file2.txt does not just display two files back to back for reading; it genuinely joins their contents into one combined stream, which is why cat file1.txt file2.txt > combined.txt is a standard way to merge two files into a new third file.
How do I combine multiple files into one new file with cat?
Redirect the output using the > operator, such as cat file1.txt file2.txt file3.txt > combined.txt, which creates combined.txt containing the contents of all three source files in the order listed. Using >> instead of > appends to an existing file rather than overwriting it.
Why shouldn’t I use cat to view a very large file?
cat prints the entire file to the terminal all at once with no pausing, no scrolling, and no way to navigate backward once text has scrolled past. For a file with thousands or millions of lines, this floods your terminal and makes it effectively impossible to read anything useful. A pager like less is designed specifically for viewing large files interactively, one screen at a time, with search and backward navigation.
What does cat -n do?
cat -n prefixes every line of output with its line number, which is useful when you need to reference a specific line in a file, for example when discussing an error message that points to a particular line, or when comparing output against another numbered listing. cat -b does something similar but only numbers non-blank lines, skipping empty lines in the count.
What is a common trick using cat to quickly create a small file without opening an editor?
cat > filename.txt followed by typing content directly into the terminal and pressing Ctrl+D creates a new file with exactly what you typed, without launching a text editor at all. This is often called a heredoc-adjacent trick, useful for quickly writing a short config file or script directly from the command line during a remote SSH session.