Cheat Sheet #day51 - cat

Cheat Sheet #day51 - cat

cat Command Cheatsheet

The cat command in Unix/Linux is used to concatenate and display the contents of files. It stands for "concatenate" and is commonly used to read and output file content. Below are the basic and advanced usages of the cat command along with practical examples.

Basic Usage

  • Display the contents of a file

      cat filename
    
  • Display the contents of multiple files

      cat file1 file2
    
  • Concatenate multiple files and redirect the output to a new file

      cat file1 file2 > newfile
    

Common Options

  • Number all output lines

      cat -n filename
    
  • Show non-printing characters (except for tabs and the end of line)

      cat -v filename
    
  • Display line ends with a $ sign

      cat -E filename
    
  • Display tabs as ^I

      cat -T filename
    
  • Suppress repeated empty output lines

      cat -s filename
    

Examples

  • Display a single file

      cat file.txt
    
  • Display multiple files

      cat file1.txt file2.txt
    
  • Concatenate files and write to a new file

      cat file1.txt file2.txt > newfile.txt
    
  • Number all output lines

      cat -n file.txt
    
  • Show non-printing characters

      cat -v file.txt
    
  • Display line ends

      cat -E file.txt
    
  • Display tabs as ^I

      cat -T file.txt
    
  • Suppress repeated empty lines

      cat -s file.txt
    

Advanced Usage

  • Concatenate files and append to an existing file

      cat file1.txt file2.txt >> existingfile.txt
    
  • Create a new file with cat (end input with Ctrl+D)

      cat > newfile.txt
      This is the content of the new file.
    
  • Combine cat with other commands using pipes

      cat file.txt | grep "search_term"
    

Practical Tips

  • View Large Files: Use cat to quickly view the contents of large files, but consider using less or more for easier navigation.

  • Combine Files: Use cat to combine multiple files into a single file.

  • Create Files: Use cat to create and quickly input content into new files.

  • Use with Caution: Be careful with output redirection (>) to avoid accidentally overwriting important files.

Quick Reference

  • Display file contents:

      cat filename
    
  • Number all output lines:

      cat -n filename
    
  • Show non-printing characters:

      cat -v filename
    
  • Display line ends:

      cat -E filename
    
  • Display tabs as ^I:

      cat -T filename
    
  • Suppress repeated empty lines:

      cat -s filename
    
  • Concatenate files:

      cat file1 file2 > newfile
    
  • Append to an existing file:

      cat file1 file2 >> existingfile
    
  • Create a new file:

      cat > newfile.txt
    

This cheatsheet covers the essential commands and options for using cat effectively, from displaying file contents to concatenating and creating new files. Adjust the commands according to your specific requirements and environment.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!