What are other uses of the `cat` command?

QuestionsQuestions8 SkillsProDec, 17 2025
095

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:

  1. Concatenating multiple files:
    You can combine the content of several files into one output.

    cat file1.txt file2.txt > combined_file.txt

    This command would take the content of file1.txt, append the content of file2.txt to it, and then redirect the combined output into a new file named combined_file.txt.

  2. Creating a new file:
    You can use cat to quickly create a small text file directly from the terminal.

    cat > new_file.txt

    After pressing Enter, you can type your content. Each line you type will be added to new_file.txt. When you're done, press Ctrl+D (on Linux/macOS) to save and exit.

  3. 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.txt

    Again, type your content, and then press Ctrl+D to save.

  4. Displaying contents with line numbers (-n option):
    If you want to see the lines numbered, the -n option is very handy.

    cat -n /tmp/hello

    This would output:

         1  Hi,
         2  I am Labby!
  5. Suppressing empty lines (-s option):
    If a file has multiple consecutive blank lines, cat -s will squeeze them into a single blank line.

    cat -s filename.txt
  6. Showing non-printing characters (-v option):
    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!

0 Comments

no data
Be the first to share your comment!