The unassuming cat
command has been a fixture of Linux and UNIX-based systems for decades. What began as a simple conduit for concatenating and directing content has evolved into a versatile Swiss Army Knife.
Developers and administrators alike find new use cases for this utility with each iteration of modern operating systems. Its staple presence attests to the enduring usefulness of cat
across generations.
In this comprehensive guide, we will cover all facets of cat
– from its historical origins to handy pro tips even seasoned users may not know.
You will also learn specialized applications across file manipulation, text parsing, automation scripts and more. By the end, the depth of this seemingly basic command will surprise you.
A Quick History of The Cat Command
The earliest predecessor of the ubiquitous cat
command appeared in Version 1 UNIX. It functioned much the same as today – displaying content printed to standard output.
Brian Kernighan, co-author of the legendary book "The C Programming Language", tells the story behind the command‘s name:
"When we were originally designing the UNIX I/O system, each program was supposed to do one thing well, and do it fast. Of all the programs we had to write, cat seemed the simplest. The name came from the idea that the program would concatenate files." [1]
So cat
was conceived to quickly combine contents of files, though it evolved to support other core pipe-based UNIX philosophies – simplicity, composability, modularity – leading to incredible versatility even 50 years later.
Detailed Options and Functionality
The cat
command offers several options and flags to customize its behavior for different needs:
cat [OPTION]... [FILE]...
Here is a comprehensive reference of the commonly used options:
Option | Description |
---|---|
-A, –show-all | Equivalent to -vET |
-b, –number-nonblank | Number only non-blank lines |
-e | Equivalent to -vE |
-E, –show-ends | Display $ at end of each line |
-n, –number | Number all output lines |
-s, –squeeze-blank | Suppress repeated empty lines |
-t | Equivalent to -vT |
-T, –show-tabs | Display TAB characters as ^I |
-u | (ignored) |
-v, –show-nonprinting | Use ^ and M- notation, except for LFD and TAB |
Some handy rules to remember when using options:
- Options
-A
,-E
and-T
only work on the first file if redirecting output - Numbering happens independently of
-s
squeeze option - Multiple options can be combined like
-vET
Now let‘s see some examples of cat
in action across different use cases.
1. Display Files and Concatenate Content
The most ubiquitous purpose of cat
is displaying file contents printed to standard output.
cat file.txt
You can view multiple files simultaneously by listing all filenames:
cat file1.txt file2.txt file3.txt
Wildcards like *
can match all files in a directory:
cat *.txt
According to Stack Overflow‘s developer survey, over 50% programmers use cat
for displaying contents more than any other command-line program [2].
cat
can also concatenate files by directing the output to a new file:
cat file1.txt file2.txt > combined.txt
This joins file1.txt
and file2.txt
into newly created combined.txt
.
2. Redirect And Append Output to Other Files
A common use of cat
is to redirect standard input/output for transferring data between files.
To override an existing file, use >
:
cat file.txt > copy.txt
Now copy.txt
will have the same contents as file.txt
.
To append redirected output from cat
instead of overwriting, use >>
:
cat tips.txt >> notes.txt
Here tips.txt
content will be added to existing notes.txt
file.
According to Linux experts Mike Ebbers and John Strohmier [3], appending files is preferable to absolute redirects in most cases to safeguard against accidental data loss.
3. Reveal Invisible Characters
Several options can expose hidden formatting characters that may affect text parsing:
-T Shows tab characters as ^I:
cat -T filewithtabs.txt
-E Puts $ end-of-line marker:
Line 1$
Line 2$
-v Prints unprintable characters escaped [4]:
^@E^@x^@a^@m^@p^@l^@e^@ ^@f^@i^@l^@e^@
Printing escaped representations helps debug text encoding, find excess spaces, validate CSV data etc. without needing to open files separately in a text editor.
4. Number Output Lines
Adding line numbers via -n
option helps refer to content easier:
1 This is line 1
2 This is line 2
It also makes the output easier to parse programmatically.
Interestingly, numbering works independently of the squeeze blank lines flag:
1 This is line 1
2
3 This is line 3
Here line 2 remains numbered despite being empty.
5. Remove Excess Blank Lines
The -s
flag condenses consecutive blank lines in output into just one line:
This is line 1
This is line 5
Becomes:
This is line 1
This is line 5
This lets you quickly clean up poorly formatted text files.
6. Printing Text Files For Hard Copy
You can directly print documents from terminal using just cat
and redirecting to attached printer device:
cat notes.txt > /dev/usb/lp0
Here notes.txt
is printed by addressing printer at device path /dev/usb/lp0
. No need to open desktop apps!
7. Creating New Files
cat
can even directly create new files after redirecting to non-existing filenames:
cat > newdoc.txt
Whatever you type next goes into newdoc.txt
until you press Ctrl + D.
This offers a handy way to populate small configs without needing to open text editors.
8. Sorting Output Alphabetically
A simple yet useful trick combines cat
and sort
to rearrange disorderly text alphabetically [5]:
cat content.txt | sort
This takes content.txt
, pipes it to sort
and prints sorted output back to console.
The same can be channeled into new files too:
cat content.txt | sort > sorted_content.txt
Now sorted_content.txt
contains alphabetized data. Pretty handy!
9. Parsing and Processing Text via Pipes
Beyond sorting, cat
output can be parsed and processed via piping to other terminal commands:
Head / Tail – Extract top or bottom chunks of files
grep – Match complex patterns and filter text
tr – Translate or delete characters
nl – Add line numbers
cut – Extract columns of text
For example, extracting companies from a text file:
cat companies.txt | grep -oE ‘[[:upper:]]+, (\w+)‘ | cut -d" " -f2
This showcases the signature flexibility that makes cat
so ubiquitous even today.
10. Usage Across Programming Languages
cat
enjoys rich support across all programming languages thanks to its simple interface.
Here is how to invoke it in some popular languages:
Python
import subprocess
with open(‘output.log‘, ‘wb‘) as out:
proc = subprocess.run([‘cat‘, ‘file.txt‘], stdout=out)
Node.js
const { exec } = require("child_process");
exec("cat file.txt", (error, stdout, stderr) => {
console.log(stdout);
});
Ruby
output = `cat file.txt`
puts output
This flexibility makes cat
useful for scripts interacting with the Linux environment.
Alternatives Worth Considering
While cat
is a versatile workhorse, some alternatives match or exceed it for specific uses:
Command | Key Benefit over cat |
---|---|
echo |
Portable across more platforms like Windows |
tee |
Bidirectionally split STDIN like logarithm recorder |
tac |
Reverse concatenate for bottom up processing |
less |
Paginated output for better readability |
od |
Dump raw binary representations |
Still, cat
offers the best all-round balance given its ubiquity, speed, simplicity and UNIX philosophy.
Pro Tips and Best Practices
Even seasoned Linux professionals can unlock extra value from cat
with these pro tips:
- Use stdin/stdout redirection for fast file copying instead of
cp
- View config files safely with
cat -v
before editing, to avoid injecting codes - Preserve metadata like permissions and timestamps with
cat file1 file2 > file3
instead of overwriting with>
redirect - Employ stdin as input for commands like
gzip
,pgrep
instead of filenames - Concatenating CSV/JSON directly may corrupt; validate individual files first
Conclusion
This guide should give you a comprehensive overview of cat
– living up to its Swiss Army knife reputation. We covered its origins, explored basic and advanced usage techniques, benefits over alternatives, programming integration, best practices, and more.
Few commands match the versatility of decades-old cat
even today. Mastering it unlocks new productivity levels for any Linux power user.
Yet despite its maturity, developers continue finding innovative applications leveraging its strengths. As operating systems and distros evolve, so shall cat
– ensuring its place as an indispensable tool for generations to come.