Skip to content

Cat command linux guide concatenate

Command definition

The cat command in Linux is a versatile utility used for reading, displaying, and concatenating files. It allows users to view the contents of files directly in the terminal, combine multiple files into one, and create new files from existing ones. Additionally, cat can be used to redirect output to other commands or files, making it a fundamental tool for file manipulation and viewing in Unix-like operating systems.

Usage examples

  1. Display file contents: The most common use of cat is to display the contents of a file on the terminal. bash cat filename.txt

  2. Concatenate multiple files: You can combine the contents of several files into one output stream. bash cat file1.txt file2.txt

  3. Create a new file: You can use cat to create a new file by redirecting its output. bash cat > newfile.txt

  4. Append to a file: You can add the output of cat to the end of an existing file using the append operator. bash cat additionalfile.txt >> existingfile.txt

  5. Display line numbers: The -n option can be used to show line numbers alongside the file content. bash cat -n filename.txt

  6. Show non-printing characters: The -v option allows you to visualize non-printing characters in the output. bash cat -v filename.txt

  7. Combine files and redirect output: You can concatenate multiple files and save the output to a new file. bash cat file1.txt file2.txt > combined.txt

  8. Display contents of a file in reverse order: While cat does not reverse files directly, you can combine it with tac to achieve this. bash tac filename.txt

  9. Use cat with pipes: You can use cat to send file contents to other commands via pipes. bash cat filename.txt | grep "search_term"

  10. Show file content with pagination: You can pipe cat output to less for easier viewing of large files. bash cat filename.txt | less