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
|
0 1 2 3 4 5 6 |
cat [options] [file1 file2 ...] |
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
|
0 1 2 3 4 5 6 |
cat hello.sh |
→ Prints every line of hello.sh to screen.
Example: Suppose notes.txt has:
|
0 1 2 3 4 5 6 7 |
Line 1: Learning Bash Line 2: Hyderabad rocks! |
Then:
|
0 1 2 3 4 5 6 |
cat notes.txt |
Output:
|
0 1 2 3 4 5 6 7 |
Line 1: Learning Bash Line 2: Hyderabad rocks! |
Quick & simple — better than opening in editor for small files.
2. View Multiple Files at Once (True Concatenation!)
|
0 1 2 3 4 5 6 |
cat file1.txt file2.txt file3.txt |
→ Shows content of file1, then immediately file2, then file3 — like glued together.
Real example:
|
0 1 2 3 4 5 6 |
cat part1.txt part2.txt > full_story.txt |
→ 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!
|
0 1 2 3 4 5 6 |
cat > newfile.txt |
Then type whatever you want:
|
0 1 2 3 4 5 6 7 8 |
This is line one This is line two I love Bash! |
Press Ctrl + D (on empty line) to save and exit.
→ Creates newfile.txt with what you typed.
Append version (add to existing file):
|
0 1 2 3 4 5 6 |
cat >> existing.txt |
→ Adds new lines at the end (instead of overwriting).
4. Very Popular: cat with Redirection
|
0 1 2 3 4 5 6 7 |
cat file1.txt file2.txt > combined.txt # create/overwrite cat file3.txt >> combined.txt # append |
Or copy file:
|
0 1 2 3 4 5 6 |
cat old.txt > copy_of_old.txt |
(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:
|
0 1 2 3 4 5 6 7 |
cat -n script.sh # numbered lines — copy to share with friend cat -ns log.txt # clean view without too many blanks |
6. cat in Pipes (Where It Shines!)
cat is often used to feed file content into another command:
|
0 1 2 3 4 5 6 7 8 9 |
cat access.log | grep "error" # find errors cat file.txt | sort # sort lines cat data.csv | wc -l # count lines cat script.sh | bash # run script from file (rare but works) |
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:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
cat << EOF > config.yaml server: host: localhost port: 8080 debug: true EOF |
→ 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!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# Create sample files echo "Part 1: Hello" > part1.txt echo "Part 2: from Hyderabad!" > part2.txt # View one cat part1.txt # Concatenate cat part1.txt part2.txt # Save combined cat part1.txt part2.txt > full.txt cat full.txt # Numbered cat -n full.txt # Create new with input cat > todo.txt # type: Buy chai # Practice Bash # Ctrl+D to save cat todo.txt # Append echo "More practice" | cat >> todo.txt cat todo.txt |
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)?
