Chapter 10: Bash Concatenate (cat)

Bash Concatenate (cat) today! 😄

cat is one of the most useful and most misused commands in Bash/Linux. It’s everywhere — beginners love it, experts use it cleverly, and even servers run it thousands of times a day.

What does “cat” stand for? cat = catenate (an old word meaning “to link together” or “chain things in a sequence”). It’s not “concatenate” exactly (though many people say that) — it’s from the shorter form “catenate”. The job of cat is to read files and send their content to the screen (or somewhere else), and when you give it multiple files, it joins them one after another — that’s the “concatenate” part.

Imagine cat as a simple copy-paste machine:

  • Give it one file → shows the content
  • Give it three files → pastes file1 + file2 + file3 together
  • Give it nothing → waits for you to type (until Ctrl+D)

Basic Syntax

Bash

No file → reads from keyboard (stdin) One or more files → reads and shows them in order

1. Most Common Use: View Content of a File

Bash

→ Prints every line of hello.sh to screen.

Example: Suppose notes.txt has:

text

Then:

Bash

Output:

text

Quick & simple — better than opening in editor for small files.

2. View Multiple Files at Once (True Concatenation!)

Bash

→ Shows content of file1, then immediately file2, then file3 — like glued together.

Real example:

Bash

→ Combines two parts into one new file (very common for logs, configs, etc.)

3. Create a New File with cat (Here-Document Style)

This is super useful — no need for editor!

Bash

Then type whatever you want:

text

Press Ctrl + D (on empty line) to save and exit.

→ Creates newfile.txt with what you typed.

Append version (add to existing file):

Bash

→ Adds new lines at the end (instead of overwriting).

4. Very Popular: cat with Redirection

Bash

Or copy file:

Bash

(Though cp is usually better for binary files — cat is perfect for text.)

5. Useful Options (Add Power!)

Option What it does Example Command Why use it?
-n Number all lines cat -n script.sh See line numbers (great for debugging)
-b Number only non-blank lines cat -b long_log.txt Cleaner than -n
-s Squeeze multiple blank lines into one cat -s messy.txt Remove extra empty lines
-E Show $ at end of each line cat -E file.txt See trailing spaces/newlines
-T Show tabs as ^I cat -T tabbed.txt Find hidden tabs
-v Show non-printing characters cat -v control_chars.txt See hidden ^M, etc.
-A All of above (-vET) cat -A weird_file.txt Full debug mode

Most popular combo:

Bash

6. cat in Pipes (Where It Shines!)

cat is often used to feed file content into another command:

Bash

Many people do cat file | command — it’s okay, but sometimes unnecessary (command < file is better).

7. Here-Document (Advanced but Very Useful in Scripts)

In scripts, you can use cat to write multi-line text:

Bash

→ Creates config.yaml with that block. EOF can be any word — just match start and end.

8. Common Mistakes & Teacher Warnings

  • cat file > file → empties the file! (overwrites before reading — dangerous!)
  • Use cat file >> file? → Also bad (appends copy of itself forever)
  • For big binary files → better use cp or dd (cat can mess up)
  • cat is not good for huge files (loads everything) → use less or more to view

9. Practice Right Now (5 Minutes!)

Bash

See? cat is simple but does so many jobs!

Got it, boss? cat = your quick file viewer + glue for joining text + easy file creator.

Confused about any part? Want more on here-documents? Or next command like “less/more” for big files? Or “tac” (reverse cat)?

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *