What Does Cat Do In Linux – Cat Command File Viewing

Typing cat in Linux lets you view a file’s contents without opening a text editor. If you’ve ever wondered “what does cat do in linux,” the answer is simple: it reads files and outputs their content to the terminal. This command is one of the most basic yet powerful tools in the Linux toolbox.

What Does Cat Do In Linux

The cat command, short for “concatenate,” does more than just display files. It can combine multiple files, create new ones, and even append data. Think of it as a Swiss Army knife for text files—quick, versatile, and always ready.

You use it daily without thinking. But knowing its full potential saves time and effort. Let’s break down everything cat can do, from the basics to advanced tricks.

Basic Syntax Of The Cat Command

The syntax is straightforward:

cat [options] [file1] [file2] ...

You type cat, then any options, then the file names. No options? No problem. Just cat filename prints the file to your screen.

How To View A File With Cat

Open your terminal and type:

cat myfile.txt

Press Enter. The entire contents of myfile.txt appear. If the file is long, it scrolls past quickly. For longer files, you might want to pipe it to less or more:

cat longfile.txt | less

This lets you scroll page by page. Press q to quit.

Creating Files With Cat

You can create a new file directly from the terminal:

cat > newfile.txt

Type your content, then press Ctrl+D to save and exit. This overwrites any existing file with the same name. Be careful!

To append to an existing file, use >>:

cat >> existingfile.txt

Now type more content and press Ctrl+D. The new text gets added to the end.

Combining Multiple Files

This is where cat shines. Combine two or more files into one:

cat file1.txt file2.txt > combined.txt

The output of both files gets written into combined.txt. You can combine as many files as you want:

cat part1.txt part2.txt part3.txt > full_document.txt

This is perfect for merging log files or assembling chapters.

Numbering Lines With Cat

Sometimes you need line numbers. Use the -n option:

cat -n myfile.txt

Every line gets a number at the start. This helps when debugging scripts or referencing specific lines.

If you only want to number non-blank lines, use -b:

cat -b myfile.txt

Blank lines stay unnumbered, which looks cleaner for certain documents.

Showing Non-Printable Characters

Hidden characters like tabs or end-of-line markers can cause issues. Use -A to show everything:

cat -A myfile.txt

This displays:

  • Tabs as ^I
  • End of lines as $
  • Other non-printable characters as ^ sequences

It’s a lifesaver when dealing with files from Windows or weird encoding.

Squeezing Blank Lines

Too many empty lines? Use -s to squeeze them:

cat -s myfile.txt

Multiple consecutive blank lines get reduced to one. Your output looks cleaner.

Using Cat With Pipes

Cat is often used with other commands via pipes. For example, count words in a file:

cat myfile.txt | wc -w

Or search for a pattern:

cat myfile.txt | grep "error"

This sends cat’s output to grep, which filters lines containing “error”. You can chain multiple commands:

cat logfile.txt | grep "ERROR" | sort | uniq -c

This finds all ERROR lines, sorts them, and counts unique occurrences.

Cat For Binary Files

Cat works with binary files too, but the output is garbage. However, you can use cat to copy binary files:

cat image.jpg > copy.jpg

This creates an exact copy. For large files, dd or cp is faster, but cat works in a pinch.

Common Cat Options Summary

Here’s a quick reference:

  • -n: Number all lines
  • -b: Number non-blank lines
  • -s: Squeeze blank lines
  • -E: Show $ at end of each line
  • -T: Show tabs as ^I
  • -A: Equivalent to -vET (show all)
  • -v: Show non-printing characters except tabs and end-of-line

Cat Vs Other Commands

You might wonder: why use cat when less or head exist? Each has its place:

  • cat: Best for small files or when piping to other commands
  • less: Better for viewing long files interactively
  • head/tail: Show only the beginning or end of a file
  • tac: Reverse cat—prints lines in reverse order

Cat is not always the right choice. For huge files, avoid cat because it dumps everything to memory. Use less or head instead.

Practical Examples Of Cat

Let’s see cat in action with real-world tasks.

Example 1: Viewing System Files

Check your system’s CPU info:

cat /proc/cpuinfo

Or memory info:

cat /proc/meminfo

These virtual files give you hardware details instantly.

Example 2: Combining Log Files

You have multiple log files from different servers. Merge them into one:

cat server1.log server2.log server3.log > all_logs.log

Then search the combined file:

cat all_logs.log | grep "404"

Example 3: Creating A Simple Script

Write a bash script directly:

cat > myscript.sh << EOF
#!/bin/bash
echo "Hello, World!"
EOF

This uses a here document (<< EOF) to write multiple lines. Then make it executable:

chmod +x myscript.sh

Example 4: Displaying File With Line Numbers

When debugging code:

cat -n script.py

You see line numbers, making it easy to reference errors.

Cat With Here Documents

Here documents let you pass multi-line input to cat. The syntax:

cat << EOF
Line 1
Line 2
Line 3
EOF

This prints the lines between << EOF and EOF. You can use any delimiter, like END or STOP.

This is great for generating configuration files or scripts on the fly.

Cat And Redirection

Redirection is cat's best friend. Common patterns:

  • cat file1 > file2: Copy file1 to file2 (overwrites)
  • cat file1 >> file2: Append file1 to file2
  • cat file1 file2 > file3: Combine file1 and file2 into file3
  • cat < file1: Read from file1 (same as cat file1)

You can also redirect input from a file:

cat < input.txt > output.txt

This reads from input.txt and writes to output.txt. It's redundant but works.

Cat With Standard Input

If you run cat without a filename, it reads from standard input (your keyboard):

cat

Type something and press Enter. Cat echoes it back. Press Ctrl+D to exit. This is useful for quick note-taking or testing.

Common Mistakes With Cat

Even experienced users make errors. Here are pitfalls to avoid:

  • Overwriting files accidentally: Using > instead of >> destroys existing data.
  • Forgetting to use quotes: Filenames with spaces need quotes: cat "my file.txt"
  • Using cat unnecessarily: Some commands accept filenames directly. Instead of cat file | grep pattern, use grep pattern file.
  • Binary file corruption: Don't redirect binary output to a terminal—it may mess up your display.

Cat In Scripts

Cat is common in shell scripts for reading configuration files or generating output. For example:

#!/bin/bash
if [ -f /etc/config.txt ]; then
    cat /etc/config.txt
else
    echo "Config file not found"
fi

This script checks if a file exists, then displays it with cat.

Performance Considerations

Cat reads the entire file into memory before outputting. For huge files (GBs), this can be slow. Alternatives:

  • Use head or tail for partial reads
  • Use less for interactive browsing
  • Use dd for binary copies

For most day-to-day tasks, cat is fast enough.

Cat And The Useless Use Of Cat Award

You might hear about the "Useless Use of Cat Award" (UUOC). This refers to patterns like:

cat file | command

When you could just do:

command file

For example, instead of cat file | grep foo, use grep foo file. It's more efficient and avoids an extra process. But cat is still useful when you need to combine multiple files or use here documents.

Advanced Cat Tricks

Here are some lesser-known uses:

Copying Multiple Files To A Directory

You can't do this directly with cat, but combine with tee:

cat file1 file2 | tee file3 file4 > /dev/null

This writes to both file3 and file4.

Creating A File With Specific Content

Use cat with a here document and variable expansion:

name="John"
cat << EOF > greeting.txt
Hello, $name!
Welcome to Linux.
EOF

Variables get expanded. To prevent expansion, quote the delimiter: cat << 'EOF'.

Using Cat With Sudo

To view protected files:

sudo cat /etc/shadow

But be careful—this file contains password hashes.

Cat Alternatives

If cat doesn't fit your needs, try:

  • tac: Reverse order
  • rev: Reverse characters on each line
  • nl: Number lines with more options
  • od: Octal dump for binary files
  • xxd: Hex dump

Each has specific use cases.

Frequently Asked Questions

What is the difference between cat and less?

Cat outputs the entire file at once. Less shows it page by page, allowing scrolling. Use cat for small files or piping, less for reading long files.

Can cat damage my files?

Cat itself doesn't modify files. But redirection with > can overwrite files if you're not careful. Always double-check your command.

How do I exit cat?

If cat is reading from standard input, press Ctrl+D to send EOF. If viewing a file, it exits automatically when done.

Why is cat called cat?

It's short for "concatenate." The original Unix command was designed to concatenate files, but it's often used just for viewing.

Does cat work on directories?

No. If you try cat mydir, you'll get an error: "Is a directory." Use ls to list directory contents.

Conclusion

Cat is a simple command with surprising depth. From viewing files to creating them, from combining data to debugging, it's a staple of Linux. Master cat, and you'll handle text files like a pro.

Remember: use it wisely, avoid the useless cat pattern, and always check your redirections. Now go ahead and try some of these examples in your terminal. You'll be surprised how often cat comes in handy.