cat
Concatenate and write files
cat is not printing to screen, it writes to standard output (stdout) and terminal is just displaying stdout
Concatenate multiple files
cat file1.txt file2.txt
Output:
file1 content
file2 content
Standard input
If no file is given, cat reads from stdin:
cat
Now it waits for your input. It echoes everything back immediately.
To stop press Ctrl + D (end of input)
-
a placeholder for live input. It lets you insert manual input between files.
cat file1.txt - file2.txt
Now the flow is:
- read file1.txt
- then wait for your typing
- then read file2.txt
Redirecting output
cat is often used with redirection:
cat file1.txt file2.txt > combined.txt
This creates a new file: combined.txt = file1 + file2
> overwrite
These are shell redirection operators, not cat features
cat original.txt > copy.txt
output of original.txt completely overwrite copy.txt, old content is deleted first then replaced
>> append (add to end)
cat file.txt >> combined.txt
output of cat file.txt is added to the END of combined.txt
If combined.txt already has content, old content stays, new content is appended after it
-n
Number all output lines (even blank lines), starting with 1:
cat -n file.txt
Output:
1 Hello
2 World
3 Linux
This option is ignored if -b is in effect.
-b
Number all nonempty output lines, starting with 1:
cat -b file.txt
Output:
1 Hello
2 World
(blank lines not numbered)
-s
squeeze repeated blank lines:
cat -s file.txt
Turns:
Hello
World
Into:
Hello
World
-E
Display a $ after the end of each line. The \r\n combination is shown as ^M$
cat -E file.txt
Output:
Hello$
World$
So you see where lines end.
-T
Display TAB characters as ^I
cat -T file.txt
Tabs become:
Hello^IWorld
-v
Display control characters except for LFD and TAB using ^ notation and precede characters that have the high bit set with M-
Useful for debugging corrupted files.
-A
Equivalent to -vET
cat -A file.txt