What Does Cat Do Linux : Cat Command File Concatenation

The cat command in Linux displays the entire contents of a file directly in your terminal. If you have ever wondered “what does cat do linux”, the answer is simple: it is one of the most basic yet powerful commands for viewing, creating, and combining files. Short for “concatenate”, cat is a daily tool for anyone working in the Linux command line.

When you run cat, it reads files sequentially and prints their output to the standard output, which is usually your screen. This makes it perfect for quickly checking configuration files, logs, or any text-based content without opening a full editor.

In this guide, you will learn everything about the cat command. From basic usage to advanced tricks, we cover it all with clear examples. By the end, you will use cat like a pro.

What Does Cat Do Linux

Let’s answer the core question: “What Does Cat Do Linux” exactly? The cat command reads one or more files and copies them to standard output. It can also create new files, append content, and combine multiple files into one.

Think of cat as a simple file reader and writer. It does not edit files; it just shows or merges their contents. This makes it ideal for quick tasks where you need to see or combine text data.

Basic Syntax Of The Cat Command

The basic syntax is straightforward:

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

You can use options to modify behavior, like numbering lines or showing non-printable characters. If you do not specify a file, cat reads from standard input (your keyboard).

Common Options You Should Know

  • -n: Number all output lines
  • -b: Number non-empty lines only
  • -s: Squeeze multiple blank lines into one
  • -E: Show $ at the end of each line
  • -T: Show tab characters as ^I
  • -A: Equivalent to -vET (show all non-printing characters)

Viewing Files With Cat

The most common use is viewing file contents. For example, to see the contents of a file named “notes.txt”, type:

cat notes.txt

This prints the entire file to your terminal. If the file is long, the output scrolls quickly. You can combine cat with the less command to paginate output:

cat notes.txt | less

Or simply use less directly. But cat is still useful when you want to pipe output to other commands.

Viewing Multiple Files At Once

You can view multiple files in sequence. For instance:

cat file1.txt file2.txt

This prints file1.txt first, then file2.txt. The output appears as one continuous stream. This is helpful for quickly comparing or concatenating files.

Creating Files With Cat

Cat can create new files using input redirection. To create a file named “newfile.txt”, type:

cat > newfile.txt

Then type your content. Press Ctrl+D to save and exit. This overwrites any existing file with the same name. Be careful!

Appending To Files

To add content to an existing file without overwriting it, use the append operator (>>):

cat >> existingfile.txt

Type your new content and press Ctrl+D. The new text is added to the end of the file.

Combining Files With Cat

One of cat’s original purposes is concatenation. To combine multiple files into one, use output redirection:

cat file1.txt file2.txt > combined.txt

This creates a new file named “combined.txt” containing the contents of file1.txt followed by file2.txt. You can combine as many files as you want.

Using Cat With Pipes

Cat is often used in pipelines. For example, to count lines in a file:

cat file.txt | wc -l

Or to search for a pattern:

cat file.txt | grep "error"

However, many commands like grep and wc can take file names directly, so cat is sometimes unnecessary. But in complex pipelines, cat helps keep the flow clear.

Numbering Lines With Cat

When viewing code or logs, line numbers help. Use the -n option:

cat -n script.sh

This numbers every line, including blank ones. If you want to number only non-empty lines, use -b:

cat -b script.sh

Showing Special Characters

Sometimes you need to see hidden characters like tabs or line endings. Use -A to show all:

cat -A file.txt

This displays tabs as ^I and line endings as $. It is invaluable for debugging files with unexpected formatting.

Practical Examples Of Cat

Let’s look at real-world scenarios where cat shines.

Checking System Files

Quickly view configuration files:

cat /etc/passwd

Or check kernel messages:

cat /var/log/syslog

These commands show file contents instantly, helping you troubleshoot or verify settings.

Creating A Simple Text File

For a quick note, use cat:

cat > todo.txt
Buy groceries
Call dentist
Finish report
Ctrl+D

This creates a file with three lines. No editor needed.

Combining Log Files

If you have multiple log files from different days, combine them for analysis:

cat log1.txt log2.txt log3.txt > all_logs.txt

Then search the combined file with grep.

Cat Vs Other Commands

Cat is not always the best choice. For large files, use less or more to scroll. For viewing the beginning or end of a file, use head or tail. For editing, use nano or vim.

But cat excels when you need to output file contents to another command or combine files quickly. It is a fundamental building block.

Common Mistakes With Cat

Beginners often forget that cat outputs everything at once. For a long file, the terminal fills up fast. Use cat with less or use head/tail instead.

Another mistake is using cat to read binary files. This can garble your terminal. Use hexdump or od for binary data.

Advanced Cat Techniques

Once you master basics, try these advanced uses.

Using Cat With Here Documents

You can create files with multi-line input using heredocs:

cat << EOF > newfile.txt
Line 1
Line 2
EOF

This is useful in scripts for generating configuration files.

Concatenating With Process Substitution

Combine output from commands:

cat <(ls) <(date)

This shows the directory listing and date side by side. It is a powerful trick for comparing outputs.

Using Cat To Copy Files

While cp is standard, you can copy with cat:

cat source.txt > dest.txt

This works but is slower than cp. Use it only when you need to combine or modify during copy.

Cat And File Permissions

If you get a "Permission denied" error, you lack read access. Use sudo:

sudo cat /etc/shadow

Be careful with sensitive files. Cat does not check permissions; the shell does.

Handling Large Files

For huge files, cat can be slow. Use dd or split instead. But for most everyday files, cat is fast enough.

If you need to view a portion of a large file, combine cat with head or tail:

cat bigfile.txt | head -50

Cat In Scripts

Cat is frequently used in shell scripts to read configuration files or generate output. For example:

#!/bin/bash
cat /etc/hostname
echo "Hostname displayed"

It is simple and reliable.

Error Messages And Solutions

If cat says "No such file or directory", check the file path. Use tab completion to avoid typos.

If output looks garbled, the file might be binary. Use file command to check type before using cat.

Alternatives To Cat

Other commands offer similar functionality:

  • tac: Reverse order of lines
  • rev: Reverse characters on each line
  • nl: Number lines like cat -b
  • od: Octal dump for binary files

But cat remains the most versatile for everyday text viewing and combining.

Performance Tips

For very large files, avoid piping cat to grep. Instead, give grep the file directly:

grep "pattern" bigfile.txt

This is faster because grep reads the file itself without an extra process.

Frequently Asked Questions

Here are common questions about the cat command.

What is the difference between cat and less?

Cat displays the entire file at once, while less shows one screen at a time. Use less for long files.

Can cat edit files?

No, cat only reads and outputs. It cannot modify file contents directly. Use sed or awk for editing.

How do I stop cat output?

Press Ctrl+C to interrupt. If output is scrolling fast, close the terminal or use Ctrl+S to pause (Ctrl+Q to resume).

Why does cat show binary garbage?

Binary files contain non-printable characters that mess up your terminal. Use hexdump or strings instead.

Is cat safe for system files?

Reading with cat is safe, but writing with redirection can overwrite files. Always double-check your command.

Summary Of Cat Command

The cat command is a Linux staple. It displays, creates, and combines files with ease. Whether you are a beginner or expert, cat saves time and simplifies tasks.

Remember these key points:

  • Use cat to view small to medium files
  • Combine files with output redirection
  • Use options like -n and -b for line numbers
  • Avoid cat for binary files
  • Pipe to less for long output

Now you know exactly "what does cat do linux". Practice these examples in your terminal. The more you use cat, the more natural it becomes. It is a command you will reach for daily.

Start with simple file viewing, then experiment with concatenation and redirection. Before long, cat will be second nature in your Linux workflow.