That's a great question! The cat command is quite versatile, even though its primary purpose (short for "concatenate") is to display file contents. You're right to ask about its other uses.
Besides simply displaying a single file's content, here are some other common ways to use cat:
-
Concatenating multiple files:
You can combine the content of several files into one output.cat file1.txt file2.txt > combined_file.txtThis command would take the content of
file1.txt, append the content offile2.txtto it, and then redirect the combined output into a new file namedcombined_file.txt. -
Creating a new file:
You can usecatto quickly create a small text file directly from the terminal.cat > new_file.txtAfter pressing Enter, you can type your content. Each line you type will be added to
new_file.txt. When you're done, pressCtrl+D(on Linux/macOS) to save and exit. -
Appending to an existing file:
Similar to creating a new file, you can append text to an existing file without overwriting it.cat >> existing_file.txtAgain, type your content, and then press
Ctrl+Dto save. -
Displaying contents with line numbers (
-noption):
If you want to see the lines numbered, the-noption is very handy.cat -n /tmp/helloThis would output:
1 Hi, 2 I am Labby! -
Suppressing empty lines (
-soption):
If a file has multiple consecutive blank lines,cat -swill squeeze them into a single blank line.cat -s filename.txt -
Showing non-printing characters (
-voption):
This is useful for debugging or inspecting files that might have hidden characters.
While cat is super useful for these tasks, it's particularly well-suited for simple operations. For more complex text processing, tools like grep, sed, or awk are often used.
Does this give you a better idea of the other ways cat can be used? Let me know if you'd like an example of any of these!