The linux cat command is an essential tool for anyone working with Linux. It helps you view, combine, and create files easily. This guide will explain the syntax, uses, and real‑world examples to help you master this powerful command.

Table of Contents

what does cat command do in the linux?

The cat command in Linux stands for concatenate. It reads files sequentially and outputs their content to the terminal or another file. It is commonly used to display file content quickly or merge files.

How to use cat command in linux

The general syntax is simple:

cat [options] [file1] [file2] ...

You can use one or more files as input. The command outputs their contents in order.

Common Optios for linux cat command

The name cat is short for “concatenate.” It can be used to create files,
view files, or join multiple files together. Below are the flags used to modify its output.


-n, –number

The above options in linux cat command Adds line numbers to each line of output starting with the number “1.”

Example:

1  First line
2  
3  Third line
-b, –nonblank-number

This option replaces the -n option’s behavior in linux cat command.

Example:


   First line
1  Second line with text
-s, –squeeze-blank

Multiple blank lines are produced by multiple consecutive blank lines; linux cat command will enter everything but only one blank line) itself into a single blank space.

Example:


Text Block A

Text Block B
-E, –ends

Each line ends with the character “$,” useful when one wishes to identify unusual whitespace that is not visible or identify whitespace that is at the end of a blank line.

Example:


Hello World$
This line has spaces at the end$
-T, –tabs

Tab characters will be rendered as ^I. This functionality is helpful to debug files that require whitespace. This includes Python scripts, Makefiles, etc.

Example:


Header
^IIndented content
-v, –nonprinting

This uses the ^ and M- notation to display control characters that can’t be printed, except that LFD and T are omitted; this helps locate hidden control characters.

-A, –all

A “master” flag. It has the same result as using -v, -E, and -T together; this displays non-printing characters (including line endings and tabs) simultaneously.

-e and -t

-e = -v -E (show nonprinting and line endings)

-t = -v -T (show nonprinting and tabs).

Quick Reference Summary for linux cat command

Short Flag Long Flag Description
-A --show-all Equivalent to -vET
-b --number-nonblank Number non-empty lines only
-e (none) Equivalent to -vE
-E --show-ends Display $ at end of lines
-n --number Number all output lines
-s --squeeze-blank Squeeze multiple blank lines into one
-t (none) Equivalent to -vT
-T --show-tabs Show tabs as ^I
-v --show-nonprinting Show non-printing characters

20 Tips to Use Linux Cat Command Efficiently

The linux cat command is more than just a file viewer. By mastering these shortcuts and combinations, you can significantly speed up your command-line workflow.

  • Create a New File: Use cat > filename to quickly create a file and start typing content directly from the terminal.
  • Append to a File: Use the double redirection operator cat >> filename to add text to the end of an existing file without overwriting it.
  • Merge Multiple Files: Concatenate several files into a single new one by running cat file1 file2 file3 > combined.txt.
  • View Line Numbers: Use the -n flag to display line numbers, making it easier to reference specific parts of a script.
  • Search with Grep: Filter file content instantly by piping output: cat file.txt | grep "keyword".
  • Handle Large Files: Prevent long files from flooding your terminal by piping to a pager: cat largefile.log | less.
  • Suppress Empty Lines: Clean up output by using cat -s to “squeeze” multiple blank lines into one.
  • Identify Tab Characters: Use cat -T to show tabs as ^I, which is helpful for debugging Python or Makefile indentation.
  • Display Line Endings: Use cat -E to show a $ at the end of lines to find hidden trailing whitespace.
  • Show All Hidden Characters: Use the -A flag to see tabs, line ends, and non-printing characters simultaneously.
  • The cat command is one of the most versatile tools in the Linux toolkit. Beyond simply viewing text, it can be used for file creation, merging, and advanced debugging when paired with other utilities.

    • Make a New File:
      Type cat > filename in the terminal, then press “Enter”. Type the content in the terminal. When finished, press “Ctrl-D” to save the file.
    • Add to a File:
      Type cat >> filename in the terminal. Press “Enter” and type the content you want to add to the end of the existing content of the file. When finished, press “Ctrl-D” to save the file.
    • Combine Files:
      Divide the text files file1, file2 and file3 into the new text file combined.txt with the command cat file1 file2 file3 > combined.txt.
    • Display Line Numbers:
      You can add line numbers by typing cat -n filename in the terminal.
    • Find Words with Grep:
      You can use grep to filter the contents of a file and type a keyword in the terminal with the command cat filename | grep keyword.
    • Prevent Large Files from Overwhelming the Terminal:
      You can create a pipe for a long text file with big content to prevent your terminal from filling it up. Use the command cat largefile.log | less.
    • Suppress Empty Lines:
      You can suppress multiple blank lines to one blank line in the output of your terminal by typing cat -s in the terminal.
    • Detect Tabs in Text Files:
      To display the tab character as ^I, type cat -T filename. This will help you to debug indentation in both Python files and Makefile files.
    • Show Trailing End of Line Characters:
      Add $ to the end of all lines with cat -E to see where you’ve added trailing whitespace to your text files and make sure they are removed.
    • Display All Non-Printable Characters:
      Use the -A option in cat to display tab characters, end-of-line characters and non-printable characters, all in one command.
    • Display Numbered Non-Empty Lines In a File:
      To show the number of lines that have text in them, and not the blank lines in between them, use cat -b filename.
    • Display the First Few Lines In a File:
      To show a small portion of the files, you can filter the output using head: cat filename | head.
    • Display the Last Few Lines In a File:
      To display the last few lines at the end of the output: cat filename | tail.
    • Display the Contents of a File in Reversed Order:
      The tac command (cat spelled backwards) allows you to display the contents of the file backwards.
    • Avoid The Use Of cat | grep:
      Most commands take filename arguments directly. For better efficiency, use grep keyword filename instead of piping cat into grep.
    • Concatenate a file with stdout:
      To display the contents of a file, and then wait to input data manually to the stream, use cat filename -.
    • View All Control Characters In a File:
      Use cat -v to display non-printing control characters.
    • Delete All Contents Of a File:
      You can delete all the contents of a file (truncate it) by using the command: cat /dev/null > filename.
    • To Convert to Uppercase Or To Lowercase:
      Pipe the output to the tr command: cat file | tr 'a-z' 'A-Z'.
    • To Sort file contents:
      Use the sort command to show the contents of a file sorted in alphabetical order: cat filename | sort.

    By combining these techniques, you can transform cat from a simple display tool into a powerful text-processing utility.

    20 FAQs About the Linux Cat Command

    1. What does the ‘cat’ command stand for in linux cat command?
      It stands for ‘concatenate’, which means to link things together in a chain or series.
    2. What does cat command do in linux?
      Its most basic use is to display the entire content of a file directly in your terminal.
    3. Can I use ‘cat’ to view hidden files?
      Yes, if you know the exact name of the hidden file (which usually starts with a dot, like .myconfig), cat .myconfig will work.
    4. Is linux cat command suitable for viewing very large files?
      While it can, it is generally not recommended for very large files as it prints the entire content at once, which can flood your terminal. Tools like less or more are better for large files.
    5. How do I create an empty file using linux cat command?
      You can simply type cat > emptyfile.tx and then immediately press Ctrl + D.
    6. What is the difference between > and >> with cat?
      > (single greater-than) overwrites the destination file with new content. >> (double greater-than) appends content to the end of the destination file without deleting existing content.
    7. Can linux cat command read from standard input (keyboard)?
      Yes, when you use cat > filename, it reads what you type from the keyboard (standard input) until you press Ctrl+D.
    8. How do I display line numbers for only non-empty lines?
      Use the -b option: cat -b myfile.txt.
    9. What does cat -s do?
      The -s option “squeezes” or compresses multiple consecutive blank lines into a single blank line in the output.
    10. How can I see tab characters with linux cat command?
      Use cat -T (or –show-tabs) to display tab characters as ^I.
    11. Can linux cat command delete files?
      No, cat is for viewing, creating, or combining file contents. It does not have a delete function. You would use rm for that.
    12. Is ‘cat’ useful for scripting?
      Yes, cat is often used in shell scripts to quickly output file contents or feed data into other commands.
    13. How do I combine the contents of three files into a new fourth file?
      You would use cat file1.txt file2.txt file3.txt > combined.txt.
    14. Does ‘cat’ modify the original files?
      No, cat only reads from the input files and writes to standard output (or a new file if you use redirection). It never changes the source files.
    15. What does cat -E do?
      The -E option displays a dollar sign ($) at the end of each line, including blank ones, which can be useful for debugging line endings.
    16. Can I use wildcards with ‘cat’?
      Yes, you can use wildcards. For example, cat *.txt would display the contents of all files ending with .txt in the current directory.
    17. How is cat different from more or less?
      cat displays the entire file at once. more and less are pagers, meaning they display content one screen at a time, allowing you to scroll through large files.
    18. Why might ‘cat file | grep text’ be preferred over ‘grep text file’?
      While grep text file is more direct, cat file | grep text is sometimes used for consistency in pipelines or when the output of cat is first processed by another command before grep takes over.
    19. Can ‘cat’ be used to copy files?
      While technically cat source > destination will copy a file, it is not the primary or most efficient way. The cp command is designed specifically for copying files.
    20. Are there any security concerns with using ‘cat’?
      The command itself is safe. However, if you cat a sensitive file in a shared terminal, its content might be visible to others. Always be mindful of what you display.

    Conclusion

    Mastering the linux cat command is crucial for efficient Linux file management. It is a versatile tool that is simple yet powerful for viewing and manipulating files. Its simplicity makes it a favorite for quick tasks, while its ability to combine with other commands makes it indispensable for complex operations.