🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

cat Command

Beginner Text Processing man(1)

Concatenate and display file contents

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
cat [OPTION]... [FILE]...

What Does cat Do?

The cat command (short for concatenate) reads, displays, and combines files. It is one of the most used commands for viewing file contents, creating files, and concatenating multiple files together.

cat reads files sequentially and writes their contents to standard output. When used with redirection (> or >>), it can create new files or append to existing ones. When given multiple filenames, it concatenates their content in order.

While cat is perfect for viewing small files, for large files consider using less, head, or tail instead. cat is commonly used in pipelines to feed file content into other commands, though tools like grep and awk can read files directly.

Options & Flags

OptionDescriptionExample
-n Number all output lines cat -n script.py
-b Number only non-empty lines cat -b code.js
-s Squeeze multiple blank lines into one cat -s document.txt
-A Show all non-printing characters (tabs as ^I, line ends as $) cat -A config.yml
-E Display $ at end of each line cat -E file.txt
-T Display TAB characters as ^I cat -T Makefile
-v Display non-printing characters cat -v binary.dat

Practical Examples

#1 Display file contents

Displays the contents of the hostname file.
$ cat /etc/hostname
Output: webserver01

#2 Concatenate multiple files

Combines three files into one new file.
$ cat header.txt body.txt footer.txt > complete.txt

#3 Display with line numbers

Shows the file with numbered lines — useful for referencing specific line numbers.
$ cat -n script.sh
Output: 1 #!/bin/bash 2 echo "Hello" 3

#4 Create a file with content

Creates a file using a here-document. Type content and end with EOF.
$ cat > notes.txt << EOF\nLine 1\nLine 2\nEOF

#5 Append to an existing file

Appends content to the end of an existing file using >>.
$ cat >> logfile.txt << EOF\nNew entry\nEOF

#6 Show hidden characters

Reveals hidden characters like tabs (^I), carriage returns (^M), and line endings ($). Essential for debugging whitespace issues.
$ cat -A config.conf
Output: server {^I$\n^Ilisten 80;$

#7 Pipe to other commands

Feeds file content through grep and wc to count 404 errors.
$ cat access.log | grep "404" | wc -l
Output: 127

Tips & Best Practices

Avoid useless use of cat: Instead of cat file | grep pattern, use grep pattern file directly. This avoids an unnecessary process (known as "UUOC" — Useless Use of Cat).
Use tac for reverse: The tac command (cat reversed) displays files with the last line first. Great for viewing recent log entries: tac /var/log/syslog | head -20
bat as a cat alternative: The bat command is a modern cat replacement with syntax highlighting, line numbers, and git integration built in.

Frequently Asked Questions

How do I view a file in Linux?
Use cat filename for small files. For large files, use less filename (scrollable) or head -n 20 filename (first 20 lines).
How do I combine two files?
Use cat file1.txt file2.txt > combined.txt. The files are concatenated in order. Use >> instead of > to append without overwriting.
How do I create a file using cat?
Type cat > filename.txt, enter your content, then press Ctrl+D to save. For multi-line content, use a here-document: cat > file.txt << EOF ... EOF

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →