cat - Linux


Overview

The cat (concatenate) command in Linux is used to read, concatenate, and display file contents to the standard output. This utility is indispensable for viewing text, creating single files from multiple files, and redirecting output in scripts and command lines.

Syntax

The basic syntax for the cat command is:

cat [OPTION]... [FILE]...
  • [FILE]: One or more files whose contents you want to display. If no file is specified, or if the - is used, cat reads from the standard input.
  • [OPTION]: Modifiers that control the behavior of the command.

Options/Flags

  • -A, --show-all: Equivalent to -vET (show non-printing characters, except for spaces and tabs).
  • -b, --number-nonblank: Number all non-empty output lines, starting with 1.
  • -e: Equivalent to -vE (display non-printing characters and show $ at the end of each line).
  • -E, --show-ends: Display a $ at the end of each line, marking line endings.
  • -n, --number: Number all output lines, regardless of their content.
  • -s, --squeeze-blank: Squeeze multiple adjacent blank lines into a single blank line.
  • -T, --show-tabs: Display TAB characters as ^I.
  • -v, --show-nonprinting: Show non-printing characters (except line breaks and TAB); use ^ and M- notation.

Examples

  1. Display file contents:
    cat file.txt
    
  2. Concatenate multiple files:
    cat file1.txt file2.txt > combined.txt
    
  3. Show all contents with line numbers:
    cat -n file.txt
    
  4. Display contents with visible end lines and tabs:
    cat -ET file.txt
    
  5. Combine and compress multiple files:
    cat file1.txt file2.txt | gzip > combined.txt.gz
    

Common Issues

  • Permission Denied: You might lack the necessary permissions to read a file. Use sudo cat file.txt or adjust permissions.
  • File Not Found: Ensure the path and file name are correct.
  • Confusion with binary files: Using cat on binary files may produce strange symbols. Use file inspection tools like file or hexdump for binary files.

Integration

Combine cat with other commands to perform more complex operations:

  • Sort the contents of a file:
    cat file.txt | sort
    
  • Unique lines after concatenation:
    cat file1.txt file2.txt | sort | uniq
    
  • Check for a specific word in multiple files:
    cat *.txt | grep 'search-word'
    
  • tac: Display file contents in reverse.
  • more and less: For file viewing with page control.
  • grep: Search for patterns within files.
  • sort: Sort lines of text files.
  • uniq: Report or filter out repeated lines in a file.

Further documentation for the cat command can be found in the GNU coreutils online manuals.